Wednesday, December 16, 2015

Day 7 - Mix and Match

Today's exercise reads (taken from Assembly Language Programming)

Write a program that: Loads a diamond into the accumulator, an asterisk into X and an "E" into Y. Then, without using any further Immediate Mode commands, moves the "E" into A, the diamond into X and the asterisk into Y. Print the diamond in the screen bottom left, the asterisk in the bottom right, and two "E"'s, one in each of the top two corner of the screen.

Let's do it together.

For loading a diamond in the accumulator, the X and E I will look at the petscii values on the commodore Character Set. E is 5, X is 24, and the diamond is 90.
LDA #$5A ; a diamond  - dec 90 -> hex 5A
LDX #$24 ; asterisk   - dec 42 -> hex 24
LDY #$05 ; letter 'E' - dec 5 -> hex 5
The next step asks us to swap those characters. We can't use immediate mode commands, that is we can't cheat by just loading them again. So we need a way to transfer those data. We can use some direct transfer instructions, (you can transfer some data from a register to another) but we also need to store one value in a swap area we will define later.
STX SWAP_AREA ; we will save the value from X into memory
TAX           ; this will move the diamond from the
                      ; Accumulator to the X register
TYA           ; This will move the 'E' from the Y register
                ; to the Accumulator
LDY SWAP_AREA ; in the end we will read the value we saved                 ; before and store into 'Y'
Now we can proceed to store the data on the four screen locations AND change the foreground character color.
STA $0400 ; E on top left
STA $0427 ; E on top right
STX $07C0 ; the diamond (now in X) goes to bottom left
STY $07E7 ; the asterisk (now in Y) goes to bottom right
LDX #$01  ; we setup the foreground color
STX $D800 ; we tell the VIC-2 to use foreground color 
STX $D827 ; on the four corners
STX $DBC0
STX $DBE7
 Now we can exit and define the SWAP area

    RTS
SWAP_AREA ; this is a label and tells the assembler to

          ; use the following area as a memory location
    BYTE $00
The following is the full listing, including the basic launcher.
 ; Exercise 01.05

*=$0820

        LDA     #$5A            ; a diamond  - dec 90 -> hex 5A
        LDX     #$2A            ; asterisk   - dec 42 -> hex 2A
        LDY     #$05            ; letter 'E' - dec 5 -> hex 5

        STX     SWAP_AREA       ; we will save the value from X into memory
        TAX                     ; this will move the diamond from the
                                ; Accumulator to the X register
        TYA                     ; This will move the 'E' from the Y register 
                                ; to the Accumulator
        LDY     SWAP_AREA       ; in the end we will read the value we saved
                                ; before and store into 'Y'

        STA     $0400           ; E on top left
        STA     $0427           ; E on top right
        STX     $07C0           ; the diamond (now in X) goes to bottom left
        STY     $07E7           ; the asterisk (now in Y) goes to bottom right
        LDX     #$01            ; we setup the foreground color
        STX     $D800           ; we tell the VIC-2 to use foreground color 
        STX     $D827           ; on the four corners
        STX     $DBC0
        STX     $DBE7
        RTS

SWAP_AREA                       ; this is a label and tells the assembler to
                                ; use the following area as a memory location 
    BYTE $00


; The following is how the machine stores the SYS 2080 command on line 10 of the
; Basic interpreter

*=$0801

        BYTE    $0E, $08, $0A, $00, $9E, $20, $32, $30, $38, $30, $00, $00, $00

No comments:

Post a Comment