Skip to navigation

Text: DigitToNumber

Name: DigitToNumber [Show more] Type: Subroutine Category: Text Summary: Convert a digit from the input buffer into a number
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * StringToNumber calls DigitToNumber

Arguments: Y The offset into the input buffer of the digit to convert
Returns: A The numerical value of the digit (0 to 9), where spaces are converted to 0
.DigitToNumber LDA inputBuffer,Y \ Set A to the ASCII digit from the input buffer that we \ want to convert CPY #4 \ If Y < 4 then jump to dnum1 to skip the following BCC dnum1 \ Y is 4 or more, so we set this character in the input \ buffer to &FF so that as we work through the buffer in \ the StringToNumber routine, converting pairs of ASCII \ digits into single-byte BCD numbers, we backfill the \ buffer with &FF PHA \ Set the Y-th character in the input buffer to &FF, LDA #&FF \ making sure not to corrupt the value of A STA inputBuffer,Y PLA .dnum1 CMP #' ' \ If the character in the input buffer is not a space BNE dnum2 \ then it must be a digit, so jump to dmum2 to convert \ it into a number LDA #'0' \ Otherwise the character from the input buffer is a \ space, so set A to ASCII "0" so we return a value of \ zero in the following subtraction .dnum2 SEC \ Convert the ASCII digit into a number by subtracting SBC #'0' \ ASCII "0" RTS \ Return from the subroutine