An Interest In:
Web News this Week
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
- April 19, 2024
- April 18, 2024
- April 17, 2024
SPO600 Lab4 String lab Option 1: Adding Calculator
Hello, welcome to lab 4 for the SPO600. 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 screendefine CHRIN $ffcf ; input character from keyboarddefine CHROUT $ffd2 ; output character to screendefine SCREEN $ffed ; get screen sizedefine PLOT $fff0 ; get/set cursor coordinatesdefine RIGHT $81define LEFT $83define ENTER $0ddefine BACKSPACE $08define NUM1 $15;define NUM2 $16;jsr SCINITldy #$00jsr firstNumPrint ; ask for input for first numberjsr getNum; get the first numberjsr storeFirstNum ; then store the first numberldy #$00jsr secondNumPrint ; ask for input for second numberjsr getNum; get the second numberjsr storeSecondNum ; store the second numberldy #$00jsr resultPrintString ; print a string 'Result'jsr printResult ; print the resultjmp mainLoop ; go back to the first stepgetNum: sec jsr PLOT ldx #$15 clc jsr PLOTgetNumLoop: sec jsr PLOT jsr CHRINcharCheck: 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 moveprintNum: cmp #$30 bcc getNumLoop clc cmp #$3a bcs getNumLoop jsr CHROUT sec jsr PLOT cpx #$17 bne getNumLoop dex clc jsr PLOT jmp getNumLoopmove_back: cpx #$15 beq getNumLoop jsr CHROUT jmp getNumLoopmove_left: cpx #$15 ; first digit beq getNumLoop jsr CHROUT jmp getNumLoopmove_right: cpx #$16 ; second digit beq getNumLoop jsr CHROUT jmp getNumLoopmove: 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 rtsstoreFirstNum: sta NUM1 txa eor NUM1 sta NUM1 rtsstoreSecondNum: sta NUM2 txa eor NUM2 sta NUM2 rtsprintResult: 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 CHROUToutputAddition: 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 rtsfirstNumPrint: lda firstNum,y beq goback_main jsr CHROUT iny bne firstNumPrintsecondNumPrint: lda secondNum,y beq goback_main jsr CHROUT iny bne secondNumPrintresultPrintString: lda result,y beq goback_main jsr CHROUT iny bne resultPrintStringgoback_main: rtsfirstNum:dcb "E","N","T","E","R",32,"F","I","R","S","T",32,"N","U","M","B","E","R",":",32,32,"0","0"dcb 00secondNum:dcb "E","N","T","E","R",32,"S","E","C","O","N","D",32,"N","U","M","B","E","R",":",32,"0","0"dcb 00result: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.
Original Link: https://dev.to/qzhang125/spo600-lab4-6502-string-lab-option-1-adding-calculator-17m8
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To