Monday, April 6, 2020

Lab 4 - part2: Adding Calculator


This is the second part of Lab4.
Our team chooses the option 1 as the second part.

Option 1: Adding Calculator

  1. Create a subroutine which enables the user to enter two numbers of up to two digits. Indicate where the cursor is, and allow the user to use the digit keys (0-9), backspace, and enter keys. Return the user's input value in the accumulator (A) register.
  2. Using this subroutine, write a program which add the two numbers (each of which is in the range 0-99) and print the result.
  • Optional challenge: extend to more than 2 digits and add a running total capability with a clear key.

1. Whole code
; ======== variables for screen input/output ========
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

; ======== variables for key ========
define RIGHT $81
define LEFT $83
define ENTER $0d
define BACKSPACE $08

; ======== variables for Numbers that user input ========
define NUM1    $15;
define  NUM2    $16;

        jsr SCINIT

; ======= main Loop: print first num input question -> receive first Number
;                   -> print second num question -> receive second Number -> print result
;                   -> go back to the first step
mainLoop:
     ldy #$00
     jsr firstNumPrint ; first number input question
     jsr receiveNum; receive first number
     jsr firstNum_store  ; store the received number
     ldy #$00
     jsr secNumPrint  ; second number input question
     jsr receiveNum; receive second number
     jsr secNum_store  ; store the received number
     ldy #$00
     jsr resultPrint ; print a string 'Result'
     jsr result_print ; print the result
     jmp mainLoop ; go back to the first step


receiveNum:
     sec
     jsr PLOT
     ldx #$15
     clc
     jsr PLOT

receiveNum_loop:
     sec
     jsr PLOT
     jsr CHRIN


charCheck: 
     cmp #BACKSPACE ; if user enter backspace, it erase the #$15 digit
beq move_back

     cmp #RIGHT ; if user enter right arrow, it goes to the first digit
     beq move_right

     cmp #LEFT ; if user enter left arrow, it goes to the second digit
     beq move_left

     cmp #ENTER ; if user enter enter, it goes to the next process
     beq move

drawNum:
; ========= check whether user enters number (#$30-#$39) ==========
     cmp #$30
     bcc receiveNum_loop

     clc
     cmp #$3a
     bcs receiveNum_loop

     jsr CHROUT

     sec
     jsr PLOT
     cpx #$17
     bne receiveNum_loop
     dex
     clc
     jsr PLOT
     jmp receiveNum_loop

move_back:
cpx #$15
beq receiveNum_loop
jsr CHROUT
jmp receiveNum_loop

move_left: 
cpx #$15 ; first digit
     beq receiveNum_loop
     jsr CHROUT
     jmp receiveNum_loop

move_right: 
cpx #$16 ; second digit
     beq receiveNum_loop
     jsr CHROUT
     jmp receiveNum_loop

move:
     sec
     jsr PLOT
     ldx #$15 ; first degit
     clc
     jsr PLOT
     sec
     jsr PLOT

     clc
     sbc #$2F ; to calculate it, it should be subtracted by #$2f (hexadecimal for number 0: #$30)

     asl
     asl
    asl
     asl

     pha

     ldx #$16
     clc
     jsr PLOT
     sec
     jsr PLOT

     clc
     sbc #$2F ; to calculate it, it should be subtracted by #$2f (hexadecimal for number 0: #$30)
     pha

     ldx #$00
     iny
     clc
     jsr PLOT
     sec
     jsr PLOT

     pla
     tax
     pla

     rts

; ======== store the first number =========
firstNum_store:
     sta NUM1
     txa
     eor NUM1
     sta NUM1
     rts

; ======== store the second number =========
secNum_store:
     sta NUM2
     txa
     eor NUM2
     sta NUM2
     rts

; ========= print result ========
result_print:
     sec
     jsr PLOT
     ldx #$15
     clc
     jsr PLOT
     sec
     jsr PLOT

     sed
     lda NUM1
     adc NUM2
     cld
     pha

     bcc outputAddition
     ldx #$14
     clc
     jsr PLOT
     sec
     jsr PLOT
     lda #$31
     jsr CHROUT

; ========= calculate for result number =========
outputAddition:
     lsr
     lsr
     lsr
     lsr
     clc
     adc #$30 ; as the received number does not fit for ASCII, it needs to add #$30
     jsr CHROUT

     pla
     and #$0F
     clc
     adc #$30 ; as the received number does not fit for ASCII, it needs to add #$30
     jsr CHROUT

     sec
     jsr PLOT
     ldx #$00
     iny
     clc
     jsr PLOT

     rts

; ======== print 'ENTER FIRST NUMBER: 00 =======
firstNumPrint:
lda firstNum,y
        beq goback_main
        jsr CHROUT
        iny
        bne firstNumPrint

; ======== print "ENTER SECOND NUMBER: 00" ==========
secNumPrint:
lda secondNum,y
        beq goback_main
        jsr CHROUT
        iny
        bne secNumPrint

; ======== print "RESULT: " ==============
resultPrint:
lda result,y
        beq goback_main
        jsr CHROUT
        iny
        bne resultPrint

; ========= goes back to the main loop ========
goback_main:
     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


This is our team's result. When we did this we had a lot of struggle time. I still did not exactly understand.

No comments:

Post a Comment