Week 2: Lab 2 - Getting hands on assembly

Hello everyone and welcome to my blog!

During this lab we had a hands on experience with assembly language using our online 6502 emulator. We had a chance to experiment a little bit with assembly language and understand some things that happen behind us at the very low level, and why things happen the way the do.

In this lab, we were experimenting with bitmap code, visual effects, trying to understand how the work with memory is done. The task was to draw a green line across the top of the bitmap screen, a blue line across the bottom, a yellow line down the left side of the screen and a purple line down the right side.

Here is our starting point, the code we are going to experiment with:

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

lda #$07 ; colour
ldy #$00 ; set index to 0

loop:
sta ($40),y ; set pixel
iny ; increment index
bne loop ; continue until done the page

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

If you are interested in testing and experimenting yourself please follow http://6502.cdot.systems/

Part 1: experimenting

The original code fills up the screen with the yellow collor.

1) adding tya after loop: label and before the sta ($40),y

This instruction transfers the value of a into y. Therefore, the screen will fill up with the strips of different colors.

2-3) adding lsr after tya and adding more lsr

This instruction conducts logical shift right, it causes division of bit by 2. The strips of colors become more thick and the more we add lsr after tya the more bits shift and thicker color gets.

4) replace lsr instructions with asl

This instruction conducts arithmetic shift left. This is doing multiplication. In the comparison to previous experiment, this will make strips thinner. This shifts color and after every additional asl instruction, there will be less colors for strips, but the strips themselves remain 1 pixel. At the end, after adding 4 or 5 asl instructions there is only one yellow dot in the first pixel.

5) add more consecutive iny instructions

This instruction increment y 5 times, skipping through the escape value every loop, and will continue until the screen is filled up.

Part 2: writing code

1) Write code to draw a green line across the top of the bitmap screen and a blue line across the bottom.

2) Extend the previous code to draw a yellow line down the left side of the screen and a purple line down the right side.

lda #$00 ; set a pointer at $40 to point to $0200
sta $40
lda #$02
sta $41
lda #$05 ; colour
ldy #$00 ; set index to 0

;draws green line at the top of the screen

loop: sta ($40),y ; set pixel
iny ; increment index
cpy #$20
bne loop ; continue until done the page
ldy #$E0 ; last line of 5
lda #$05 ; set page to 5
sta $41 ; set page to 5
lda #$06

;draws blue line at the bottom of the screen

loop2:
sta ($40),y
iny
cpy #$00
bne loop2
ldy #$00
lda #$00 ; set a pointer at $40 to point to $0200
sta $40
lda #$02
sta $41

;draws yellow line on the left and purple line on the right side

loop3:
clc
lda #$07
sta ($40),y
tya ;move y value to a for adding command
adc #$1f ;add 32 to accumulator (display is 32 pixels wide)
tay ;transfer new value back to y
lda #$04 ;load purple
sta ($40),y
iny ;increment to start of next line
cpy #$00 ;check if finished page
bne loop3
inc $41 ;increment page
ldx $41 ;load value of page
cpx #$06 ;check if passed end of display
bne loop3

Comments