Skip to navigation


Market: gnum

[Commodore 64 version]

Name: gnum [Show more] Type: Subroutine Category: Market Summary: Get a number from the keyboard
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * EQSHP calls gnum * TT210 calls gnum * TT219 calls gnum

Get a number from the keyboard, up to the maximum number in QQ25, for the buying and selling of cargo and equipment. Pressing "Y" will return the maximum number (i.e. buy/sell all items), while pressing "N" will abort the sale and return a 0. Pressing a key with an ASCII code less than ASCII "0" will return a 0 in A (so that includes pressing Space or Return), while pressing a key with an ASCII code greater than ASCII "9" will jump to the Inventory screen (so that includes all letters and most punctuation).
Arguments: QQ25 The maximum number allowed
Returns: A The number entered R Also contains the number entered C flag Set if the number is too large (> QQ25), clear otherwise
.gnum LDA #MAG2 ; Switch the text colour to purple STA COL2 LDX #0 ; We will build the number entered in R, so initialise STX R ; it with 0 LDX #12 ; We will check for up to 12 key presses, so set a STX T1 ; counter in T1 .TT223 JSR TT217 ; Scan the keyboard until a key is pressed, and return ; the key's ASCII code in A (and X) LDX R ; If R is non-zero then skip to NWDAV2, as we are BNE NWDAV2 ; already building a number CMP #'Y' ; If "Y" was pressed, jump to NWDAV1 to return the BEQ NWDAV1 ; maximum number allowed (i.e. buy/sell the whole stock) CMP #'N' ; If "N" was pressed, jump to NWDAV3 to return from the BEQ NWDAV3 ; subroutine with a result of 0 (i.e. abort transaction) .NWDAV2 STA Q ; Store the key pressed in Q SEC ; Subtract ASCII "0" from the key pressed, to leave the SBC #'0' ; numeric value of the key in A (if it was a number key) BCC OUT ; If A < 0, jump to OUT to load the current number and ; return from the subroutine, as the key pressed was ; RETURN (or some other character with a value less than ; ASCII "0") CMP #10 ; If A >= 10, jump to BAY2 to display the Inventory BCS BAY2 ; screen, as the key pressed was a letter or other ; non-digit and is greater than ASCII "9" STA S ; Store the numeric value of the key pressed in S LDA R ; Fetch the result so far into A CMP #26 ; If A >= 26, where A is the number entered so far, then BCS OUT ; adding a further digit will make it bigger than 256, ; so jump to OUT to return from the subroutine with the ; result in R (i.e. ignore the last key press) ASL A ; Set A = (A * 2) + (A * 8) = A * 10 STA T ASL A ASL A ADC T ADC S ; Add the pressed digit to A and store in R, so R now STA R ; contains its previous value with the new key press ; tacked onto the end CMP QQ25 ; If the result in R = the maximum allowed in QQ25, jump BEQ TT226 ; to TT226 to print the key press and keep looping (the ; BEQ is needed because the BCS below would jump to OUT ; if R >= QQ25, which we don't want) BCS OUT ; If the result in R > QQ25, jump to OUT to return from ; the subroutine with the result in R .TT226 LDA Q ; Print the character in Q (i.e. the key that was JSR TT26 ; pressed, as we stored the ASCII value in Q earlier) DEC T1 ; Decrement the loop counter BNE TT223 ; Loop back to TT223 until we have checked for 12 digits .OUT LDA #$10 ; Switch the text colour to white STA COL2 LDA R ; Set A to the result we have been building in R RTS ; Return from the subroutine .NWDAV1 ; If we get here then "Y" was pressed, so we return the ; maximum number allowed, which is in QQ25 JSR TT26 ; Print the character for the key that was pressed LDA QQ25 ; Set R = QQ25, so we return the maximum value allowed STA R JMP OUT ; Jump to OUT to return from the subroutine .NWDAV3 ; If we get here then "N" was pressed, so we return 0 JSR TT26 ; Print the character for the key that was pressed LDA #0 ; Set R = 0, so we return 0 STA R JMP OUT ; Jump to OUT to return from the subroutine