Home » Archimedes archive » Archimedes World » AW-1997-01.adf » !Assembler_Assembler » CubeRoot

CubeRoot

This website contains an archive of files for the Acorn Electron, BBC Micro, Acorn Archimedes, Commodore 16 and Commodore 64 computers, which Dominic Ford has rescued from his private collection of floppy disks and cassettes.

Some of these files were originally commercial releases in the 1980s and 1990s, but they are now widely available online. I assume that copyright over them is no longer being asserted. If you own the copyright and would like files to be removed, please contact me.

Tape/disk: Home » Archimedes archive » Archimedes World » AW-1997-01.adf » !Assembler_Assembler
Filename: CubeRoot
Read OK:
File size: 061E bytes
Load address: 0000
Exec address: 0000
File contents
   10REM Assemble the cube root function
   20PROCassemble
   30:
   40REM Prompt for one number, so you can see that the
   41REM function is working
   50REPEAT
   60INPUT"Find cube root of ";A%
   70UNTIL A%>-1
   80REM The USR function takes the *address* of the machine code as its
   81REM parameter, not the parameter to pass to it!
   82REM This is subtly different from FNcube_root...
  100result%=USR(cube_root)
  110IF (result%*result%*result%)=A% THEN
  120 PRINT"Cube root is ";result%;" exactly" 
  130ELSE
  140 PRINT"Cube root is between ";result%-1;" and ";result%
  150ENDIF
  160:
  170REM Then do our speed test
  180PRINT'"Speed test... find cubes of all numbers from 1-10000"
  190:
  200TIME=0:FOR A%=1 TO 10000
  210v%=FNcube_root(A%)
  220NEXT:PRINT TIME/100;" seconds in BASIC"
  230:
  240TIME=0:FOR A%=1 TO 10000
  250v%=USR(cube_root)
  260NEXT:PRINT TIME/100;" seconds in ARM code"
  270:
  280END
  290:
  300DEF FNcube_root(n%)
  310check%=1:result%=0
  320WHILE (result%<n%):check%+=1:result%=check%*check%*check%:ENDWHILE
  330=n%
  340:
  350DEF PROCassemble
  360DIM code 256
  370P%=code:[OPT 2
  380:
  390.cube_root                    ; Enter with R0 = no. to find cube root of
  400          MOV       R1,#0     ; R1 = starting search value
  410.cube_search
  420          ADD       R1,R1,#1  ; Increase search value by 1
  430          MUL       R2,R1,R1  
  440          MUL       R2,R1,R2  ; Cube search value
  450          CMP       R2,R0     ; Compare it to our original number
  460          BLT       cube_search ; Loop around if it's less than our original
  470          :
  480          MOV       R0,R1     ; Move search value into return register
  490          :
  500          MOV       PC,R14    ; Return to BASIC
  510]
  520ENDPROC

%� Assemble the cube root function

