DEV Community

Michael Brackett
Michael Brackett

Posted on

Week 2 - Part 1

This blog post will speak about Week Two Part 1, which contains the lab 2

You can view Lab 1 using the cdot wiki here but in essence I had to draw 4 lines around a square in different colours.

Sounds easy right? Well, it's surprisingly difficult when you are working with 6502 Assembly code.

Below is my entire code for the program:

    lda #$00    ; load A with 00
        sta $40     ; set lowbit to 00
    lda #$02    ; load A with 02
    sta $41     ; set highbit to 02 (0200)

    lda #$05    ; load a with color green

    ldy #$00    ; set y index to 0

top:    sta ($40),y ; store A inside memory location $40+y highbit is 02, lowbit is 00 so 0200+y
    iny     ; increment y
    CPY #$20    ; compare y to hash value 20 (dec 32)
    bne top     ; if y does not equal #$20 go back to top

    lda #$05    ; load accumulator with final page (0500)
    sta $41     ; store a inside highbit
    lda #$06    ; load blue color into A
    ldy #$e0    ; load final line in Y (05e0)

bottom: sta ($40),y ; store A inside memory location $40+y highbit is 05, lowbit is e0 so 05e0+y
    iny     ; increment y
    cpy #$ff    ; compare y to hash value ff (end of line 05ff)
    bne bottom  ; get out of loop once you go to next page

    ldy #$00    ; reset Y to 00 (follow commands are to reset pixel to first line first pixel)
        lda #$00    ; load A to 00
        sta $40     ; store A inside lowbit
        lda #$02    ; load A with 02
        sta $41     ; store A inside highbit

left_to_right: 
    lda #$07    ; load A with colour yellow
    sta ($40),y ; store A inside memory location $40+y highbit is 02, lowbit is 00 so 0200+y (will change after first loop = 0200)
        tya         ; transfer y to a
        clc         ; clear counter bit for next line since we are adding
        adc #$1f    ; this addition skips to the other side of the bitmap

        tay     ; transfer a to y

        lda #$04    ; load A with colour purple   

        sta ($40),y ; store A inside memory location $40+y highbit is 02, lowbit is 00 so 0200+y (will change after first loop = 021f) 
        iny         ; increment y
        bne left_to_right ; restart loop until end of page

        inc $41     ; increment highbit to next page (first loop goes from 02 to 03)        
        ldx $41     ; load x with current page
        cpx #$06    ; compare page to 06 (05ff is final pixel of bitmap so if it equals highbit equals 06 we are outside of bitmap)

        bne left_to_right    ; restart loop if current page (highbit) does not equal 6
Enter fullscreen mode Exit fullscreen mode

Here is what it outputs: output

You can actually run the code yourself here

As you can see, it doesn't even look that great. Just 4 flat plain colours around the border of a square. But it was actually fun & interesting while I was working on it.

Something that I had trouble understanding was why we used memory location 40 when storing the accumulator when memory location 41 was where the actual page number was stored (40 just always contained 00).

Well Chris came to my rescue and explained to me that 6502 is something called 'little endian' which basically means that the memory locations are split in two, the lowbyte and the highbyte.
Take a look at this line and it might help you understand as well:

sta ($40),y ; store A inside memory location $40+y highbit is 02, lowbit is 00 so 0200+y 
Enter fullscreen mode Exit fullscreen mode

Basically we use the lowbyte to access the entire point in memory (40 = 0200).

Once I understood this concept the rest was fairly straight forward!

Top comments (0)