Lab2 - 6502 assembly language lab

In this post, I will discuss about 6502 assembly language lab.

You can visit this website to use a web-based 6502 emulator. Enter your code in the left box on the website, then press the "Assembly" button followed by the "Run" button. The bitmap result will be displayed in the black box.

Bitmap Code

This code fills the emulator's bitmapped display with the colour yellow.

        lda #$00	        ; set a pointer in memory location $40 to point to $0200
	sta $40		; ... low byte ($00) goes in address $40
	lda #$02	
	sta $41		; ... high byte ($02) goes into address $41

	lda #$07	        ; colour number

	ldy #$00	        ; set index to 0

loop:	sta ($40),y	; set pixel colour at the address (pointer)+Y

	iny		        ; increment index
	bne loop	        ; continue until done the page (256 pixels)

	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 is the output when the code is executed.




Calculating Performance

Calculate the original code

I will assume a 1 MHz clock speed to calculate the execution time. Here is a video to guide you on how to calculate it.

CyclesCycle CountAlt CyclesAlt CountTotal
lda #$00212
sta $40313
lda #$02212
sta $41313
lda #$07212
ldy #$00212
loop:sta ($40),y610246144
iny210242048
bne loop31023213071
inc $415420
ldx $413412
cpx #$06248
bne loop432114
Total:11331cycles
CPU Speed:1MHz
uS per clock:1
Time:11331uS
11.331mS
0.011331S

Each page has 256 pixels and one page is a quarter of the display screen. Therefore, the cycle count in the loop is 1024. The bne loop was added 2 to cycles because it occurs to different page.
It takes 11.331 milliseconds, and the total memory usage is 25 bytes, as you can see in the first screenshot.

Decrease the execution time

I experimented with modifying the code to reduce its execution time. I believed that one approach to achieve this goal was to minimize the number of lines of code while preserving essential functionality. As a result, here are the outcomes of the execution:

Based on this code, I conducted an analysis of the execution time.

CyclesCycle CountAlt CyclesAlt CountTotal
lda #$07212
loop:sta ($40),y610246144
iny210242048
bne loop31023213071
inc $415420
bne loop432114
Total:11299cycles
CPU Speed:1MHz
uS per clock:1
Time:11299uS
11.299mS
0.011299S
For the fastest version of this program, there could be a way to reduce the execution time by more than twice as fast as the original version, but I couldn't find it. While the reduction in execution time is not dramatically significant, it seems that it has slightly improved.


Modifying the Code

Change the colour

To change the color from yellow to Light Blue in the code, you can modify the lda #$07 part to lda #$0E. Here's the modified code:
	lda #$00
	sta $40
	lda #$02
	sta $41
	
	lda #$0e
	
	ldy #$00
	
loop:	sta ($40),y
	
	iny
	bne loop
	
	inc $41
	ldx $41
	cpx #$06
	bne loop
It fills the display with light blue.

A different colour on each page 

After displaying the colour in the first quarter of the page, increase the colour number to make a different colour number. 
        lda #$00    ; set a pointer at $40 to point to $0200
        sta $40
        lda #$02
        sta $41

        lda #$07    ; colour number
        sta $10 ; store colour number to memory location $10

        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

        inc $10 ; increment the color number
        lda $10 ; colour number

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

This is the output:


Experiments

1. Add this instruction after the loop: label and before the sta ($40),y instruction: tya


It repeats 16 colours twice. Initially, the code loads the accumulator with the color code $07 (lda #$07), representing yellow, and sets the index to 0 (ldy #$00). The command, tya, transfers index Y to accumulator, and change the colour code to black. As we increment index Y, the colors are displayed sequentially according to the color code.

2. Add this instruction after the tya: lsr

This is the result of the code:

The command, lsr, means 'Shift one bit right'. Now, I only see a set of 16 colors once. From my monitoring, I could tell that the numbers repeat twice, and it seems like this is because each color is allocated two bits.

3. Repeat the above tests with two, three, four, and five lsr instructions in a row. Describe and explain the effect in each case.

This is the result when I repeat it with two lsr. Eight colors are displayed on one row, spanning a total of 16 colors across two rows. This pattern repeats a total of 16 times.

This is the result when I repeat it with three lsr. Four colors are displayed on one row. Each pattern spans four rows and repeats a total of eight times.

This is the result when I repeat it with four
lsr. This time, there are two colors on one line, and each pattern consists of a total of 8 lines. Each pattern repeats four times in total.


This is the result when I repeat it with five
lsr. Here, there is one color per line, with a total of 8 colors repeating four times.


Repeat the tests using asl instructions instead of lsr instructions. Describe and explain the effect in each case.


I'll briefly summarize this experiment. The images above represent the addition of the asl operation once, twice, and three times in sequence. Initially, the colors that repeated vertically in sets of eight decreased by half with each asl operation, while the repetition count of each pattern doubled.

More experiments and challenges

If you're curious about 6502 assembly language and want to explore more experiments and challenges, feel free to visit here for further exploration.








Comments

Popular posts from this blog

Unveiling the World of Software Development Methodologies: Agile, Scrum, and DevOps Made Simple

Understanding Docker: A Comprehensive Guide

Lab1 - Code Review