Skip to navigation

Revs on the BBC Micro

Text: GetNumberFromText

Name: GetNumberFromText [Show more] Type: Subroutine Category: Text Summary: Convert a two-digit string into a number
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * GetNumberInput calls GetNumberFromText

Arguments: T The first digit of the number, as text U The second digit of the number, as text
Returns: A The numerical value of the number C flag The status of the conversion: * Clear if the string is a valid number and A <= 40 * Set if string is not a valid number, or A > 40
.GetNumberFromText LDA T \ Set A to the character containing the first digit CMP #' ' \ If the first digit is not a space, skip the following BNE tnum1 \ instruction LDA #'0' \ The first digit is a space, so convert it to a "0" .tnum1 SEC \ Subtract the ASCII value for "0" to get the numerical SBC #'0' \ value of the first digit into A CMP #10 \ If the value of the first digit is greater than 10, BCS tnum2 \ then this is not a valid number, so jump to tnum2 to \ return from the subroutine with the C flag set, to \ indicate an error STA T \ Set T = the value of the first digit LDX U \ Set X to the character containing the second digit CPX #' ' \ If the second digit is a space, then jump to tnum2 to CLC \ return from the subroutine with the value of the first BEQ tnum2 \ digit in A and the C flag clear, to indicate success ASL A \ Set T = (A << 2 + T) << 1 ASL A \ = (A * 4 + A) * 2 ADC T \ = 10 * A ASL A \ STA T \ So T contains 10 * the numerical value of the first \ digit TXA \ Set A to the character containing the second digit SEC \ Subtract the ASCII value for "0" to get the numerical SBC #'0' \ value of the second digit CMP #10 \ If the value of the second digit is greater than 10, BCS tnum2 \ then this is not a valid number, so jump to tnum2 to \ return from the subroutine with the C flag set, to \ indicate an error ADC T \ Set A = A + T \ = the numerical value of the second digit \ + 10 * the numerical value of the first digit \ \ which is the numerical value of the two-digit string CMP #41 \ If A < 41, clear the C flag, otherwise set it .tnum2 RTS \ Return from the subroutine