Lab 3 - Understanding arithmetic/math and strings in 6502 assembly language(SPO600)

Introduction

As a beginner delving into the world of assembly language programming, I recently had the opportunity to explore the realms of the 6502 Jumps, Branches, and Procedural Math (including Bitwise Operations). In Lab 3, I got the learning opportunity to work with 6502 Math and String in order to develop and understand how we can utilize user interaction to receive our desired output. My quest led me to a captivating code adventure - a color selector with live updates. This blog chronicles my journey, highlighting the code's working functionality, critical elements, and my reflections on the experience.


The Actual Code Block

The code begins with a warm welcome, invoking the SCINIT routine to initialize and clear the screen, setting the stage for the colorful escapade. A delightful list of color names is then printed, each ready to be chosen by the user.

 

; Colour selector - live updates


; ROM routine entry points

define                   SCINIT                  $ff81 ; initialize/clear screen

define                   CHRIN                  $ffcf ; input character from keyboard

define                   CHROUT              $ffd2 ; output character to screen

define                   SCREEN                $ffed ; get screen size

define                   PLOT                     $fff0 ; get/set cursor coordinates

 

; zeropage variables

define                   PRINT_PTR          $00

define                   PRINT_PTR_H     $01

define                   CURRENT            $02

define                   SCRN_PTR           $03

define                   SCRN_PTR_H      $04

 

; constants

; --------------------------------------------------------

                              jsr SCINIT

 

                              jsr PRINT

dcb "B","l","a","c","k",$0d

dcb "W","h","i","t","e",$0d

dcb "R","e","d",$0d

dcb "C","y","a","n",$0d

dcb "P","u","r","p","l","e",$0d

dcb "G","r","e","e","n",$0d

dcb "B","l","u","e",$0d

dcb "Y","e","l","l","o","w",$0d

dcb "O","r","a","n","g","e",$0d

dcb "B","r","o","w","n",$0d

dcb "L","i","g","h","t",32,"r","e","d",$0d

dcb "D","a","r","k",32,"g","r","e","y",$0d

dcb "G","r","e","y",$0d

dcb "L","i","g","h","t",32,"g","r","e","e","n",$0d

dcb "L","i","g","h","t",32,"b","l","u","e",$0d

dcb "L","i","g","h","t",32,"g","r","e","y",$0d

dcb $0d

dcb "C","h","o","o","s","e",32,"a",32,"c","o","l","o","u","r",32

dcb "a","b","o","v","e",32,"t","o",32,"s","e","e",32,"i","t",32

dcb "o","n",32,"t","h","e",32,"b","i","t","m","a","p","p","e","d",32

dcb "d","i","s","p","l","a","y",".",$0d

dcb 00

                              lda #$00

 

get_colour:          jsr SELECT

 

                              ldy #$00

                              sty SCRN_PTR

                              ldx #$02

                              stx SCRN_PTR_H

                              ldx #$04               ; number of pages to fill

 

draw:                    sta (SCRN_PTR),y

                              iny

                              bne draw

 

                              inc SCRN_PTR_H

                              dex

                              bne draw

 

                              jmp get_colour

 

; -----------------------------------------------------------

; SELECT :: select one line from the screen

 

SELECT:                sta CURRENT

 

show_and_go:    jsr HIGHLIGHT

 

getkey:                 jsr CHRIN

                              cmp #$80            ; cursor up

                              bne try_down

 

                              lda CURRENT

                              beq getkey

                              jsr HIGHLIGHT

                              dec CURRENT

                              jmp return

 

try_down:            cmp #$82            ; cursor down

                              bne getkey

 

                              lda CURRENT

                              cmp #$0f

                              beq getkey

                              jsr HIGHLIGHT   

                              inc CURRENT

 

return:                  lda CURRENT

                              rts

; --------------------------------------------------------

; Highlight :: highlight the CURRENT line on the display

 

