Lab 03 - Addition and Subtraction Calculator

 Addition and Subtraction Calculator


This blog post features an Assembly Language code for creating an addition and subtraction calculator. This code is not fully functional and is still a work in progress and can be improved over time.

-------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------

Down below are the defined values that are used to ensure addition or subtraction operations work:

define NUM1                        $0200   - first number storage
define NUM2                        $0201   - second number storage
define OPERATION             $0202   - operation such as +/-
define RESULT                     $0203  - result based on operation chosen
define TEXT_SCREEN        $0400  - screen memory
define SCREEN_PTR_LO    $00     - for screen pointer high byte
define SCREEN_PTR_HI     $01     - for screen pointer low byte
define KEYBOARD              $FF    - keyboard memory location
define ASCII_ENTER           #13    - ASCII enter key

START:

This following is to initialize the screen pointer

    LDA #<TEXT_SCREEN - loads $0400
    STA SCREEN_PTR_LO  - stores it into $00
    LDA #>TEXT_SCREEN - loads $0400
    STA SCREEN_PTR_HI.  - stores it into $01 

Reads first number

    JSR GET_NUMBER
    STA NUM1

Reads operation

    JSR GET_OPERATION
    STA OPERATION

Reads second number

    JSR GET_NUMBER
    STA NUM2

Performs the calculation

    LDA OPERATION
    CMP #$2B   - checks if it's #$2B, if yes, choose addition
    BEQ ADDITION
    CMP #$2D   - checks if it's #$2D, if yes, choose subtraction
    BEQ SUBTRACTION
    JMP ERROR - loads error if neither of the comparison

Addition logic

ADDITION:
    LDA NUM1 - loads number
    CLC - clears
    ADC NUM2 - adds number 1 and number 2 using ADC
    STA RESULT - loads result
    JMP DISPLAY_RESULT - loads display_result

Subtraction logic

SUBTRACTION:
    LDA NUM1 - loads number 
    SEC - sets carry 
    SBC NUM2 - subtracts using SBC
    STA RESULT - loads result
    JMP DISPLAY_RESULT - displays result

Displays result to screen

DISPLAY_RESULT:
    LDA RESULT
    STA TEXT_SCREEN
    JMP START

Displays "E" on screen if input is invalid

ERROR:
    LDA #$45  - loads "e"
    STA TEXT_SCREEN
    JMP START

Reads full number input and not just per digit entered, for example "23" instead of 2 and 3

GET_NUMBER:
    JSR GET_KEY
    CMP ASCII_ENTER  
    BEQ DONE_INPUT
    STA RESULT
    RTS

DONE_INPUT:
    LDA RESULT
    RTS

Get operation from keyboard, waits for user to enter - or +

GET_OPERATION:
    JSR GET_KEY
    RTS

This loads input from keyboard

GET_KEY:
    LDA KEYBOARD   
    CMP #$00
    BEQ GET_KEY
    RTS

------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------

Reflection:


Despite the difficulty of this lab I was able to understand how Assembly works. This code is not functional in a way that the number on the screen does not reflect what the user inputs, instead, only shows pixel colours. I am still trying to explore more and find a better solution for this to work.

Comments

Popular posts from this blog

BATCH 3 | Project Stage 2, Part 3 - Cloned Functions Comparison and Reflection

BATCH 4 | Clone-Prune Analysis Code Pass On Both Architectures

BATCH 3 | Project Stage 2, Part 1 - Clone-Pruning Analysis Pass