SPO600 Lab 3

 

For lab 3, my task is to write a simple program using the output screen and to make use of math operation in 6502 assembly. Even though it is a simple program with basic tasks, it is not that easy to write the program in assembly language, while we can do this easily in modern language. Especially we just learned a whole new concept for several classes. Luckily, the lab says:


I decided to write a subtraction calculator. Inspired by the adding calculator from my professor’s Github:

 (https://github.com/ctyler/6502js-code/blob/master/adding-calculator.6502)

I made an investigation to understand how the code works, so later I can modify it to become a subtraction calculator. With a limited amount of time learning 6502 assembly and not many resources on the internet, I could only understand the main part of the program, but it was enough for me to make an incomplete subtraction calculator.

My input process is identical to the source code. The only difference is instead of adding two numbers together, I do subtract them. However, I am not subtracting the two numbers directly. How I do the subtraction is: first, get two numbers from user inputs, then store them in 2 locations. After calling GETNUM function, a number is stored in the accumulator. I store the accumulator in “value” variable for the first number and “$40” for the second number.

"start: jsr PRINT

dcb $0d,$0d,"E","n","t","e","r",32,"a",32,"n","u","m","b","e","r"

dcb "(","0","-","9","9",")",":"

dcb 32,32,32,32,32,32,32,32,00


lda #$00

sta value_h


jsr GETNUM

sta value


jsr PRINT


dcb "E","n","t","e","r",32,"a","n","o","t","h","e","r"

dcb 32,"n","u","m","b","e","r",32,"(","0","-","9","9",")",":",32,00


jsr GETNUM

sta $40"

 I have tried to subtract them directly by using SBC instruction. However, it did not work, maybe because the user inputs are converted to two-decimal numbers. So, I made a loop to manually decrease the first number until the second number equal to 0.

"loop: dec value

dec $40

ldx $40

cpx #$0

bne loop"

 After finishing the subtraction, the result is in the accumulator. If I store the result to “value” variable and print it to the screen immediately, I get the wrong answer.



 To fix this, I had to do an addition between the result after subtraction and zero.

"sed

clc

lda #0

adc value

cld"

 Finally, I got the right result.



Comments

Popular posts from this blog

SPO600 Lab 2

SPO600 Lab 1

SPO600 Project Stage 3