SPO600 LAB 1

6502 Assembly Language Lab

Hello! My name is Harleen Singh, and this post is about my experiments with 6502 assembly language as part of the SPO600 course. 

Bitmap Code

The first task was to run the following code to fill the display with yellow using the 6502 processor:

Here is the code which was given to us in Lab 1 instructions.

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

To calculate the execution time for my code running on a 1 MHz clock, I broke it down into two parts: the inner loop and the outer loop.



12,996 \, \text{cycles} = 12.996 \, \text{milliseconds}

Memory Usage

The memory usage was simple to calculate. Most of the instructions used 2 bytes (except for the iny and inc instructions, which use 1 byte). The total memory consumption for the code was:

27 bytes

Optimization

The optimized code fills screen memory more efficiently by writing 4 pixels per loop iteration, reducing total iterations from 256 to 64, and improving execution speed significantly.

lda #$00 ; Set a pointer in memory location $40 to point to $0200
sta $40 ; Low byte ($00) goes in address $40 lda #$02 ; High byte sta $41 ; High byte ($02) goes into address $41 lda #$07 ; Color number ldy #$00 ; Set index to 0 loop: sta ($40), y ; Set pixel color at the address (pointer) + Y iny ; Increment index sta ($40), y ; Set next pixel color iny ; Increment index sta ($40), y ; Set next pixel color iny ; Increment index sta ($40), y ; Set next pixel color iny ; Increment index bne loop ; Continue until all pixels are set for 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

After running the above code on 6502 emulator we can calculate the execution time:


8,260cycles=8.260milliseconds

Modifying the Code

1. Changing the Color to Light Blue

 lda #$0E: This line loads the color code for light blue. The rest of the code remains the same as the original, looping through each pixel and filling it with the light blue color until the entire screen is filled.

    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 #$0E       ; Light blue color code

    ldy #$00       ; Set index to 0


loop:

    sta ($40),y    ; Set pixel color 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

 

2. Different Color on Each Page

The code uses registers $40 and $41 to access the screen memory. It goes through different pages, fills them with specific colors, and jumps to the corresponding color based on which page it is on.

    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

    ldy #$00       ; Set index to 0


loop:

    ldx $41        ; Get the current page number

    cpx #$02

    beq blue       ; If page 2, set to blue

    cpx #$03

    beq green      ; If page 3, set to green

    cpx #$04

    beq red        ; If page 4, set to red

    cpx #$05

    beq yellow     ; If page 5, set to yellow


blue:

    lda #$06       ; Color code for blue

    jmp page_fill


green:

    lda #$05       ; Color code for green

    jmp page_fill


red:

    lda #$02       ; Color code for red

    jmp page_fill


yellow:

    lda #$07       ; Color code for yellow


page_fill:

    sta ($40),y    ; Fill the current pixel with the selected color

    iny            ; Increment the index

    bne page_fill  ; Continue filling the page


    inc $41        ; Increment the page number

    cpx #$06       ; Compare page number to 6 (end of the screen)

    bne loop       ; Continue the loop until all pages are filled



3. Random Color for Each Pixel

The code starts by setting up two registers, $40 and $41, to point to the screen memory at $0200. It then loops through each pixel and fills it with colors taken from a random number generator at $FE. Once one page of colors is filled, it moves on to the next page and repeats this until all pages are filled.


    lda #$00          ; Set pointer for low byte

    sta $40           ; Memory address $40 (low byte)

    lda #$02          ; Set pointer for high byte

    sta $41           ; Memory address $41 (high byte)

    ldy #$00          ; Initialize index to 0


loop:

    lda $FE           ; Load a pseudo-random color from memory

    sta ($40),y       ; Store the color at the pixel address

    iny               ; Increment the index

    bne loop          ; Repeat until all pixels are filled


    inc $41           ; Increment to the next page

    ldx $41           ; Load current page number

    cpx #$06          ; Compare with the number of pages

    bne loop          ; Repeat for all pages




MY EXPERIENCE

Working with Assembly Language was a tough but rewarding experience. I realized how crucial it is to optimize code for better performance; even minor things can lead to significant improvements. Learning how the CPU processes instructions gave me a deeper appreciation for hardware. Debugging in Assembly required me to closely track registers and memory, which sharpened my problem-solving skills. Overall, this experience sparked my interest in computer architecture and low-level programming, showing me that small changes can greatly boost efficiency in coding.

Comments

Popular posts from this blog

Project Stage 1- GCC Build & Dump on AArch64

Project Stage 3: Tidy & Wrap