SPO600 Lab4 String lab Option 1: Adding Calculator
Qzhang125
Posted on November 24, 2021
Hello, welcome to lab 4 for the SPO600(Software Portability and Optimization).This is a lab for the week 4 materials. My team chose to work on option 1 and option 4.
Introduction
This blog is for option 1, we created an adding calculator using 6502 assembly language. I will create a subroutine for the user to insert 2 numbers and add them together.
Requirements
- 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.
- Using this subroutine, write a program which add the two numbers (each of which is in the range 0-99) and print the result.
Coding
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
define RIGHT $81
define LEFT $83
define ENTER $0d
define BACKSPACE $08
define NUM1 $15;
define NUM2 $16;
jsr SCINIT
ldy #$00
jsr firstNumPrint ; ask for input for first number
jsr getNum; get the first number
jsr storeFirstNum ; then store the first number
ldy #$00
jsr secondNumPrint ; ask for input for second number
jsr getNum; get the second number
jsr storeSecondNum ; store the second number
ldy #$00
jsr resultPrintString ; print a string 'Result'
jsr printResult ; print the result
jmp mainLoop ; go back to the first step
getNum:
sec
jsr PLOT
ldx #$15
clc
jsr PLOT
getNumLoop:
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
printNum:
cmp #$30
bcc getNumLoop
clc
cmp #$3a
bcs getNumLoop
jsr CHROUT
sec
jsr PLOT
cpx #$17
bne getNumLoop
dex
clc
jsr PLOT
jmp getNumLoop
move_back:
cpx #$15
beq getNumLoop
jsr CHROUT
jmp getNumLoop
move_left:
cpx #$15 ; first digit
beq getNumLoop
jsr CHROUT
jmp getNumLoop
move_right:
cpx #$16 ; second digit
beq getNumLoop
jsr CHROUT
jmp getNumLoop
move:
sec
jsr PLOT
ldx #$15 ; first digit
clc
jsr PLOT
sec
jsr PLOT
clc
sbc #$2F ; to calculate it, it should be subtracted by #$2f
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
pha
ldx #$00
iny
clc
jsr PLOT
sec
jsr PLOT
pla
tax
pla
rts
storeFirstNum:
sta NUM1
txa
eor NUM1
sta NUM1
rts
storeSecondNum:
sta NUM2
txa
eor NUM2
sta NUM2
rts
printResult:
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
outputAddition:
lsr
lsr
lsr
lsr
clc
adc #$30 ; as the received number does not fit for ASCII, it needs to add
jsr CHROUT
pla
and #$0F
clc
adc #$30 ; as the received number does not fit for ASCII, it needs to add
jsr CHROUT
sec
jsr PLOT
ldx #$00
iny
clc
jsr PLOT
rts
firstNumPrint:
lda firstNum,y
beq goback_main
jsr CHROUT
iny
bne firstNumPrint
secondNumPrint:
lda secondNum,y
beq goback_main
jsr CHROUT
iny
bne secondNumPrint
resultPrintString:
lda result,y
beq goback_main
jsr CHROUT
iny
bne resultPrintString
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
Thoughts & reflection
The assembly language program is getting harder than before, even harder than the third lab. In this lab, I spent a lot of time on how to get the user input, store the two numbers and the results, and display the results on the screen. As I said in each blog about the 6502 assembly language, It is a very low level programming language and it frequently accesses the memory and registers. The operation that we designed for this program is quite easy in any higher level language. But in assembler language, I'm still not familiar with the syntax. I hope more and more practices would help me to solve the problem easily.
Posted on November 24, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 30, 2024
November 30, 2024