SPO600 - Lab 4
Published: 2021-12-03 07:17:00

After countless hours I've finally came up with a somewhat reliable conclusion for the 4th lab. This lab requires you to create a subroutine which enables you to enter 2 digits, and adding the numbers together and displaying them.

It should also allow the use of digits, backspace, and moving left and right between the two digits so you could edit as you wish.

```
define  CHRIN  $ffcf ; input character from keyboard
define  CHROUT  $ffd2 ; output character to screen
define  PLOT    $fff0 ; get/set cursor coordinates

define BACKSPACE $08
define RIGHT  $81
define LEFT  $83
define ENTER  $0d

define NUM1 $15;
define NUM2 $16;
```
```
main:
     ldy #$00
     jsr getFirstNum ; get and store first number
     jsr getNum
     jsr storeFirstNum

     ldy #$00
     jsr getSecondNum
     jsr getNum ; get and store second number
     jsr storeSecondNum

     ldy #$00
     jsr getResult
     jsr printResult
     jmp main
```
```
getNum:
     jsr PLOT
     ldx #$15
     jsr PLOT
```
```
getNumCon:
     sec
     jsr PLOT
     jsr CHRIN
```
```
keyPress: 
     cmp #BACKSPACE 
     beq erase ; erase the first digit

     cmp #RIGHT 
     beq left ; move to left digit

     cmp #LEFT
     beq right ; move to right digit

     cmp #ENTER 
     beq continue ; continue 
```
```
dispNum:
     cmp #$30
     bcc getNumCon

     clc

     cmp #$3a
     bcs getNumCon

     jsr CHROUT
     jsr PLOT

     cpx #$17
     bne getNumCon

     dex
     clc

     jsr PLOT
     jmp getNumCon
```
```
erase:
     cpx #$15
     beq getNumCon ; erase
     jsr CHROUT
     jmp getNumCon
```
```
left: 
     cpx #$15
     beq getNumCon ; move left
     jsr CHROUT
     jmp getNumCon
```
```
right: 
     cpx #$16
     beq getNumCon ; move right
     jsr CHROUT
     jmp getNumCon
```
```
continue:
     jsr PLOT
     ldx #$15

     jsr PLOT
     sec
     jsr PLOT

     sbc #$2F

     asl
     asl
     asl
     asl

     pha

     ldx #$16
 
     jsr PLOT
     sec
     jsr PLOT

     sbc #$2F
     pha

     ldx #$00
     iny
     clc

     jsr PLOT
     sec
     jsr PLOT

     pla
     tax
     pla

     rts
```
```
printResult:
     jsr PLOT
     ldx #$15
     jsr PLOT
     sec
     jsr PLOT

     lda NUM1
     adc NUM2

     pha

     bcc addition
```
```
addition:
     lsr
     lsr
     lsr
     lsr

     adc #$30
     jsr CHROUT

     pla

     and #$0F
     clc
     adc #$30
     jsr CHROUT

     jsr PLOT
     ldx #$00
     iny
     jsr PLOT

     rts
```
```
getFirstNum:
     lda firstNum,y
     beq return
     jsr CHROUT
     iny
     bne getFirstNum
```
```
getSecondNum:
     lda secondNum,y
     beq return
     jsr CHROUT
     iny
     bne getSecondNum
```
```
getResult:
     lda result,y
     beq return
     jsr CHROUT
     iny
     bne getResult
```
```
storeFirstNum:
     sta NUM1
     txa
     eor NUM1
     sta NUM1
     rts
```
```
storeSecondNum:
     sta NUM2
     txa
     eor NUM2
     sta NUM2
     rts
```
```
return:
     rts
```
```
firstNum:
dcb "E","N","T","E","R",32,"F","I","R","S","T",32,"N","U","M","B","E","R",":",32,32,"0","0"
dcb 00

secondNum:
dcb "E","N","T","E","R",32,"S","E","C","O","N","D",32,"N","U","M","B","E","R",":",32,"0","0"
dcb 00

result:
dcb "R","E","S","U","L","T",":"
dcb 00
```

Overall, this lab was extremely hard. I had to skip over the third lab because I wasn't quite sure how to do it. Apparently I was supposed to pick two topics for this lab but I'm not quite ready for the second one yet. It was however really informative and taught me a good bit about 6502. I learned how to get user input, display it, modify it, and more.