Lab 4 - SPO600 - Part 2
Tags: beginners
Published: 2021-11-14 16:46:16

## Introduction
Hello everyone, my name is Dustin. Today, I'd like to talk about my experience working with another assignment in Assembly language in SPO class.

For this lab, we got to choose 1 out of 3 options that my professor gave to me which are Data Input Form, Hexdump, Screen Colour Selector. I found Data Input Form is another option of my interest I guess. 

## Requirement
- Create a subroutine which enables the user to enter a string of up to 40 characters. Decide how the data will be returned from the subroutine. The user should be able to use the left/right cursor keys and backspace to edit the text, and the cursor location should be visible to the user.
- Write a program which uses this subroutine to collect the user's name, address, city, province, and postal code, and then display this information formatted as a shipping address (for example, as shown below).

For this lab, it's almost the same as the part 1 that I did, but way more difficult as it has more requirements. For the previous one, I also have to input 2 numbers but this one, I have to handle the whole string 40 characters long. Apart from that, user's also be able to left or right cursor keys or backspace to edit the string they've entered which is AMAZING. 

## Wrap up
I was stuck many times and truth is Assembly Language is not my thing, the trouble was real. But with the help of my friends, we are able to solve it and get it up and running. Thank you for reading my blog, and below is the code  

## Code

Rom routines
```
define SCINIT  $ff81 ; initialize/clear screen
define CHROUT  $ffd2 ; output character to screen
```

Declare var
```
define COLOUR  $10
define COLOUR_INDEX $11
define POINTER  $40
define POINTER_H $41
define UP_KEY  $80
define DOWN_KEY $82
```

init
```
 lda #$00 
 sta COLOUR
 sta COLOUR_INDEX 

 jsr initializePrint
```

up and down arrow keys
```
getKey:
 lda $ff
 sty $ff

 cmp #UP_KEY
 beq decrementKey

 cmp #DOWN_KEY 
 beq incrementKey

 jmp getKey

decrementKey:
 lda COLOUR
 cmp #$01
 bpl decrementColour

 jmp getKey

decrementColour:
 dec COLOUR

 jsr initializePrint
 jsr initializePaint
 jmp getKey

incrementKey:
 lda COLOUR
 cmp #$0f
 bmi incrementColour

 jmp getKey

incrementColour:
 inc COLOUR
 jsr initializePrint
 jsr initializePaint
 jmp getKey

```

Print screen
```
initializePrint:
 jsr SCINIT
        ldy #$00

writeTitle:
 lda title,y
        beq titleDone
        jsr CHROUT
        iny
        bne writeTitle

titleDone:
 lda #$00
 sta COLOUR_INDEX

startColour:
 ora #$00
 ldy #$00

colourName:
 jsr selectedColour
 beq afterWriting
 jsr highlightLine

 jsr CHROUT

 iny
 bne colourName

afterWriting:
 inc COLOUR_INDEX
 lda COLOUR_INDEX
 cmp #$10
 bne startColour

selectedColour:
 lda COLOUR_INDEX

 cmp #$00
 beq printColour0

 cmp #$01
 beq printColour1

 cmp #$02
 beq printColour2

 cmp #$03
 beq printColour3

 cmp #$04
 beq printColour4

 cmp #$05
 beq printColour5

 cmp #$06
 beq printColour6

 cmp #$07
 beq printColour7

 cmp #$08
 beq printColour8

 cmp #$09
 beq printColour9

 cmp #$0a
 beq printColour10

 cmp #$0b
 beq printColour11

 cmp #$0c
 beq printColour12

 cmp #$0d
 beq printColour13

 cmp #$0e
 beq printColour14

 cmp #$0f
 beq printColour15

 rts

printColour0:
 lda colour0,y
 rts

printColour1:
 lda colour1,y
 rts

printColour2:
 lda colour2,y
 rts

printColour3:
 lda colour3,y
 rts

printColour4:
 lda colour4,y
 rts

printColour5:
 lda colour5,y
 rts

printColour6:
 lda colour6,y
 rts

printColour7:
 lda colour7,y
 rts

printColour8:
 lda colour8,y
 rts

printColour9:
 lda colour9,y
 rts

printColour10:
 lda colour10,y
 rts

printColour11:
 lda colour11,y
 rts

printColour12:
 lda colour12,y
 rts

printColour13:
 lda colour13,y
 rts

printColour14:
 lda colour14,y
 rts

printColour15: 
 lda colour15,y
 rts

highlightLine:
 ldx COLOUR_INDEX
 cpx COLOUR
 beq highlight

 ora #$00
 rts

highlight:
 ora #$80
 rts

```

Change color of the bitmap
```
initializePaint:
 lda #$00         ; set a pointer at $40 to point to $0200
        sta POINTER
        lda #$02
        sta POINTER_H

 ldy #$00

 lda COLOUR

draw_screen:
  sta ($40), y     ; set pixel

        iny              ; increment index
        bne draw_screen  ; continue until done the page

        inc $41          ; increment the page
        ldx $41          ; get the page
        cpx #$06         ; compare with 6
        bne draw_screen  ; continue until done all pages

 rts

```

List of colors on the screen
```
title:
dcb "L","i","s","t",32,"o","f",32,"C","o","l","o","u","r","s",":",13
dcb 00

colour0:
dcb "B","l","a","c","k",13
dcb 00

colour1:
dcb "W","h","i","t","e",13
dcb 00

colour2:
dcb "R","e","d",13
dcb 00

colour3:
dcb "C","y","a","n",13
dcb 00

colour4:
dcb "P","u","r","p","l","e",13
dcb 00

colour5:
dcb "G","r","e","e","n",13
dcb 00

colour6:
dcb "B","l","u","e",13
dcb 00

colour7:
dcb "Y","e","l","l","o","w",13
dcb 00

colour8:
dcb "O","r","a","n","g","e",13
dcb 00

colour9:
dcb "B","r","o","w","n",13
dcb 00

colour10:
dcb "L","i","g","h","t",32,"r","e","d",13
dcb 00

colour11:
dcb "D","a","r","k",32,"g","r","e","y",13
dcb 00

colour12:
dcb "G","r","e","y",13
dcb 00

colour13:
dcb "L","i","g","h","t",32,"g","r","e","e","n",13
dcb 00

colour14:
dcb "L","i","g","h","t",32,"b","l","u","e",13
dcb 00

colour15:
dcb "L","i","g","h","t",32,"g","r","e","y",13
dcb 00
```