lab3
This lab is for the student to understand how to use the math and string in 6502 assembly language, including displaying output, getting input from users and perform some operatiion based on the users' input. For this lab, I have designed and implement simple add calculator that accept the two input from the users. Check the input if they are number and then perform the add instruction and display the add output for the users.
The code:
define SCINIT $ff81 ; Initialize/clear screen
define CHRIN $ffcf ; Input character from keyboard
define CHROUT $ffd2 ; Output character to screen
define NUM1 = $01 ; Memory location for first number
define NUM2 = $02 ; Memory location for second number
define RESULT = $03 ; Memory location for result
START:
JSR SCINIT ; Clear the screen
; Display message to enter the first number
LDA #<MSG1
LDY #>MSG1
JSR PRINT_MSG
; Get the first number from the user
JSR GETNUM ; Call the number input routine
STA NUM1 ; Store the first number in memory
; Display message to enter the second number
LDA #<MSG2
LDY #>MSG2
JSR PRINT_MSG
; Get the second number from the user
JSR GETNUM ; Call the number input routine
STA NUM2 ; Store the second number in memory
; Perform the addition
CLC ; Clear carry before addition
LDA NUM1 ; Load the first number
ADC NUM2 ; Add the second number
STA RESULT ; Store the result in memory
; Display the result message
LDA #<MSG3
LDY #>MSG3
JSR PRINT_MSG
; Display the result
LDA RESULT ; Load the result
CLC ; Clear carry for ASCII conversion
ADC #$30 ; Convert number to ASCII
JSR CHROUT ; Output the result as a character
BRK ; End of program
; Routine to get number input from keyboard
GETNUM:
JSR CHRIN ; Read a character from the keyboard
CMP #$0D ; Compare with ASCII code for Enter (carriage return)
BEQ WAIT_ENTER ; If it's Enter, wait for a valid number input
CMP #$30 ; Compare with ASCII '0'
BMI GETNUM ; If input is less than '0', retry
CMP #$3A ; Compare with ASCII '9' + 1
BPL GETNUM ; If input is greater than '9', retry
PHA ; Save the input number on the stack
JSR CHROUT ; Echo the character back to the screen
WAIT_ENTER:
JSR CHRIN ; Wait for Enter key
CMP #$0D ; Compare with ASCII code for Enter
BNE WAIT_ENTER ; If not Enter, keep waiting
LDA #$0A ; Load line feed (newline)
JSR CHROUT ; Print new line
PLA ; Retrieve the input number from the stack
SEC ; Set carry flag for proper subtraction
SBC #$30 ; Convertdefine SCINIT $ff81 ; Initialize/clear screen
PRINT_MSG:
LDY #$00 ; Initialize Y to 0
CHARNEXT:
LDA MSG1,Y ; Load character from MSG+Y into A
BEQ MSGDONE ; If we've loaded a 0, we're done
JSR CHROUT ; Output the character
INY ; Move to next character
JMP CHARNEXT ; Repeat for next character
MSGDONE:
RTS ; Return from subroutine
MSG1:
DCB "E","N","T","E","R",$20,"T","H","E",$20
DCB "N","U","M","B","E","R",":"$00
评论
发表评论