|
| 1 | +;================================================ |
| 2 | +; ASM Test |
| 3 | +; ParseInt - String to Value |
| 4 | +;================================================ |
| 5 | + |
| 6 | +;================================================= |
| 7 | +; Constants |
| 8 | +;================================================= |
| 9 | + |
| 10 | +assert_zero EQU $00D0000C ; magical register |
| 11 | + |
| 12 | +;================================================= |
| 13 | +; Entry Point |
| 14 | +;================================================= |
| 15 | + |
| 16 | +start: |
| 17 | + |
| 18 | + lea number1,a0 ; parse number 1 |
| 19 | + bsr ParseInt10 ; |
| 20 | + move.l d0,d1 ; |
| 21 | + |
| 22 | + lea number2,a0 ; parse number 2 |
| 23 | + bsr ParseInt10 ; |
| 24 | + move.l d0,d2 ; |
| 25 | + |
| 26 | + lea number3,a0 ; parse number 3 |
| 27 | + bsr ParseInt16 ; |
| 28 | + move.l d0,d3 ; |
| 29 | + |
| 30 | + add.l d1,d2 ; n1 + n2 + n3 |
| 31 | + add.l d3,d2 ; |
| 32 | + |
| 33 | + subi.l #$6C575,d2 ; raise error if |
| 34 | + move.l d2,assert_zero ; d3 != precalc |
| 35 | + |
| 36 | + stop #-1 ; stop execution |
| 37 | + |
| 38 | +;================================================ |
| 39 | +; ParseInt Subroutine (string to value) |
| 40 | +; Input : A0 = Null-terminating string |
| 41 | +; Output : D0 = Long value |
| 42 | +;================================================ |
| 43 | + |
| 44 | +ParseInt10: |
| 45 | + move.l #10,d6 ; decimal - base 10 |
| 46 | + bra ParseInt ; |
| 47 | +ParseInt16: |
| 48 | + move.l #16,d6 ; hexadecimal - base 16 |
| 49 | +ParseInt: |
| 50 | + clr.l d0 ; reset digit |
| 51 | + move.l #0,d7 ; reset result |
| 52 | +.loop |
| 53 | + move.b (a0)+,d0 ; read string |
| 54 | + beq.b .exit ; exit if null-char |
| 55 | + bsr ParseDigit ; read digit in d0 |
| 56 | + mulu.l d6,d7 ; result x 10 |
| 57 | + add.l d0,d7 ; result + digit |
| 58 | + bra .loop ; continue |
| 59 | +.exit |
| 60 | + move.l d7,d0 ; put result in d0 |
| 61 | + rts ; exit subroutine |
| 62 | + |
| 63 | +;================================================ |
| 64 | +; ParseDigit Subroutine (char to value) |
| 65 | +; Input : D0 = Digit char |
| 66 | +; Output : D0 = Byte value |
| 67 | +;================================================ |
| 68 | + |
| 69 | +ParseDigit: |
| 70 | + cmpi.b #'0',d0 ; '0' <= d0 <= '9' |
| 71 | + blt .exit ; |
| 72 | + cmpi.b #'9',d0 ; |
| 73 | + bls .numeric ; |
| 74 | + cmpi.b #'A',d0 ; 'A' <= d0 <= 'F' |
| 75 | + blt .exit ; |
| 76 | + cmpi.b #'F',d0 ; |
| 77 | + bls .alphanum1 ; |
| 78 | + cmpi.b #'a',d0 ; 'a' <= d0 <= 'f' |
| 79 | + blt .exit ; |
| 80 | + cmpi.b #'f',d0 ; |
| 81 | + bls .alphanum2 ; |
| 82 | + bra .exit ; invalid char |
| 83 | +.numeric |
| 84 | + subi.b #'0',d0 ; numeric |
| 85 | + bra .exit ; |
| 86 | +.alphanum1 |
| 87 | + subi.b #'A'-10,d0 ; alphanum uppercase |
| 88 | + bra .exit ; |
| 89 | +.alphanum2 |
| 90 | + subi.b #'a'-10,d0 ; alphanum lowercase |
| 91 | +.exit |
| 92 | + rts ; exit subroutine |
| 93 | + |
| 94 | +;================================================ |
| 95 | +; Data Section |
| 96 | +;================================================ |
| 97 | + |
| 98 | +number1: dc.b '12345',0 ; $00003039 |
| 99 | +number2: dc.b '400000',0 ; + $00061A80 |
| 100 | +number3: dc.b '7AbC',0 ; + $00007ABC |
| 101 | + ; ----------- |
| 102 | + ; = $0006C575 |
| 103 | +;================================================ |
| 104 | +; End of program |
| 105 | +;================================================ |
0 commit comments