|
| 1 | +; |
| 2 | +; This algorithm takes an ASCII string of three decimal digits and |
| 3 | +; converts it into a binary number. R0 is used to collect the result. |
| 4 | +; R1 keeps track of how many digits are left to process. ASCIIBUFF |
| 5 | +; contains the most significant digit in the ASCII string. |
| 6 | +; |
| 7 | +ASCIItoBinary AND R0,R0,#0 ; R0 will be used for our result |
| 8 | + ADD R1,R1,#0 ; Test number of digits. |
| 9 | + BRz DoneAtoB ; There are no digits |
| 10 | +; |
| 11 | + LD R3,NegASCIIOffset ; R3 gets xFFD0, i.e., -x0030 |
| 12 | + LEA R2,ASCIIBUFF |
| 13 | + ADD R2,R2,R1 |
| 14 | + ADD R2,R2,#-1 ; R2 now points to "ones" digit |
| 15 | +; |
| 16 | + LDR R4,R2,#0 ; R4 <-- "ones" digit |
| 17 | + ADD R4,R4,R3 ; Strip off the ASCII template |
| 18 | + ADD R0,R0,R4 ; Add ones contribution |
| 19 | +; |
| 20 | + ADD R1,R1,#-1 |
| 21 | + BRz DoneAtoB ; The original number had one digit |
| 22 | + ADD R2,R2,#-1 ; R2 now points to "tens" digit |
| 23 | +; |
| 24 | + LDR R4,R2,#0 ; R4 <-- "tens" digit |
| 25 | + ADD R4,R4,R3 ; Strip off ASCII template |
| 26 | + LEA R5,LookUp10 ; LookUp10 is BASE of tens values |
| 27 | + ADD R5,R5,R4 ; R5 points to the right tens value |
| 28 | + LDR R4,R5,#0 |
| 29 | + ADD R0,R0,R4 ; Add tens contribution to total |
| 30 | +; |
| 31 | + ADD R1,R1,#-1 |
| 32 | + BRz DoneAtoB ; The original number had two digits |
| 33 | + ADD R2,R2,#-1 ; R2 now points to "hundreds" digit |
| 34 | +; |
| 35 | + LDR R4,R2,#0 ; R4 <-- "hundreds" digit |
| 36 | + ADD R4,R4,R3 ; Strip off ASCII template |
| 37 | + LEA R5,LookUp100 ; LookUp100 is hundreds BASE |
| 38 | + ADD R5,R5,R4 ; R5 points to hundreds value |
| 39 | + LDR R4,R5,#0 |
| 40 | + ADD R0,R0,R4 ; Add hundreds contribution to total |
| 41 | +; |
| 42 | +DoneAtoB RET |
| 43 | +NegASCIIOffset .FILL xFFD0 |
| 44 | +ASCIIBUFF .BLKW 4 |
| 45 | +LookUp10 .FILL #0 |
| 46 | + .FILL #10 |
| 47 | + .FILL #20 |
| 48 | + .FILL #30 |
| 49 | + .FILL #40 |
| 50 | + .FILL #50 |
| 51 | + .FILL #60 |
| 52 | + .FILL #70 |
| 53 | + .FILL #80 |
| 54 | + .FILL #90 |
| 55 | +; |
| 56 | +LookUp100 .FILL #0 |
| 57 | + .FILL #100 |
| 58 | + .FILL #200 |
| 59 | + .FILL #300 |
| 60 | + .FILL #400 |
| 61 | + .FILL #500 |
| 62 | + .FILL #600 |
| 63 | + .FILL #700 |
| 64 | + .FILL #800 |
| 65 | + .FILL #900 |
0 commit comments