.StringToNumber LDY #0 \ We want to work through the input buffer, converting \ each character in turn from an ASCII digit into a \ number, so set an index in Y to work through the \ buffer, one ASCII digit at a time LDX #0 \ Each pair of ASCII digits gets converted into a value \ that will fit into a single BCD byte, which we store \ in-place, so set an index in X to work through the \ buffer, so we can store the resulting BCD number one \ byte at a time (i.e. two ASCII digits at a time) .snum1 \ We now fetch two digits from the input buffer and \ convert them into a single BCD number, remembering \ that the input buffer is stored as an ascending stack, \ so the digits on the left of the stack (i.e. those \ that were typed first) are lower significance than \ those on the right of the stack (i.e. those that were \ typed last) \ \ Effectively the stack is little-endian, just like the \ 6502 processor \ \ The calls to DigitToNumber will backfill the input \ buffer with &FF if we are reading from the last four \ characters of the input buffer, so the final result \ will have four BCD numbers at the start of inputBuffer \ (from inputBuffer to inputBuffer+3), and the rest of \ the buffer will be padded out with four &FF bytes \ (from inputBuffer+4 to inputBuffer+7) JSR DigitToNumber \ Set T to the numerical value of the character at index STA T \ Y in the input buffer, which is the low significance \ digit of the number we are fetching, and in the range \ 0 to 9 INY \ Increment Y to the next character in the input buffer JSR DigitToNumber \ Set A to the numerical value of the character at index \ Y in the input buffer, which is the high significance \ digit of the number we are fetching, and in the range \ 0 to 9 ASL A \ Shift the high significance digit in A into bits 4-7, ASL A \ so A contains the first digit of the BCD number ASL A ASL A ORA T \ Insert the high significance digit in T into bits 0-3, \ so A now contains both the first and second digits of \ the BCD number STA inputBuffer,X \ Store the BCD number in-place at index X INX \ Increment the result index in X to move on to the next \ BCD number INY \ Increment the buffer index in Y to move on to the next \ pair of digits CPY #8 \ Loop back until we have converted the whole string BNE snum1 \ into a multi-byte BCD number RTS \ Return from the subroutineName: StringToNumber [Show more] Type: Subroutine Category: Text Summary: Convert a string of ASCII digits in the input buffer in-place into a multi-byte BCD numberContext: See this subroutine in context in the source code References: This subroutine is called as follows: * MainTitleLoop calls StringToNumber
[X]
Subroutine DigitToNumber (category: Text)
Convert a digit from the input buffer into a number
[X]
Variable inputBuffer in workspace Main variable workspace
The eight-byte keyboard input buffer
[X]
Label snum1 is local to this routine