�assemble
:
(4� Prompt for one number, so you can see that the
)� function is working
2�
<�"Find cube root of ";A%
F� A%>-1
PE� The USR function takes the *address* of the machine code as its
Q1� parameter, not the parameter to pass to it!
R2� This is subtly different from FNcube_root...
dresult%=�(cube_root)
n$� (result%*result%*result%)=A% �
x) �"Cube root is ";result%;" exactly" 
��
�7 �"Cube root is between ";result%-1;" and ";result%
��
�:
�� Then do our speed test
�<�'"Speed test... find cubes of all numbers from 1-10000"
�:
��=0:� A%=1 � 10000
�v%=�cube_root(A%)
�!�:� �/100;" seconds in BASIC"
�:
��=0:� A%=1 � 10000
�v%=�(cube_root)
$�:� �/100;" seconds in ARM code"
:
�
":
,� �cube_root(n%)
6check%=1:result%=0
@<ȕ (result%<n%):check%+=1:result%=check%*check%*check%:�
J=n%
T:
^� �assemble
h� code 256
rP%=code:[OPT 2
|:
�L.cube_root                    ; Enter with R0 = no. to find cube root of
�>          MOV       R1,#0     ; R1 = starting search value
�.cube_search
�>          ADD       R1,R1,#1  ; Increase search value by 1
�"          MUL       R2,R1,R1  
�5          MUL       R2,R1,R2  ; Cube search value
�E          CMP       R2,R0     ; Compare it to our original number
�P          BLT       cube_search ; Loop around if it's less than our original
�          :
�J          MOV       R0,R1     ; Move search value into return register
�          :
�3          MOV       PC,R14    ; Return to BASIC
�]
�
�
00000000  0d 00 0a 25 f4 20 41 73  73 65 6d 62 6c 65 20 74  |...%. Assemble t|
00000010  68 65 20 63 75 62 65 20  72 6f 6f 74 20 66 75 6e  |he cube root fun|
00000020  63 74 69 6f 6e 0d 00 14  0d f2 61 73 73 65 6d 62  |ction.....assemb|
00000030  6c 65 0d 00 1e 05 3a 0d  00 28 34 f4 20 50 72 6f  |le....:..(4. Pro|
00000040  6d 70 74 20 66 6f 72 20  6f 6e 65 20 6e 75 6d 62  |mpt for one numb|
00000050  65 72 2c 20 73 6f 20 79  6f 75 20 63 61 6e 20 73  |er, so you can s|
00000060  65 65 20 74 68 61 74 20  74 68 65 0d 00 29 19 f4  |ee that the..)..|
00000070  20 66 75 6e 63 74 69 6f  6e 20 69 73 20 77 6f 72  | function is wor|
00000080  6b 69 6e 67 0d 00 32 05  f5 0d 00 3c 1c e8 22 46  |king..2....<.."F|
00000090  69 6e 64 20 63 75 62 65  20 72 6f 6f 74 20 6f 66  |ind cube root of|
000000a0  20 22 3b 41 25 0d 00 46  0b fd 20 41 25 3e 2d 31  | ";A%..F.. A%>-1|
000000b0  0d 00 50 45 f4 20 54 68  65 20 55 53 52 20 66 75  |..PE. The USR fu|
000000c0  6e 63 74 69 6f 6e 20 74  61 6b 65 73 20 74 68 65  |nction takes the|
000000d0  20 2a 61 64 64 72 65 73  73 2a 20 6f 66 20 74 68  | *address* of th|
000000e0  65 20 6d 61 63 68 69 6e  65 20 63 6f 64 65 20 61  |e machine code a|
000000f0  73 20 69 74 73 0d 00 51  31 f4 20 70 61 72 61 6d  |s its..Q1. param|
00000100  65 74 65 72 2c 20 6e 6f  74 20 74 68 65 20 70 61  |eter, not the pa|
00000110  72 61 6d 65 74 65 72 20  74 6f 20 70 61 73 73 20  |rameter to pass |
00000120  74 6f 20 69 74 21 0d 00  52 32 f4 20 54 68 69 73  |to it!..R2. This|
00000130  20 69 73 20 73 75 62 74  6c 79 20 64 69 66 66 65  | is subtly diffe|
00000140  72 65 6e 74 20 66 72 6f  6d 20 46 4e 63 75 62 65  |rent from FNcube|
00000150  5f 72 6f 6f 74 2e 2e 2e  0d 00 64 18 72 65 73 75  |_root.....d.resu|
00000160  6c 74 25 3d ba 28 63 75  62 65 5f 72 6f 6f 74 29  |lt%=.(cube_root)|
00000170  0d 00 6e 24 e7 20 28 72  65 73 75 6c 74 25 2a 72  |..n$. (result%*r|
00000180  65 73 75 6c 74 25 2a 72  65 73 75 6c 74 25 29 3d  |esult%*result%)=|
00000190  41 25 20 8c 0d 00 78 29  20 f1 22 43 75 62 65 20  |A% ...x) ."Cube |
000001a0  72 6f 6f 74 20 69 73 20  22 3b 72 65 73 75 6c 74  |root is ";result|
000001b0  25 3b 22 20 65 78 61 63  74 6c 79 22 20 0d 00 82  |%;" exactly" ...|
000001c0  05 cc 0d 00 8c 37 20 f1  22 43 75 62 65 20 72 6f  |.....7 ."Cube ro|
000001d0  6f 74 20 69 73 20 62 65  74 77 65 65 6e 20 22 3b  |ot is between ";|
000001e0  72 65 73 75 6c 74 25 2d  31 3b 22 20 61 6e 64 20  |result%-1;" and |
000001f0  22 3b 72 65 73 75 6c 74  25 0d 00 96 05 cd 0d 00  |";result%.......|
00000200  a0 05 3a 0d 00 aa 1c f4  20 54 68 65 6e 20 64 6f  |..:..... Then do|
00000210  20 6f 75 72 20 73 70 65  65 64 20 74 65 73 74 0d  | our speed test.|
00000220  00 b4 3c f1 27 22 53 70  65 65 64 20 74 65 73 74  |..<.'"Speed test|
00000230  2e 2e 2e 20 66 69 6e 64  20 63 75 62 65 73 20 6f  |... find cubes o|
00000240  66 20 61 6c 6c 20 6e 75  6d 62 65 72 73 20 66 72  |f all numbers fr|
00000250  6f 6d 20 31 2d 31 30 30  30 30 22 0d 00 be 05 3a  |om 1-10000"....:|
00000260  0d 00 c8 16 d1 3d 30 3a  e3 20 41 25 3d 31 20 b8  |.....=0:. A%=1 .|
00000270  20 31 30 30 30 30 0d 00  d2 15 76 25 3d a4 63 75  | 10000....v%=.cu|
00000280  62 65 5f 72 6f 6f 74 28  41 25 29 0d 00 dc 21 ed  |be_root(A%)...!.|
00000290  3a f1 20 91 2f 31 30 30  3b 22 20 73 65 63 6f 6e  |:. ./100;" secon|
000002a0  64 73 20 69 6e 20 42 41  53 49 43 22 0d 00 e6 05  |ds in BASIC"....|
000002b0  3a 0d 00 f0 16 d1 3d 30  3a e3 20 41 25 3d 31 20  |:.....=0:. A%=1 |
000002c0  b8 20 31 30 30 30 30 0d  00 fa 13 76 25 3d ba 28  |. 10000....v%=.(|
000002d0  63 75 62 65 5f 72 6f 6f  74 29 0d 01 04 24 ed 3a  |cube_root)...$.:|
000002e0  f1 20 91 2f 31 30 30 3b  22 20 73 65 63 6f 6e 64  |. ./100;" second|
000002f0  73 20 69 6e 20 41 52 4d  20 63 6f 64 65 22 0d 01  |s in ARM code"..|
00000300  0e 05 3a 0d 01 18 05 e0  0d 01 22 05 3a 0d 01 2c  |..:.......".:..,|
00000310  14 dd 20 a4 63 75 62 65  5f 72 6f 6f 74 28 6e 25  |.. .cube_root(n%|
00000320  29 0d 01 36 16 63 68 65  63 6b 25 3d 31 3a 72 65  |)..6.check%=1:re|
00000330  73 75 6c 74 25 3d 30 0d  01 40 3c c8 95 20 28 72  |sult%=0..@<.. (r|
00000340  65 73 75 6c 74 25 3c 6e  25 29 3a 63 68 65 63 6b  |esult%<n%):check|
00000350  25 2b 3d 31 3a 72 65 73  75 6c 74 25 3d 63 68 65  |%+=1:result%=che|
00000360  63 6b 25 2a 63 68 65 63  6b 25 2a 63 68 65 63 6b  |ck%*check%*check|
00000370  25 3a ce 0d 01 4a 07 3d  6e 25 0d 01 54 05 3a 0d  |%:...J.=n%..T.:.|
00000380  01 5e 0f dd 20 f2 61 73  73 65 6d 62 6c 65 0d 01  |.^.. .assemble..|
00000390  68 0e de 20 63 6f 64 65  20 32 35 36 0d 01 72 12  |h.. code 256..r.|
000003a0  50 25 3d 63 6f 64 65 3a  5b 4f 50 54 20 32 0d 01  |P%=code:[OPT 2..|
000003b0  7c 05 3a 0d 01 86 4c 2e  63 75 62 65 5f 72 6f 6f  ||.:...L.cube_roo|
000003c0  74 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |t               |
000003d0  20 20 20 20 20 3b 20 45  6e 74 65 72 20 77 69 74  |     ; Enter wit|
000003e0  68 20 52 30 20 3d 20 6e  6f 2e 20 74 6f 20 66 69  |h R0 = no. to fi|
000003f0  6e 64 20 63 75 62 65 20  72 6f 6f 74 20 6f 66 0d  |nd cube root of.|
00000400  01 90 3e 20 20 20 20 20  20 20 20 20 20 4d 4f 56  |..>          MOV|
00000410  20 20 20 20 20 20 20 52  31 2c 23 30 20 20 20 20  |       R1,#0    |
00000420  20 3b 20 52 31 20 3d 20  73 74 61 72 74 69 6e 67  | ; R1 = starting|
00000430  20 73 65 61 72 63 68 20  76 61 6c 75 65 0d 01 9a  | search value...|
00000440  10 2e 63 75 62 65 5f 73  65 61 72 63 68 0d 01 a4  |..cube_search...|
00000450  3e 20 20 20 20 20 20 20  20 20 20 41 44 44 20 20  |>          ADD  |
00000460  20 20 20 20 20 52 31 2c  52 31 2c 23 31 20 20 3b  |     R1,R1,#1  ;|
00000470  20 49 6e 63 72 65 61 73  65 20 73 65 61 72 63 68  | Increase search|
00000480  20 76 61 6c 75 65 20 62  79 20 31 0d 01 ae 22 20  | value by 1..." |
00000490  20 20 20 20 20 20 20 20  20 4d 55 4c 20 20 20 20  |         MUL    |
000004a0  20 20 20 52 32 2c 52 31  2c 52 31 20 20 0d 01 b8  |   R2,R1,R1  ...|
000004b0  35 20 20 20 20 20 20 20  20 20 20 4d 55 4c 20 20  |5          MUL  |
000004c0  20 20 20 20 20 52 32 2c  52 31 2c 52 32 20 20 3b  |     R2,R1,R2  ;|
000004d0  20 43 75 62 65 20 73 65  61 72 63 68 20 76 61 6c  | Cube search val|
000004e0  75 65 0d 01 c2 45 20 20  20 20 20 20 20 20 20 20  |ue...E          |
000004f0  43 4d 50 20 20 20 20 20  20 20 52 32 2c 52 30 20  |CMP       R2,R0 |
00000500  20 20 20 20 3b 20 43 6f  6d 70 61 72 65 20 69 74  |    ; Compare it|
00000510  20 74 6f 20 6f 75 72 20  6f 72 69 67 69 6e 61 6c  | to our original|
00000520  20 6e 75 6d 62 65 72 0d  01 cc 50 20 20 20 20 20  | number...P     |
00000530  20 20 20 20 20 42 4c 54  20 20 20 20 20 20 20 63  |     BLT       c|
00000540  75 62 65 5f 73 65 61 72  63 68 20 3b 20 4c 6f 6f  |ube_search ; Loo|
00000550  70 20 61 72 6f 75 6e 64  20 69 66 20 69 74 27 73  |p around if it's|
00000560  20 6c 65 73 73 20 74 68  61 6e 20 6f 75 72 20 6f  | less than our o|
00000570  72 69 67 69 6e 61 6c 0d  01 d6 0f 20 20 20 20 20  |riginal....     |
00000580  20 20 20 20 20 3a 0d 01  e0 4a 20 20 20 20 20 20  |     :...J      |
00000590  20 20 20 20 4d 4f 56 20  20 20 20 20 20 20 52 30  |    MOV       R0|
000005a0  2c 52 31 20 20 20 20 20  3b 20 4d 6f 76 65 20 73  |,R1     ; Move s|
000005b0  65 61 72 63 68 20 76 61  6c 75 65 20 69 6e 74 6f  |earch value into|
000005c0  20 72 65 74 75 72 6e 20  72 65 67 69 73 74 65 72  | return register|
000005d0  0d 01 ea 0f 20 20 20 20  20 20 20 20 20 20 3a 0d  |....          :.|
000005e0  01 f4 33 20 20 20 20 20  20 20 20 20 20 4d 4f 56  |..3          MOV|
000005f0  20 20 20 20 20 20 20 50  43 2c 52 31 34 20 20 20  |       PC,R14   |
00000600  20 3b 20 52 65 74 75 72  6e 20 74 6f 20 42 41 53  | ; Return to BAS|
00000610  49 43 0d 01 fe 05 5d 0d  02 08 05 e1 0d ff        |IC....].......|
0000061e