LAB3
Introduction:
This project is a number guessing game on a 6502 microprocessor. The player has to guess a random number between 0 and 9. The game gives the player 4 chances to guess. After each guess, the program tells the player if the guess is "Too low!", "Too high!", or "Correct!". The game ends when the player guesses the number or runs out of guesses. The program checks the player's input, gives feedback, and tracks how many guesses are left. The correct number is saved in memory, and at the end, the game shows a "Game Over" message with the correct number. After each guess, the game also changes the screen color. This routine makes the screen color cycle, creating a visual effect that shows the progress of the game. It helps make the game more interactive and visually shows how the game is going.
Output:
Too low:
Too High:
Correct Answer:
Screen Color after each guess:
Challenges and Reflection:
For this lab, the logic itself isn’t hard; however, fully understanding instructions can be tricky. Small mistakes, like missing commas or wrong syntax, take me a lot of time to fix. Also, manually converting ASCII to binary can be confusing, especially when showing messages or feedback. It takes me a while to figure out, for example, when the random number is stored and printed on the screen (which I did on purpose for debugging), the correct message doesn’t appear when the user guesses the right number. Because I didn’t convert binary to ASCII, so the user input is not being processed correctly, causing the program to not display the correct message.After this lab, I learned how important it is to pay attention to small details like syntax and instructions, especially when working with assembly code. I also realized that converting between binary and ASCII correctly is important to make sure the program works and shows the right information.
Code:
; ROM routine entry points
define SCINIT $ff81 ; initialize/clear screen
define CHRIN $ffcf ; input character from keyboard
define CHROUT $ffd2 ; output character to screen
define SCREEN $ffed ; get screen size
define PLOT $fff0 ; get/set cursor coordinates
; Constants
define random_number $0082; Define random variable
define guess_count $0081 ; Guess counter
define value $23 ; Temporary storage for user input
define color $24 ; Color variable for LOOP routine
; Zero-page variables
define PRINT_PTR $00
define PRINT_PTR_H $01
; Start of program
jsr PRINT ; Print title
dcb "N","u","m","b","e","r",32,"G","u","e","s","s","o","r",00
START:
LDA $FE ; Load random value from memory (assuming $FE has some randomness)
AND #$0F ; Mask out upper bits, keeping only values 0-15
CMP #10 ; Ensure it's within 0-9
BCS START ; If 10-15, generate again
STA random_number ; Store valid random number
; Convert binary 0-9 to ASCII ('0' - '9')
LDA random_number
CLC
ADC #$30 ; Convert to ASCII ('0' = $30, ..., '9' = $39)
LDA #$00
STA $40
LDA #$02
STA $41
; Initialize color and guess number
LDA #$02
STA color
LDA #$04
sta guess_count
; Main game loop
GUESSNUMBERS:
jsr PRINT
dcb $0d,"Y","o","u",32,"h","a","v","e",32,"f","o","u","r",32,"c","h","a","n","c","e","s",32,"t","o",32,"g","u","e","s","s",32,"a",32,"n","u","m","b","e","r",$0d
dcb $0d,"G","u","e","s","s",32,"a",32,"n","u","m","b","e","r"
dcb "(","0","-","9",")",":"
dcb 32,32,32,00
GET_NUMBER:
LDA #$00 ; Clear value (user input storage)
STA value
JSR CHRIN ; Get a character from input
CMP #$30 ; Ensure it's '0' or higher
BCC GET_NUMBER; If below '0', ignore
CMP #$3A ; Ensure it's '9' or lower
BCS GET_NUMBER; If above '9', ignore
SEC
SBC #$30 ; Convert ASCII ('0'-'9') to binary (0-9)
STA value ; Store user input as binary
; Convert ASCII digit to binary and store in accumulator
sta value
jsr CHROUT
lda random_number
cmp value
beq CORRECT ; If guess is correct, jump to correct message
bcs TOO_LOW ;
jmp TOO_HIGH;
TOO_LOW:
jsr LOOP
jsr PRINT
dcb $0d,"T","o","o",32,"l","o","w","!",00
dec guess_count
bne ONE_MORE_TRY
jmp END_GAME
TOO_HIGH:
jsr LOOP
jsr PRINT
dcb $0d,"T","o","o",32,"h","i","g","h","!",00
dec guess_count
bne ONE_MORE_TRY
jmp END_GAME
ONE_MORE_TRY:
JSR PRINT
dcb 10,"E","n","t","e","r",32 ,"a","n","o","t","h","e","r",32,"g","u","e","s","s",00
JSR GUESSNUMBERS
CORRECT:
; Print correct guess message
jsr PRINT
dcb $0d,"C","o","r","r","e","c","t","!",00
jsr PRINT
dcb $0d,"Y","o","u",32,"W","o","n",33,00 ; Player wins
jmp END_GAME ; End the game after a correct guess
END_GAME:
jsr LOOP
jsr PRINT
dcb $0d,"G","a","m","e",32,"O","v","e","r",32,"C","o","r","r","e","c","t",32,"N","u","m","b","e","r",":",00
; Convert binary 0-9 to ASCII ('0' - '9')
LDA random_number
CLC
ADC #$30 ; Convert to ASCII ('0' = $30, ..., '9' = $39)
JSR CHROUT ; Print character to screen
RTS; Return from subroutine, ends the program
; Routine to handle the color change on failure
LOOP: LDA color ; Load current color value EOR #$01 ; Toggle the least significant bit to change color STA color LDA color ; Store the new color at the screen address STA ($40),y ; Store color in memory using Y index INY BNE LOOP ; Loop until Y register is zero (end of a row/column) INC $41 ; Increment $41 for the next row if necessary LDX $41 ; Load X register with new row/column index RTS ; Return from subroutine
; Print a message
PRINT: pla
clc
adc #$01
sta PRINT_PTR
pla
sta PRINT_PTR_H
tya
pha
ldy #$00
print_next: lda (PRINT_PTR),y
beq print_done
jsr CHROUT
iny
jmp print_next
print_done: tya
clc
adc PRINT_PTR
sta PRINT_PTR
lda PRINT_PTR_H
adc #$00
sta PRINT_PTR_H
pla
tay
lda PRINT_PTR_H
pha
lda PRINT_PTR
pha
rts
Comments
Post a Comment