DEV Community

A Serputov
A Serputov

Posted on

How To Create Adding Calculator with 6502 Assembler Emulator

Introduction

Today, I'm going to create a tiny calculator with assembly language and 6502 emulator.

The 6502 is an 8-bit processor with a 16-bit address bus. It is, therefore, able to access 64 kilobytes (216 bytes). Since each 16-bit address is comprised of two 8-bit bytes, memory can be viewed as 256 pages of 256 bytes each.

Here you can see my code:

define ENTER  $0d;   for the ENTER key
define BACKSPACE $08; for the BACKSPACE key
define RIGHT  $81;   for the RIGHT Key
define LEFT  $83;    for the LEFT key

define ARG1   $15;   for the first number of user's input 
define ARG2   $16;   for the second number of user's input

define  SCREEN  $ffed; for getting the screen size
define  PLOT    $fff0; for getting or setting the cursor coordinates
define  SCINIT  $ff81; for initializing or clearing the screen
define  CHRIN  $ffcf; for taking the input character from keyboard
define  CHROUT  $ffd2; for displaying the output character to screen

jsr SCINIT

autoRestart: 
     ldy #$00
     jsr firstArgInput ; first number input question
     jsr getArg; receive the first number
     jsr firstArgStore  ; store the input number

     ldy #$00
     jsr secondArgInput  ; second number input question
     jsr getArg; receive the second number
     jsr secondArgStore  ; store the input number

     ldy #$00
     jsr resultStr  ; for displaying the result string
     jsr result ; for displaying the calculation result
     jmp autoRestart  ; go back to the first step


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

numLoop:
     sec
     jsr PLOT
     jsr CHRIN

keyboardCheck: 
     cmp #RIGHT ; using RIGHT arrow key, the program goes to the first digit
     beq right

     cmp #LEFT ; using LEFT arrow key, the program goes to the second digit
     beq left

     cmp #BACKSPACE ; after using BACKSPACE, one digit will be deleted
     beq delete

     cmp #ENTER ; after pressing ENTER, the program goes to the next process
     beq move

print:
     cmp #$30
     bcc numLoop

     clc
     cmp #$3a
     bcs numLoop

     jsr CHROUT

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

left: 
     cpx #$15 ; first digit
     beq numLoop
     jsr CHROUT
     jmp numLoop

right: 
     cpx #$16 ; second digit
     beq numLoop
     jsr CHROUT
     jmp numLoop

delete:
     cpx #$15
     beq numLoop
     jsr CHROUT
     jmp numLoop


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

firstArgStore:
     sta ARG1
     txa
     eor ARG1
     sta ARG1
     rts


secondArgStore:
     sta ARG2
     txa
     eor ARG2
     sta ARG2
     rts


calculateAdd:
     lsr
     lsr
     lsr
     lsr
     clc
     adc #$30;
     jsr CHROUT

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

     sec
     jsr PLOT
     ldx #$00
     iny
     clc
     jsr PLOT

     rts

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

     sed
     lda ARG1
     adc ARG2
     cld
     pha

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

firstArg:
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


secondArg:
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

results:
dcb "R","E","S","U","L","T",":"
dcb 00


firstArgInput:
 lda firstArg,y
        beq goback_main
        jsr CHROUT
        iny
        bne firstArgInput

secondArgInput:
 lda secondArg,y
        beq goback_main
        jsr CHROUT
        iny
        bne secondArgInput

resultStr:
 lda results,y
        beq goback_main
        jsr CHROUT
        iny
        bne resultStr

goback_main:
     rts
Enter fullscreen mode Exit fullscreen mode

Still in progress...

Conclusion

While working on these experiments, I did solve a minor bug in Assembly Compiler. See the Link to the pull request.

⚠️ Open Source For Developers Blog Post: Link

Links

🖇 git https://github.com/aserputov
🖇 twitter https://twitter.com/aserputov

p.s This post was made for my SPO class Lab 3 assignment

Top comments (0)