DEV Community

MundaneMonday
MundaneMonday

Posted on

Introduction to 6502 Assembly Language - SPO600

6502 is an inexpensive microprocessor that was commonly used in 8 bit hardware back in the 80s. Examples include Atari 2600 and NES. It can display up to 256 colors or 2^(8) at a time.

lda #$00    ; set a pointer at $40 to point to $0200
sta $40
lda #$02
sta $41

lda #$05    ; set colour to green

ldy #$00    ; set index to 0

greenloop:
    sta ($40),y ; set pixel at the address ($40)+Y
    iny     ; increment index
    cpy #$20
bne greenloop   ; continue until the line is filled

lda #$00    ; set a pointer at $40 to point to $0500
sta $42
lda #$05
sta $43

lda #$06    ; load blue

ldy #$ff    ; set index to ff

blueloop:
    sta ($42),y ; set pixel at address ($42)+Y
    dey     ; decrement index
    cpy #$df
bne blueloop    ; continue until the line is filled


clc     ; clear carry
lda #$07    ; load yellow
tax     ; put colour in X
ldy #$00    ; set counter to 0

yellowloop:
    txa     ; put colour in A
    sta ($40),y ; set pixel at the address ($40)+Y

    tya     ; add 20 to Y
    adc #$20
    tay
    cpy #$00    ; compare Y to 0
    clc     ; clear carry
bne yellowloop      ; loop until done the page
    inc $41     ; increment the page
    lda $41     ; get the page
    cmp #$06
bne yellowloop      ; loop until done all pages

lda #$1f    ; set a pointer at $44 to point to $021f
sta $44
lda #$02
sta $45

clc     ; clear carry
lda #$04    ; load purple
tax     ; put colour in X
ldy #$00    ; set counter to 0

purpleloop:
    txa     ; put color in A
    sta ($44),y ; set pixel at the address ($44)+Y

    tya     ; add 20 to Y
    adc #$20
    tay
    cpy #$00    ; compare Y to 0
    clc     ; clear carry
bne purpleloop      ; loop until done the page
    inc $45     ; increment the page
    lda $45     ; get the page
    cmp #$06
bne purpleloop      ; loop until done all pages
Enter fullscreen mode Exit fullscreen mode

To make a green line at the top, I set the stack pointer $40 to the color green. I set the index to 0 and start incrementing the pointer $40 till y is equal to $20(or 32th pixel).

The blue line at the bottom is similarly programmed as the green line at the top, except the index is initially set at the 255th pixel and decrement index till it reaches $df(or 223th pixel).

For the yellow vertical line on the left, I have to clear the carry flag. TAX transfers the color value of A register to X. Display yellow for every 32 pixels.

For the purple vertical line on the right, I set a pointer $44 at $1f or the 31st pixel, which is located at the top right corner. Then similar to the yellow line, I display purple for every 32 pixels or $20 added to Y.

Using the Opcode reference to determine the number of machine cycles this program requires, I calculated that it takes around 750 machine cycles, which is 750 microseconds.

I learned how to use the opcodes for this particular assembly language. it was difficult trying to visualize what a horizontal line looks like in this language, but then you realize that each line is 32 pixels and can just display the color for every 32 pixels.

Top comments (0)