HIGHLIGHT:         ldy CURRENT

                              ldx #$00

                              clc

                              jsr PLOT

 

highlight_next:   sec

                              jsr PLOT

                              eor #$80

                              jsr CHROUT

 

                              inx

                              cpx #20

                              bne highlight_next

                              rts

 

; --------------------------------------------------------

; Print a message

; Prints the message in memory immediately after the

; JSR PRINT. The message must be null-terminated and

; 255 characters maximum in length.

 

PRINT:                  pla

                              clc

                              adc #$01

                              sta PRINT_PTR

                              pla

                              sta PRINT_PTR_H

 

                              tya

                              pha

 

                              ldy #$00

print_next:          lda (PRINT_PTR),y

                              beq print_done

                             

                              jsr CHROUT

                              iny

                              jmp print_next

 

print_done:         tya

                              clc

                              adc PRINT_PTR

                              sta PRINT_PTR

 

                              lda PRINT_PTR_H

                              adc #$00

                              sta PRINT_PTR_H

 

                              pla

                              tay

 

                              lda PRINT_PTR_H

                              pha

                              lda PRINT_PTR

                              pha

 

                              rts

 

 

Code block understanding via some Critical Elements in the Code:

 

1.      Seamless working of SELECT routine

The heart of the program lies in its well-written SELECT routine, allowing users to navigate through the spectrum of colors. The arrow keys serve like a magic wand, guiding the cursor up and down the list. As each color is selected, the display generously updates in real time, offering a visual feast of the chosen hues and thereafter seamlessly returning control to the main loop..

 

; ...

SELECT: sta CURRENT

show_and_go:    jsr HIGHLIGHT

getkey:  jsr CHRIN

               cmp #$80            ; cursor up

               bne try_down

 

               lda CURRENT

               beq getkey

               jsr HIGHLIGHT

               dec CURRENT

               jmp return

 

try_down:            cmp #$82            ; cursor down

               bne getkey

 

               lda CURRENT

               cmp #$0f

               beq getkey

               jsr HIGHLIGHT   

               inc CURRENT

 

return:                  lda CURRENT

               rts

; ...

 

2. Pointer Play

The code elegantly employs zeropage variables, such as PRINT_PTR and SCRN_PTR, to manipulate memory efficiently. The use of pointers in assembly language can be perplexing to understand for beginners, but here it becomes a dance of bytes, managing the display and message printing with finesse.

 

lda (PRINT_PTR),y

sta (SCRN_PTR),y

 

A Reflection from a Beginner's Perspective

As a novice adventurer in assembly language, the journey was both challenging and rewarding. Understanding the critical concepts of memory manipulation and subroutine logic proved to be challenging and time-consuming. However, with each challenge, a sense of accomplishment emerged, fueled by the curiosity of how we can witness the display transform with each color selection.

 

Ideas for Further Enhancement

The code, like any program, leaves room for expansion and enhancement. Here are a few ideas to take this color selector to new heights:

  1. Dynamic Color Palette: Allows users to define custom color palettes, opening up endless possibilities.
  2. User-Defined Shortcuts: Implement shortcuts for quicker navigation, providing a more user-friendly experience.
  3. Graphics Integration: Elevate the visual experience by incorporating simple graphics or patterns associated with each color.
  4. Color Information Display: Provide additional information about each color, such as RGB values or hex codes, enhancing the educational aspect of the program.

 

Conclusion

Embarking on this assembly language adventure has been a journey through the vibrant landscapes of user interaction. The color selector, with its live updates and interactive features, stands as a testament to the tasks that can be achieved with just a few lines of code. Before I bid farewell to this project, here is the reference of my code in case you want to delve deeper.

I eagerly anticipate the next adventure(Lab), armed with newfound knowledge.

 


Comments

Popular posts from this blog

Navigating the Patch Submission Processes in Open Source Communities: Linux Kernel & Python

Building GCC from Source: A Journey into Compiler Construction (Project Stage 1)