Home » Archimedes archive » Acorn Computing » 1995 03.adf » 9503 » TechForum/Barnett/ReadMe
TechForum/Barnett/ReadMe
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 » Acorn Computing » 1995 03.adf » 9503 |
Filename: | TechForum/Barnett/ReadMe |
Read OK: | ✔ |
File size: | 0B01 bytes |
Load address: | 0000 |
Exec address: | 0000 |
File contents
---------------------------------- DivMod16 --------------------------------- Divides 16-bit number by 16-bit number. I haven't worked out how to tackle the Challenge in the December issue, so this is just based on the conventional approach - not bad at ONLY 36 instructions/ticks! The usual trick in division routines is to store the quotient in the bottom of the dividend as the latter is shifted out into the remainder:- remainder = 0 for each bit do (remainder, dividend) = (remainder, dividend) << 1 if remainder >= divisor then dividend += 1 remainder -= divisor endif next Since the arguments here are 16-bits, the remainder is stored in the top half of 'dnd' and the dividend/quotient in the bottom half. ; Initially dsr = d ; The initialisation MOV dsr,dsr,LSL #16 dsr = d << 16 ; divisor in top half of dsr SUB dsr,dsr,#1 dsr = (d << 16) - 1 ; The body (repeated 16 times) RSBS dnd,dsr,dnd,LSL #1 dnd = (dnd << 1) - (d << 16) + 1 ; shift next bit of dividend into ; remainder, subtract divisor from ; remainder, increment quotient, ; carry = 0 (borrow) if remainder too ; small ADDCC dnd,dnd,dsr if remainder too small then dnd = dnd + (d << 16) - 1 ; restore original remainder, decrement ; quotient ; The finalisation MOV dsr,dnd,LSR #16 dsr = dnd >> 16 ; remainder in dsr BIC dnd,dnd,dsr,LSL #16 dnd = dnd BIC (dsr << 16) ; quotient in dnd ------------------------------------ Sqrt ----------------------------------- Calculates the square root of an unsigned number. ; The initialisation MOV dst,#0 The root MOV square,#0 square = root ^ 2 MOV bit,#15 MOV one,#1 ; The loop .next_bit MOV temp,dst,LSL #1 temp = 2 * root ADD temp,temp,one,LSL bit temp = 2 * root + (2 ^ bit) ADD temp,square,temp,LSL bit temp = root ^ 2 + (2 * root + (2 ^ bit)) * (2 ^ bit) therefore temp = (root + (2 ^ bit)) ^ 2 ; new_root ^ 2 CMP temp,src MOVLS square,temp if not too big then square = new square ; new_root ^ 2 ADDLS dst,dst,one,LSL bit if not too big then root = new root SUBS bit,bit,#1 BPL next_bit ---------------------------------- Speech! ---------------------------------- Did you know that Speech! (version 1.30) expects text to be terminated by ASCII NULL or CR, but not LF? This sounds reasonable, until you realise that Obey files end their lines with LF, so you can't use *Say, etc., directly in such files. Aargh! Also, it doesn't handle errors properly, so SYS "XSpch_Speak", "Q" TO ; Flags% will always generate an error. -----------------------------------------------------------------------------
00000000 0a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |.---------------| 00000010 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |----------------| 00000020 2d 2d 2d 20 44 69 76 4d 6f 64 31 36 20 2d 2d 2d |--- DivMod16 ---| 00000030 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |----------------| 00000040 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 0a |--------------..| 00000050 44 69 76 69 64 65 73 20 31 36 2d 62 69 74 20 6e |Divides 16-bit n| 00000060 75 6d 62 65 72 20 62 79 20 31 36 2d 62 69 74 20 |umber by 16-bit | 00000070 6e 75 6d 62 65 72 2e 0a 0a 49 20 68 61 76 65 6e |number...I haven| 00000080 27 74 20 77 6f 72 6b 65 64 20 6f 75 74 20 68 6f |'t worked out ho| 00000090 77 20 74 6f 20 74 61 63 6b 6c 65 20 74 68 65 20 |w to tackle the | 000000a0 43 68 61 6c 6c 65 6e 67 65 20 69 6e 20 74 68 65 |Challenge in the| 000000b0 20 44 65 63 65 6d 62 65 72 20 69 73 73 75 65 2c | December issue,| 000000c0 20 73 6f 0a 74 68 69 73 20 69 73 20 6a 75 73 74 | so.this is just| 000000d0 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 63 6f | based on the co| 000000e0 6e 76 65 6e 74 69 6f 6e 61 6c 20 61 70 70 72 6f |nventional appro| 000000f0 61 63 68 20 2d 20 6e 6f 74 20 62 61 64 20 61 74 |ach - not bad at| 00000100 20 4f 4e 4c 59 20 33 36 0a 69 6e 73 74 72 75 63 | ONLY 36.instruc| 00000110 74 69 6f 6e 73 2f 74 69 63 6b 73 21 0a 0a 54 68 |tions/ticks!..Th| 00000120 65 20 75 73 75 61 6c 20 74 72 69 63 6b 20 69 6e |e usual trick in| 00000130 20 64 69 76 69 73 69 6f 6e 20 72 6f 75 74 69 6e | division routin| 00000140 65 73 20 69 73 20 74 6f 20 73 74 6f 72 65 20 74 |es is to store t| 00000150 68 65 20 71 75 6f 74 69 65 6e 74 20 69 6e 20 74 |he quotient in t| 00000160 68 65 20 62 6f 74 74 6f 6d 0a 6f 66 20 74 68 65 |he bottom.of the| 00000170 20 64 69 76 69 64 65 6e 64 20 61 73 20 74 68 65 | dividend as the| 00000180 20 6c 61 74 74 65 72 20 69 73 20 73 68 69 66 74 | latter is shift| 00000190 65 64 20 6f 75 74 20 69 6e 74 6f 20 74 68 65 20 |ed out into the | 000001a0 72 65 6d 61 69 6e 64 65 72 3a 2d 0a 0a 20 20 72 |remainder:-.. r| 000001b0 65 6d 61 69 6e 64 65 72 20 3d 20 30 0a 20 20 66 |emainder = 0. f| 000001c0 6f 72 20 65 61 63 68 20 62 69 74 20 64 6f 0a 20 |or each bit do. | 000001d0 20 20 20 28 72 65 6d 61 69 6e 64 65 72 2c 20 64 | (remainder, d| 000001e0 69 76 69 64 65 6e 64 29 20 3d 20 28 72 65 6d 61 |ividend) = (rema| 000001f0 69 6e 64 65 72 2c 20 64 69 76 69 64 65 6e 64 29 |inder, dividend)| 00000200 20 3c 3c 20 31 0a 20 20 20 20 69 66 20 72 65 6d | << 1. if rem| 00000210 61 69 6e 64 65 72 20 3e 3d 20 64 69 76 69 73 6f |ainder >= diviso| 00000220 72 20 74 68 65 6e 0a 20 20 20 20 20 20 64 69 76 |r then. div| 00000230 69 64 65 6e 64 20 2b 3d 20 31 0a 20 20 20 20 20 |idend += 1. | 00000240 20 72 65 6d 61 69 6e 64 65 72 20 2d 3d 20 64 69 | remainder -= di| 00000250 76 69 73 6f 72 0a 20 20 20 20 65 6e 64 69 66 0a |visor. endif.| 00000260 20 20 6e 65 78 74 0a 0a 53 69 6e 63 65 20 74 68 | next..Since th| 00000270 65 20 61 72 67 75 6d 65 6e 74 73 20 68 65 72 65 |e arguments here| 00000280 20 61 72 65 20 31 36 2d 62 69 74 73 2c 20 74 68 | are 16-bits, th| 00000290 65 20 72 65 6d 61 69 6e 64 65 72 20 69 73 20 73 |e remainder is s| 000002a0 74 6f 72 65 64 20 69 6e 20 74 68 65 20 74 6f 70 |tored in the top| 000002b0 0a 68 61 6c 66 20 6f 66 20 27 64 6e 64 27 20 61 |.half of 'dnd' a| 000002c0 6e 64 20 74 68 65 20 64 69 76 69 64 65 6e 64 2f |nd the dividend/| 000002d0 71 75 6f 74 69 65 6e 74 20 69 6e 20 74 68 65 20 |quotient in the | 000002e0 62 6f 74 74 6f 6d 20 68 61 6c 66 2e 0a 0a 3b 20 |bottom half...; | 000002f0 49 6e 69 74 69 61 6c 6c 79 0a 0a 20 20 64 73 72 |Initially.. dsr| 00000300 20 3d 20 64 0a 0a 3b 20 54 68 65 20 69 6e 69 74 | = d..; The init| 00000310 69 61 6c 69 73 61 74 69 6f 6e 0a 0a 4d 4f 56 20 |ialisation..MOV | 00000320 64 73 72 2c 64 73 72 2c 4c 53 4c 20 23 31 36 0a |dsr,dsr,LSL #16.| 00000330 0a 20 20 64 73 72 20 3d 20 64 20 3c 3c 20 31 36 |. dsr = d << 16| 00000340 20 3b 20 64 69 76 69 73 6f 72 20 69 6e 20 74 6f | ; divisor in to| 00000350 70 20 68 61 6c 66 20 6f 66 20 64 73 72 0a 0a 53 |p half of dsr..S| 00000360 55 42 20 64 73 72 2c 64 73 72 2c 23 31 0a 0a 20 |UB dsr,dsr,#1.. | 00000370 20 64 73 72 20 3d 20 28 64 20 3c 3c 20 31 36 29 | dsr = (d << 16)| 00000380 20 2d 20 31 0a 0a 3b 20 54 68 65 20 62 6f 64 79 | - 1..; The body| 00000390 20 28 72 65 70 65 61 74 65 64 20 31 36 20 74 69 | (repeated 16 ti| 000003a0 6d 65 73 29 0a 0a 52 53 42 53 20 64 6e 64 2c 64 |mes)..RSBS dnd,d| 000003b0 73 72 2c 64 6e 64 2c 4c 53 4c 20 23 31 0a 0a 20 |sr,dnd,LSL #1.. | 000003c0 20 64 6e 64 20 3d 20 28 64 6e 64 20 3c 3c 20 31 | dnd = (dnd << 1| 000003d0 29 20 2d 20 28 64 20 3c 3c 20 31 36 29 20 2b 20 |) - (d << 16) + | 000003e0 31 20 3b 20 73 68 69 66 74 20 6e 65 78 74 20 62 |1 ; shift next b| 000003f0 69 74 20 6f 66 20 64 69 76 69 64 65 6e 64 20 69 |it of dividend i| 00000400 6e 74 6f 0a 20 20 20 20 20 20 20 20 20 20 20 20 |nto. | 00000410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 | | 00000420 20 20 20 20 20 20 20 3b 20 72 65 6d 61 69 6e 64 | ; remaind| 00000430 65 72 2c 20 73 75 62 74 72 61 63 74 20 64 69 76 |er, subtract div| 00000440 69 73 6f 72 20 66 72 6f 6d 0a 20 20 20 20 20 20 |isor from. | 00000450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 | | 00000460 20 20 20 20 20 20 20 20 20 20 20 20 20 3b 20 72 | ; r| 00000470 65 6d 61 69 6e 64 65 72 2c 20 69 6e 63 72 65 6d |emainder, increm| 00000480 65 6e 74 20 71 75 6f 74 69 65 6e 74 2c 0a 20 20 |ent quotient,. | 00000490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 | | * 000004b0 20 3b 20 63 61 72 72 79 20 3d 20 30 20 28 62 6f | ; carry = 0 (bo| 000004c0 72 72 6f 77 29 20 69 66 20 72 65 6d 61 69 6e 64 |rrow) if remaind| 000004d0 65 72 20 74 6f 6f 0a 20 20 20 20 20 20 20 20 20 |er too. | 000004e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 | | 000004f0 20 20 20 20 20 20 20 20 20 20 3b 20 73 6d 61 6c | ; smal| 00000500 6c 0a 0a 41 44 44 43 43 20 64 6e 64 2c 64 6e 64 |l..ADDCC dnd,dnd| 00000510 2c 64 73 72 0a 0a 20 20 69 66 20 72 65 6d 61 69 |,dsr.. if remai| 00000520 6e 64 65 72 20 74 6f 6f 20 73 6d 61 6c 6c 20 74 |nder too small t| 00000530 68 65 6e 0a 0a 20 20 20 20 64 6e 64 20 3d 20 64 |hen.. dnd = d| 00000540 6e 64 20 2b 20 28 64 20 3c 3c 20 31 36 29 20 2d |nd + (d << 16) -| 00000550 20 31 20 3b 20 72 65 73 74 6f 72 65 20 6f 72 69 | 1 ; restore ori| 00000560 67 69 6e 61 6c 20 72 65 6d 61 69 6e 64 65 72 2c |ginal remainder,| 00000570 20 64 65 63 72 65 6d 65 6e 74 0a 20 20 20 20 20 | decrement. | 00000580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 | | 00000590 20 20 20 20 20 20 20 20 20 3b 20 71 75 6f 74 69 | ; quoti| 000005a0 65 6e 74 0a 0a 3b 20 54 68 65 20 66 69 6e 61 6c |ent..; The final| 000005b0 69 73 61 74 69 6f 6e 0a 0a 4d 4f 56 20 64 73 72 |isation..MOV dsr| 000005c0 2c 64 6e 64 2c 4c 53 52 20 23 31 36 0a 0a 20 20 |,dnd,LSR #16.. | 000005d0 64 73 72 20 3d 20 64 6e 64 20 3e 3e 20 31 36 20 |dsr = dnd >> 16 | 000005e0 3b 20 72 65 6d 61 69 6e 64 65 72 20 69 6e 20 64 |; remainder in d| 000005f0 73 72 0a 0a 42 49 43 20 64 6e 64 2c 64 6e 64 2c |sr..BIC dnd,dnd,| 00000600 64 73 72 2c 4c 53 4c 20 23 31 36 0a 0a 20 20 64 |dsr,LSL #16.. d| 00000610 6e 64 20 3d 20 64 6e 64 20 42 49 43 20 28 64 73 |nd = dnd BIC (ds| 00000620 72 20 3c 3c 20 31 36 29 20 3b 20 71 75 6f 74 69 |r << 16) ; quoti| 00000630 65 6e 74 20 69 6e 20 64 6e 64 0a 0a 2d 2d 2d 2d |ent in dnd..----| 00000640 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |----------------| * 00000660 20 53 71 72 74 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d | Sqrt ----------| 00000670 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |----------------| 00000680 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 0a 43 61 6c 63 75 |---------..Calcu| 00000690 6c 61 74 65 73 20 74 68 65 20 73 71 75 61 72 65 |lates the square| 000006a0 20 72 6f 6f 74 20 6f 66 20 61 6e 20 75 6e 73 69 | root of an unsi| 000006b0 67 6e 65 64 20 6e 75 6d 62 65 72 2e 0a 0a 3b 20 |gned number...; | 000006c0 54 68 65 20 69 6e 69 74 69 61 6c 69 73 61 74 69 |The initialisati| 000006d0 6f 6e 0a 0a 4d 4f 56 20 64 73 74 2c 23 30 0a 0a |on..MOV dst,#0..| 000006e0 20 20 54 68 65 20 72 6f 6f 74 0a 0a 4d 4f 56 20 | The root..MOV | 000006f0 73 71 75 61 72 65 2c 23 30 0a 0a 20 20 73 71 75 |square,#0.. squ| 00000700 61 72 65 20 3d 20 72 6f 6f 74 20 5e 20 32 0a 0a |are = root ^ 2..| 00000710 4d 4f 56 20 62 69 74 2c 23 31 35 0a 4d 4f 56 20 |MOV bit,#15.MOV | 00000720 6f 6e 65 2c 23 31 0a 0a 3b 20 54 68 65 20 6c 6f |one,#1..; The lo| 00000730 6f 70 0a 0a 2e 6e 65 78 74 5f 62 69 74 0a 4d 4f |op...next_bit.MO| 00000740 56 20 74 65 6d 70 2c 64 73 74 2c 4c 53 4c 20 23 |V temp,dst,LSL #| 00000750 31 0a 0a 20 20 74 65 6d 70 20 3d 20 32 20 2a 20 |1.. temp = 2 * | 00000760 72 6f 6f 74 0a 0a 41 44 44 20 74 65 6d 70 2c 74 |root..ADD temp,t| 00000770 65 6d 70 2c 6f 6e 65 2c 4c 53 4c 20 62 69 74 0a |emp,one,LSL bit.| 00000780 0a 20 20 74 65 6d 70 20 3d 20 32 20 2a 20 72 6f |. temp = 2 * ro| 00000790 6f 74 20 2b 20 28 32 20 5e 20 62 69 74 29 0a 0a |ot + (2 ^ bit)..| 000007a0 41 44 44 20 74 65 6d 70 2c 73 71 75 61 72 65 2c |ADD temp,square,| 000007b0 74 65 6d 70 2c 4c 53 4c 20 62 69 74 0a 0a 20 20 |temp,LSL bit.. | 000007c0 74 65 6d 70 20 3d 20 72 6f 6f 74 20 5e 20 32 20 |temp = root ^ 2 | 000007d0 2b 20 28 32 20 2a 20 72 6f 6f 74 20 2b 20 28 32 |+ (2 * root + (2| 000007e0 20 5e 20 62 69 74 29 29 20 2a 20 28 32 20 5e 20 | ^ bit)) * (2 ^ | 000007f0 62 69 74 29 0a 0a 20 20 74 68 65 72 65 66 6f 72 |bit).. therefor| 00000800 65 0a 0a 20 20 74 65 6d 70 20 3d 20 28 72 6f 6f |e.. temp = (roo| 00000810 74 20 2b 20 28 32 20 5e 20 62 69 74 29 29 20 5e |t + (2 ^ bit)) ^| 00000820 20 32 20 3b 20 6e 65 77 5f 72 6f 6f 74 20 5e 20 | 2 ; new_root ^ | 00000830 32 0a 0a 43 4d 50 20 74 65 6d 70 2c 73 72 63 0a |2..CMP temp,src.| 00000840 0a 4d 4f 56 4c 53 20 73 71 75 61 72 65 2c 74 65 |.MOVLS square,te| 00000850 6d 70 0a 0a 20 20 69 66 20 6e 6f 74 20 74 6f 6f |mp.. if not too| 00000860 20 62 69 67 20 74 68 65 6e 0a 20 20 20 20 73 71 | big then. sq| 00000870 75 61 72 65 20 3d 20 6e 65 77 20 73 71 75 61 72 |uare = new squar| 00000880 65 20 3b 20 6e 65 77 5f 72 6f 6f 74 20 5e 20 32 |e ; new_root ^ 2| 00000890 0a 0a 41 44 44 4c 53 20 64 73 74 2c 64 73 74 2c |..ADDLS dst,dst,| 000008a0 6f 6e 65 2c 4c 53 4c 20 62 69 74 0a 0a 20 20 69 |one,LSL bit.. i| 000008b0 66 20 6e 6f 74 20 74 6f 6f 20 62 69 67 20 74 68 |f not too big th| 000008c0 65 6e 0a 20 20 20 20 72 6f 6f 74 20 3d 20 6e 65 |en. root = ne| 000008d0 77 20 72 6f 6f 74 0a 0a 53 55 42 53 20 62 69 74 |w root..SUBS bit| 000008e0 2c 62 69 74 2c 23 31 0a 42 50 4c 20 6e 65 78 74 |,bit,#1.BPL next| 000008f0 5f 62 69 74 0a 0a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |_bit..----------| 00000900 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |----------------| 00000910 2d 2d 2d 2d 2d 2d 2d 2d 20 53 70 65 65 63 68 21 |-------- Speech!| 00000920 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d | ---------------| 00000930 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |----------------| 00000940 2d 2d 2d 0a 0a 44 69 64 20 79 6f 75 20 6b 6e 6f |---..Did you kno| 00000950 77 20 74 68 61 74 20 53 70 65 65 63 68 21 20 28 |w that Speech! (| 00000960 76 65 72 73 69 6f 6e 20 31 2e 33 30 29 20 65 78 |version 1.30) ex| 00000970 70 65 63 74 73 20 74 65 78 74 20 74 6f 20 62 65 |pects text to be| 00000980 20 74 65 72 6d 69 6e 61 74 65 64 20 62 79 0a 41 | terminated by.A| 00000990 53 43 49 49 20 4e 55 4c 4c 20 6f 72 20 43 52 2c |SCII NULL or CR,| 000009a0 20 62 75 74 20 6e 6f 74 20 4c 46 3f 20 20 54 68 | but not LF? Th| 000009b0 69 73 20 73 6f 75 6e 64 73 20 72 65 61 73 6f 6e |is sounds reason| 000009c0 61 62 6c 65 2c 20 75 6e 74 69 6c 20 79 6f 75 20 |able, until you | 000009d0 72 65 61 6c 69 73 65 0a 74 68 61 74 20 4f 62 65 |realise.that Obe| 000009e0 79 20 66 69 6c 65 73 20 65 6e 64 20 74 68 65 69 |y files end thei| 000009f0 72 20 6c 69 6e 65 73 20 77 69 74 68 20 4c 46 2c |r lines with LF,| 00000a00 20 73 6f 20 79 6f 75 20 63 61 6e 27 74 20 75 73 | so you can't us| 00000a10 65 20 2a 53 61 79 2c 20 65 74 63 2e 2c 0a 64 69 |e *Say, etc.,.di| 00000a20 72 65 63 74 6c 79 20 69 6e 20 73 75 63 68 20 66 |rectly in such f| 00000a30 69 6c 65 73 2e 20 20 41 61 72 67 68 21 0a 0a 41 |iles. Aargh!..A| 00000a40 6c 73 6f 2c 20 69 74 20 64 6f 65 73 6e 27 74 20 |lso, it doesn't | 00000a50 68 61 6e 64 6c 65 20 65 72 72 6f 72 73 20 70 72 |handle errors pr| 00000a60 6f 70 65 72 6c 79 2c 20 73 6f 0a 0a 20 20 53 59 |operly, so.. SY| 00000a70 53 20 22 58 53 70 63 68 5f 53 70 65 61 6b 22 2c |S "XSpch_Speak",| 00000a80 20 22 51 22 20 54 4f 20 3b 20 46 6c 61 67 73 25 | "Q" TO ; Flags%| 00000a90 0a 0a 77 69 6c 6c 20 61 6c 77 61 79 73 20 67 65 |..will always ge| 00000aa0 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72 2e |nerate an error.| 00000ab0 0a 0a 0a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |...-------------| 00000ac0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |----------------| * 00000b00 0a |.| 00000b01