6502 - Assembly Language Lab
Tecca Yu
Posted on January 31, 2022
I just started learning assembly language and software portability and optimization. The goal of this post is to demonstrate some basic Assembly code examples and how do they work on a 6502 processor emulator.
1.The following code fills the emulator's bitmapped display with the color yellow (lda #$07).
lda #$00 ; set a pointer at $40 to point to $0200
sta $40
lda #$02
sta $41
lda #$07 ; colour number
ldy #$00 ; set index to 0
loop: sta ($40),y ; set pixel at the address (pointer)+Y
iny ; increment index
bne loop ; continue until done the page
inc $41 ; increment the page
ldx $41 ; get the current page number
cpx #$06 ; compare with 6
bne loop ; continue until done all pages
This bitmap code paints the whole page yellow. By increasing the index (ldy #$00)which helps to indicate the location of the page as a starting point and painting that bit yellow, it goes into a loop and paints the whole pages yellow at the end.
Modifying the Code
Change the code to fill the display with light blue instead of yellow.
Changing the color number from #$07(yellow) to #$e(light blue)
lda #$00 ; set a pointer at $40 to point to $0200
sta $40
lda #$02
sta $41
lda #$e ; colour number
ldy #$00 ; set index to 0
loop:
sta ($40),y ; set pixel at the address (pointer)+Y
iny ; increment index
bne loop ; continue until done the page
inc $41 ; increment the page
ldx $41 ; get the current page number
cpx #$06 ; compare with 6
bne loop ; continue until done all pages
Change the code to fill the display with a different color on each page
lda #$00 ; set a pointer at $40 to point to $0200
sta $40
lda #$02
sta $41
lda #$07 ; colour number
ldy #$00 ; set index to 0
loop:
sta ($40),y ; set pixel at the address (pointer)+Y
iny ; increment index
bne loop ; continue until done the page
lsr
inc $41 ; increment the page
ldx $41 ; get the current page number
cpx #$06 ; compare with 6
bne loop ; continue until done all pages
Result:
Adding instruction TYA
lda #$00 ; set a pointer at $40 to point to $0200
sta $40
lda #$02
sta $41
lda #$07 ; colour number
ldy #$00 ; set index to 0
loop: tya
sta ($40),y ; set pixel at the address (pointer)+Y
iny ; increment index
bne loop ; continue until done the page
inc $41 ; increment the page
ldx $41 ; get the current page number
cpx #$06 ; compare with 6
bne loop ; continue until done all pages
Instruction TYA means ‘Transfer Y to A’ it is one of the instruction for Transferring Data between Registers. Every time the value in a Y register is incremented, the value in the A register is incremented as well. As the value in the A register takes responsibility to decide what color is painted to the bit, the color has changed for each bit. There are 16 different colors you can use that can be represented from 0000 to 1111. When the number is over 1111, the above number will be neglected. For example, if the color is 10000, the first number 1 will be ignored and the color displays in the emulator will be 0000. As the number of bit of each line is 32, a bunch of all different colors is repeatedly painted twice for each line. As a result, the emulator will be painted horizontally.
Add instruction lsr after tya
lda #$00 ; set a pointer at $40 to point to $0200
sta $40
lda #$02
sta $41
lda #$07 ; colour number
ldy #$00 ; set index to 0
loop:
tya:lsr
sta ($40),y ; set pixel at the address (pointer)+Y
iny ; increment index
bne loop ; continue until done the page
inc $41 ; increment the page
ldx $41 ; get the current page number
cpx #$06 ; compare with 6
bne loop ; continue until done all pages
The command ‘LSR’ means ‘Logically shift right’. I assume it shifts the color bit right, can act as divide-by-2.
Each time I use LSR in this code, the width of the color is going to be smaller and smaller.
Write a program which draws lines at the top and bottom of the display:
A red line across the top
lda #$00 ; set a pointer at $40 to point to $0200
sta $40
lda #$02
sta $41
lda #$2 ; set color red
ldy #$00 ; set index to 0
top: sta ($40), y ; set pixel
iny ; increment index
cpy #$20
bne top
result:
A green line across the bottom
lda #$00 ; set a pointer at $40 to point to $0200
sta $40
lda #$02
sta $41
lda #$02 ; set color green
ldy #$00 ; set index to 0
top: sta ($40), y ; set pixel
iny ; increment index
cpy #$20 ; compare y value with 20. If the index is 20, end the loop
bne top
lda #$05
sta $41 ; point the last page
lda #$06 ; set color blue
ldy #$e0 ; set the index to indicate the last line
bottom: sta ($40), y ; set pixel
iny ; increment index
bne bottom ; continue until done the line
lda #$00 ; set a pointer at $40 to point to $0200
sta $40
lda #$05
sta $41
My experience with this lab:
It is new to learn a low-level language like assembly comparing to all the other languages I've learn. I have learned that Assembly Language is the world of bits and bytes. The commands are simple and clear but as it is simple I need to find a better way to remember them, and at this point I think only practice them and use them in writing code would help.
Posted on January 31, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.