Hi, I'm pretty new to 6502 assembly and I need some help.
Basically, I'm creating a game with a 5-digit score. The game keeps track of it using 3 bytes of RAM in BCD format. That works fine, but when the game is over I want to compare this score to a high score elsewhere in RAM.
The code I've written starts by comparing the most significant bytes. If the score's MSB < the high score's MSB, it exits the subroutine. Otherwise it moves on, comparing the second and least significant bytes the same way. If it never exited the routine, it'll overwrite the high score with the current score. For some reason it doesn't work, and I can't figure out why.
The first problem is, it doesn't seem to check the MSB at all. For example, I tried getting a score of 10050, with a high score of 1200. It thought 1200 was larger!
If both digits are 9999 or less, it seems to work 90% of the time, but occasionally it'll just fail for no discernable reason at all. I feel like the answer is obvious, but I don't know what I'm overlooking.
Below is the code I'm using. SCORE+2/HIGHSCORE+2 is the MSB, while SCORE/HIGHSCORE is the LSB.
checkhs lda SCORE+2 ; compare most significant bytes
cmp HIGHSCORE+2
bcc ret ; if score's MSB < high score's MSB, exit the subroutine
lda SCORE+1 ; compare middle bytes
cmp HIGHSCORE+1
bcc ret ; if score's middle byte < high score's middle byte, exit the subroutine
lda SCORE ; compare least significant bytes
cmp HIGHSCORE
bcc ret ; if score's LSB < high score's LSB, exit the subroutine
lda SCORE ; if all checks passed, set new high score and exit
sta HIGHSCORE
lda SCORE+1
sta HIGHSCORE+1
lda SCORE+2
sta HIGHSCORE+2
ret rts
Thanks!