Introduction
In this blog post, I will endeavor to enhance the performance of the provided code by optimizing its execution time. Furthermore, I will also introduce modifications to vary the displayed colours.
The following 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
Calculating Performance
Q. Execution time, assuming a 1 MHz clock speed and total memory usage
https://www.masswerk.at/6502/6502_instruction_set.html#STA
lda #$00 ; 2 cycles
sta $40 ; 3 cycles
lda #$02 ; 2 cycles
sta $41 ; 3 cycles
lda #$07 ; 2 cycles
ldy #$00 ; 2 cycles
loop:
sta ($40),y ; 6 cycles
iny ; 2 cycles
bne loop ; 3 cycles
inc $41 ; 5 cycles
ldx $41 ; 4 cycles
cpx #$06 ; 2 cycles
bne loop ; 3 cycles
- Total cycle count = 39 cycles.
- Total time = 39 cycles / 1,000,000 cycles per sec = 0.000039 sec or 39 microsecond.
- Total memory usage = 13 instructions * 1 byte + 2 variables * 2 bytes = 17 bytes
Q. Optimized version in terms of faster execution time
ldx #$00 ; Set pointer in X register
stx $40 ; Store low byte in address $40
inx ; Increment X
stx $41 ; Store high byte in address $41
lda #$07 ; Colour number
ldy #$00 ; Set index to 0
ldx $41 ; Get the current page number (moved outside the loop)
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
cpx #$06 ; Compare with 6
bne loop ; Continue until done all pages
- Total cycle count = 35 cycles.
- Total time = 35 cycles / 1,000,000 cycles per sec = 0.000035 sec or 35 microsecond.
Modifying the Code
Q. Modified code to fill the display with light blue instead of yellow.
ldx #$00 ; Set pointer in X register
stx $40 ; Store low byte in address $40
inx ; Increment X
stx $41 ; Store high byte in address $41
lda #$0E ; Light blue color number (instead of #$07 for yellow)
ldy #$00 ; Set index to 0
ldx $41 ; Get the current page number (moved outside the loop)
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
cpx #$06 ; Compare with 6
bne loop ; Continue until done all pages
Q. Modified code to fill the display with a different colour on each page (each "page" will be one-quarter of the bitmapped display).
ldx #$00 ; Set pointer in X register
stx $40 ; Store low byte in address $40
inx ; Increment X
stx $41 ; Store high byte in address $41
ldy #$00 ; Set index to 0
ldx $41 ; Get the current page number (moved outside the loop)
loop:
; Calculate color based on page number
lda $41 ; Load current page number
asl ; Multiply by 2 (each page is one-quarter)
clc
adc #$0E ; Add base color value
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
cpx #$06 ; Compare with 6
bne loop ; Continue until done all pages
My thoughts through the process were...
My first impression of assembly language is worrying about not understanding anything in the given code. I still need a lot of time to figure out what is going on with the code. While I was calculating performance and modifying the codes to improve the performance, I learned how to calculate execution time with the total cycle count. It was very cumbersome to find each cycle and count to figure out the execution time, though.



Top comments (0)