Hi! I'm continuing my blog about my SPO
classes. After a brief introduction in Assembly, we are good to hit Lab3. Our
instructor kindly let us choose one project out of five. And of course, we
decided to go with the easiest!
We had to do a two-digit numeric display
where the numbers are incremented or decremented by pressing plus and minus key
in the keyboard. Soon the challenges were reviled as we dive into how to code
it. Should we treat every digit separated or together? How to print them into
the display?
After a moment of reflection, we decided to
handle the digits independently to facilitate the printing display. Also, we
had to add a bit-map representation of the numbers because the 6502 chip
doesn’t know any font.
In this post, I’ll show you the code with
the logic to increment and decrement without displaying anything. You can
monitor the address $13 and $14 to make sure that it is working.
Let me explain how it works. When the
program starts, it will set $13 and $14 to zero, and then it will fall into the
infinite loop called “main.” This loop will know if one of the keys was pressed
and then jump to the proper function. Let’s say that you hit the plus key. It
will call the “incr_l” subroutine,
incrementing the low digit and checking if it is more than 9 (#$0A). If
so, it will call the “incr_h” that will increment the high digit and set the
low digit to zero. If the high digit gets more than 9, the counter resets to
zero. The same logic is applied to decrementing, but instead, it limits at zero
and reset it to 99.
In the next post, I’ll add the numeric
display. See you.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; zero-page variable locations | |
define NUM_H $13 | |
define NUM_L $14 | |
lda #$00 | |
reset: | |
sta NUM_H | |
sta NUM_L | |
main: | |
lda $ff ; get a keystroke | |
ldx #$00 ; clear out the key buffer | |
stx $ff | |
cmp #$71 ; handle q -> increment | |
beq incr_l | |
cmp #$77 ; handle w -> decrement | |
beq decr_l | |
jmp main ; if no key pressed loop forever | |
incr_l: | |
ldy NUM_L | |
iny | |
cpy #$0A | |
beq incr_h | |
sty NUM_L | |
jmp main | |
incr_h: | |
ldy NUM_H | |
iny | |
cpy #$0A | |
beq reset_incr | |
sty NUM_H | |
ldy #$00 | |
sty NUM_L | |
jmp main | |
reset_incr: | |
lda #$00 | |
jmp reset | |
decr_l: | |
ldy NUM_L | |
dey | |
cpy #$FF | |
beq decr_h | |
sty NUM_L | |
jmp main | |
decr_h: | |
ldy NUM_H | |
dey | |
cpy #$FF | |
beq reset_decr | |
sty NUM_H | |
ldy #$09 | |
sty NUM_L | |
jmp main | |
reset_decr: | |
lda #$09 | |
jmp reset |
Comments
Post a Comment