8-bit number input in 6502 machine code ======================================= Scan 8-bit hexadecimal 0-FF, preserving X register ================================================== Scan string to parse hexadecimal number \ On entry (lptr)=>start of text string \ num = holds number being scanned \ On exit CC=number scanned, A=number \ CS=no number scanned or bad number \ (lptr),Y=>terminating non-digit character \ X=preserved \ Size 62 bytes .ScanHex JSR CheckHexDigit :\ Get current digit BCS ScanHexExit :\ Nothing to scan .ScanHexLp STA num :\ Store as current number JSR CheckHexDigit :\ Get current digit BCS ScanHexDone :\ No more digits PHA:LDA num :\ Save current digit CMP #16:BCS ScanHexTooBig :\ If num>15, will overflow ASL A:ASL A:ASL A:ASL A :\ num=num*16 STA num PLA:ADC num:BCC ScanHexLp :\ num=num*16+digit PHA :\ Pre-balance stack .ScanHexTooBig PLA .ScanHexExit SEC:RTS :\ CS=bad number or digit .ScanHexDone LDA num:CLC:RTS :\ CC=valid number scanned : .CheckHexDigit LDA (lptr),Y :\ Get current character CMP #ASC"0":BCC ScanHexExit :\ <'0' CMP #ASC"9"+1:BCS ChkHexOk :\ <='9' AND #&DF :\ Force to upper case CMP #ASC"A":BCC ScanHexExit :\ <'A' CMP #ASC"F"+1:BCS ScanHexExit :\ >'F' SBC #6 :\ Reduce hex letter .ChkHexOk INY:AND #15:RTS :\ Convert character to binary Scan 8-bit decimal 0-255, preserving X register =============================================== Scan string to parse decimal number \ On entry (lptr)=>start of text string \ On exit CC=number scanned, A=number \ CS=no number scanned or bad number \ (lptr),Y=>terminating non-digit character \ num=number scanned \ X=preserved \ Size 53 bytes .ScanDecimal JSR ScanChkDigit :\ Get current digit BCS ScanDecExit :\ Nothing to scan .ScanDecLp STA num :\ Store as current number JSR CheckDigit :\ Get current digit BCS ScanDecDone :\ No more digits PHA:LDA num :\ Save current digit \ \ Non-optimised multiply by ten \ ASL A:BCS ScanDecTooBig :\ num=num*2 \ ASL A:BCS ScanDecTooBig :\ num=num*4 \ ADC num:BCS ScanDecTooBig :\ num=num*5 \ ASL A:BCS ScanDecTooBig :\ num=num*10 \ \ Optimised multiply by ten CMP #26:BCS ScanDecTooBig :\ If num>25, will overflow ASL A:ASL A:ADC num:ASL A :\ num=num*10 \ STA num:PLA :\ Get current digit back ADC num:BCC ScanDecLp :\ num=num*10+digit PHA :\ Pre-balance stack .ScanDecTooBig PLA .ScanDecExit SEC:RTS :\ CS=bad number or digit .ScanDecDone LDA num:CLC:RTS :\ CC=valid number scanned : .CheckDigit LDA (lptr),Y :\ Get current character CMP #ASC"0":BCC ScanDecExit :\ <'0' CMP #ASC"9"+1:BCS ScanDecExit :\ >'9' INY:AND #15:RTS :\ Convert character to binary Scan 8-bit decimal 00-99 ======================== Scan 8-bit decimal number by scanning as a hex number, treating it as BCD and converting it to binary. \ On entry (lptr)=>start of text string \ On exit CC=number scanned, X=number \ CS=no number scanned or bad number \ (lptr),Y=>terminating non-digit character \ Note No checks made for hex being invalid BCD, for \ example 1A or C2 \ Size 76 bytes (16 bytes + 62 bytes ScanHex) .ScanDec JSR ScanHex :\ Scan 8-bit hex number BCS ScanDecExit :\ Too big or bad number : \ Fall through into BCDtoBIN .BCDtoBIN LDX #&FF :\ Start with result=-1 SEC :\ Prepare for subtraction SED :\ Switch to Decimal arithmetic .BCDtoBINlp INX :\ Add 1 to result SBC #1 :\ Subtract 1 with BCD arithmetic BCS BCDtoBINlp :\ Loop until BCD value < 0 CLD :\ Switch back to Binary arithmetic .ScanDecExit RTS :\ Note, returns with X=binary number Scan for short number ===================== Scans for hex or dec 0-15 or 0-F, suitable for eg. ROM numbers \ On entry (lptr)=>start of text string \ On exit CC=number scanned, A=number \ CS=no number scanned or bad number \ (lptr),Y=>terminating non-digit character \ X=preserved \ Size 61 bytes .ScanShortNum JSR CheckHexDigit :\ Get current digit BCS ScanShortExit :\ Nothing to scan CMP #1:BNE ScanShortEnd :\ Not '1', check for end of number JSR CheckHexDigit :\ Get next digit BCC ScanShortTen :\ Valid digit, check terminator LDA #1:BCS ScanShortEnd :\ Just '1', check terminator .ScanShortTen ADC #10 :\ Convert 1x to 10-15 \ \ The following could be CMP #20 to allow 0-19 decimal CMP #16:BCS ScanShortEnd :\ 16-19, too big, return CS .ScanShortEnd PHA:JSR CheckHexDigit :\ Get following character PLA:BCC ScanShortExit :\ Followed by another digit, so is not ok CLC:RTS :\ Return number, CC=ok .ScanShortExit SEC:RTS :\ CS=not valid number : .CheckHexDigit LDA (lptr),Y :\ Get current character CMP #ASC"0":BCC ScanShortExit :\ <'0' CMP #ASC"9"+1:BCS ChkHexOk :\ <='9' AND #&DF :\ Force to upper case CMP #ASC"A":BCC ScanShortExit :\ <'A' CMP #ASC"F"+1:BCS ScanShortExit :\ >'F' SBC #6 :\ Reduce hex letter .ChkHexOk INY:AND #15:RTS :\ Convert character to binary Combined ScanHex and ScanShort ============================== Uses 8-bit hex scan to also scan short decimals \ On entry (lptr)=>start of text string \ On exit CC=number scanned, A=number \ CS=no number scanned or bad number \ (lptr),Y=>terminating non-digit character \ X=preserved \ Size 79 bytes (17 bytes + 62 bytes ScanHex) .ScanShort JSR ScanHex:BCS ScanShortExit :\ CS=no number or invalid number CMP #&16:BCS ScanShortExit :\ >&15, exit with CS=invalid number CMP #&10:BCC ScanShortExit :\ <&10, exit with CC=valid dec or hex number SBC #5:CLC :\ Reduce &10-&15 to 10-15 .ScanShortExit RTS Combined ScanDecimal and ScanShort ================================== Uses 8-bit decimal scan to also scan short decimals \ On entry (lptr)=>start of text string \ On exit CC=number scanned, A=number \ CS=no number scanned or bad number \ (lptr),Y=>terminating non-digit character \ num=number scanned \ X=preserved \ Size xx bytes (xx bytes + 53 bytes ScanDecimal) .ScanShort LDA (lptr),Y :\ Get current character CMP #ASC"A":BCC ScanShortDec :\ <'A', scan as decimal AND #&DF CMP #ASC"F"+1:BCS ScanShortExit :\ >'F', exit with CS=invalid number ...something .ScanShortDec JSR ScanDecimal:BCS ScanShortExit :\ CS=no number or invalid number CMP #16 :\ >15, exit with CS=invalid number .ScanShortExit RTS