Lab 3 - Follow up

Samarth SharmaSamarth Sharma
3 min read

Introduction

In this project, I modified a 6502 Assembly program originally designed for addition and turned it into a subtraction calculator. I used and adapted code from Krins Kumar’s blog post. This post satisfies that requirement by clearly labeling borrowed, modified, and new code segments.


Source and Permission

  • Original Code:SPO Week 3.1 ‐ Full Lab” by Krins Kumar.

  • Permission: Granted by the author, with freedom to skip attribution—but I’m including it as per my lab requirements.


Borrowed vs. Modified vs. New Code

In the annotated code below, I use the following markers:

  • [BORROWED]: Lines copied verbatim or nearly so from Krins Kumar’s code.

  • [MODIFIED]: Lines originally from Krins’s code but significantly changed (renamed labels, reorganized arithmetic).

  • [NEW]: Entirely new lines or sections I wrote.

All unlabeled lines are effectively the same classification as the preceding comment block. For brevity, I’m showing only an excerpt below; my complete code is posted in my repository.

assemblyCopyEdit; =================== ROM Routine Definitions ===================
; Original: define      SCINIT      $ff81 ; initialize/clear screen
;define      SCINIT      $ff81 ; [BORROWED - commented out original]
define      CLEAR_SCREEN $ff81   ; (changed name) [MODIFIED]

; Original: define      CHRIN       $ffcf ; input character from keyboard
;define      CHRIN       $ffcf ; [BORROWED - commented out original]
define      GET_CHAR    $ffcf   ; (changed name) [MODIFIED]

; Original: define      CHROUT      $ffd2 ; output character to screen
;define      CHROUT      $ffd2 ; [BORROWED - commented out original]
define      PUT_CHAR    $ffd2   ; (changed name) [MODIFIED]

; =================== Zero‑Page Variable Definitions ===================
; Original: define      PRINT_PTR   $10
;define      PRINT_PTR   $10 ; [BORROWED]
define      PRNT_PTR    $10   ; (renamed) [MODIFIED]

; Original: define      value       $14
;define      value       $14  ; [BORROWED]
define      SUM_BCD     $14   ; now holds the minuend/result (changed function) [MODIFIED]

; [NEW] define      SUB_NUM     $1E     ; new variable for subtrahend
; [NEW] define      TEMP_BCD    $1F     ; temporary storage for swapping

; =================== Main Routine: SUBTRACTNUMBERS ===================
; (changed from ADDNUMBERS)
SUBTRACTNUMBERS:
    ; [NEW] Prompt for first number
    JSR PRINT_STR
    dcb $0D,$0D,"Enter first number:",00

    LDA #$00
    ; [BORROWED] 'STA value_h' turned into:
    STA SUM_H       ; [MODIFIED]
    JSR GET_NUMBER  ; [MODIFIED, originally GETNUM]
    STA SUM_BCD     ; [MODIFIED, originally 'sta value']

    ; [NEW] Prompt for second number
    JSR PRINT_STR
    dcb "Enter second number:",00
    JSR GET_NUMBER
    STA SUB_NUM

    ; [NEW] Compare & swap if the first < second
    LDA SUM_BCD
    CMP SUB_NUM
    BCS DO_SUB
    LDA #$2D      ; ASCII '-'
    JSR PUT_CHAR
    LDA SUM_BCD
    STA TEMP_BCD
    LDA SUB_NUM
    STA SUM_BCD
    LDA TEMP_BCD
    STA SUB_NUM

DO_SUB:
    SED
    CLC
    SBC SUB_NUM
    CLD
    STA SUM_BCD

    ; [MODIFIED] Display result
    PHA
    JSR PRINT_STR
    dcb "Result:",32,32,32,32,32,00

    LDA SUM_H
    BEQ PROC_TENS
    AND #$0F
    ORA #$30
    JSR PUT_CHAR
    JSR UPDATE_GFX
    LDA #$10
    LDX CUR_X
    LDY CUR_Y
    JSR DRAW_IMAGE
    LDA CUR_X
    CLC
    ADC #$06
    STA CUR_X
    JMP PROC_TENS

    ; etc. ...
    RTS

(The full code includes the DRAW_IMAGE, PRINT_STR, GET_NUMBER, UPDATE_GFX subroutines, along with the pixel data for digits. I have placed [BORROWED] or [MODIFIED] markers on relevant lines, and [NEW] markers where I added lines that did not exist previously in Krins’s code.)


Key Changes and Additions

  1. Arithmetic: Changed from ADC (addition) to SBC (subtraction) in BCD mode, with logic to handle negative results by swapping numbers.

  2. Variable Renaming: e.g., valueSUM_BCD, XPOSCUR_X, etc.

  3. New Variables: SUB_NUM for the subtrahend, TEMP_BCD for swapping.

  4. Updated Prompts: New strings asking the user for first and second numbers.

  5. Minus Sign Output: If first < second, print “–” and swap.


Conclusion

This project involved modifying a 6502 assembly program to implement subtraction instead of addition while maintaining proper BCD arithmetic. The code structure was improved for readability, and key functions were optimized.

🔗 GitHub Repositoryhttps://github.com/samartho4/SPO-600/blob/main/Lab%203/calculator

0
Subscribe to my newsletter

Read articles from Samarth Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Samarth Sharma
Samarth Sharma

Looping around thinking to write it down...