Lab 2 - 6502 Assembly Language Lab
Hello, my name is Pat and this is my Lab 2 write-up for my Software Portability and Optimization course at Seneca. Guidelines for this lab can be found here. Short version of it is, I was given some 6502 assembly language code (included below) and given some tasks to do with that code. The code given is very simple and fills the display with the colour yellow by looping through all of the pixels on the display.
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
Part 1 - Calculating Performance
First step was to calculate how long it takes for the code to execute assuming a 1 MHz clock speed. I calculated it would take 11323 μs. The table below shows my work for this calculation.
cycles | cycle count | alt cycles | alt count | total | |||
lda #$00 | 2 | 1 | 2 | ||||
sta $40 | 3 | 1 | 3 | ||||
lda #$02 | 2 | 1 | 2 | ||||
sta $41 | 3 | 1 | 3 | ||||
lda #$07 | 2 | 1 | 2 | ||||
loop: | sta ($40),y | 6 | 1024 | 6144 | |||
iny | 2 | 1024 | 2048 | ||||
bne loop | 3 | 1020 | 2 | 4 | 3068 | ||
inc $41 | 5 | 4 | 20 | ||||
ldx $41 | 3 | 4 | 12 | ||||
cpx #$06 | 2 | 4 | 8 | ||||
bne loop | 3 | 3 | 2 | 1 | 11 | ||
Total: | 11323 | cycles | |||||
CPU speed: | 1 | MHz | |||||
uS per clock: | 1 | ||||||
Time: | 11323 | uS |
The next step was to find optimizations in this code, and then calculate the runtime, however I was unfortunately unable to find any optimizations.
Part 2 - Modifying the code
The first step of this part is to modify the code to fill the display with light blue instead of yellow. To do this I change line 6 from lda #$07 to lda #$0e. Full code is displayed below.
lda #$00 ; set a pointer at $40 to point to $0200 sta $40 lda #$02 sta $41 lda #$0e ; 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
Second step was to make each page of the display a different. To do this, I added 1 to the colour value every time the page changed. This is a somewhat simple approach, but it's drawbacks are that the colours display must all be consecutive according to the assembly languages listing. Some variance can be achieved by changing how much the value increments by (e.g. by 2 or 3) but not much. Code displayed below.
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
adc #$01
cpx #$06 ; compare with 6
bne loop ; continue until done all pages
Comments
Post a Comment