Lab1

 

Lab 1: 6502 Assembly Language Lab

Introduction

In this lab, we explore 6502 assembly language using a processor emulator. The 6502 is a well-known 8-bit microprocessor, and understanding its low-level architecture will allow us to grasp fundamental programming concepts, including memory management and CPU cycles. In this exercise, we wrote and optimized an assembly program that fills the screen with color. We then calculate execution time, memory usage, and modify the code to achieve specific outcomes.

Setup

To follow this lab, we used the 6502 Emulator

Bitmap Code

The following code fills the screen with the color yellow by pointing to memory at $0200 and incrementally writing the color to every pixel.

    lda #$00        ; Load $00 into accumulator (low byte of pointer)

    sta $40         ; Store low byte in address $40

    lda #$02        ; Load $02 into accumulator (high byte of pointer)

    sta $41         ; Store high byte in address $41

    lda #$07        ; Load color value (yellow)

    ldy #$00        ; Initialize Y index to 0


loop:

    sta ($40), y    ; Store color at address ($40) + Y

    iny             ; Increment Y index

    bne loop        ; Loop until Y overflows (256 pixels)

    inc $41         ; Increment high byte to move to the next page

    ldx $41         ; Load high byte into X register

    cpx #$06        ; Compare X with 6 (end of screen)

    bne loop        ; Repeat until all pages are filled


Calculating Performance

We now calculate how long it takes for this program to execute, assuming a 1 MHz clock speed.

Cycles Per Instruction

Below is the breakdown of cycles for each instruction:

InstructionCyclesCountTotal Cycles
lda #$00212
sta $40313
lda #$02212
sta $41313
lda #$07212
ldy #$00212
sta ($40),y610246144
iny210242048
bne loop3 (branch taken)10203060
2 (branch not taken)48
inc $415420
ldx $413412
cpx #$06248
bne loop3 (branch taken)39
2 (branch not taken)12

Total Cycles = 11,325

Execution Time

The execution time is calculated by dividing the total cycles by the clock speed. At 1 MHz:

  • Execution Time = 11,325 cycles / 1,000,000 cycles per second = 0.011325 seconds

Memory Usage

The memory footprint of the program is calculated by considering the bytes used by each instruction:

InstructionMemory (Bytes)
lda #$002
sta $402
lda #$022
sta $412
lda #$072
ldy #$002
sta ($40),y2
iny1
bne loop2
inc $412
ldx $412
cpx #$062
bne loop2

Total Memory = 25 bytes

Optimizing the Code

We can optimize the code by directly addressing different memory pages instead of incrementing the pointer, thus reducing the need for inc and ldx instructions. This results in faster execution.

    lda #$07        ; Load yellow color
    ldy #$00        ; Initialize Y index

loop:
    sta $0200, y    ; Store yellow at $0200 + Y
    sta $0300, y    ; Store yellow at $0300 + Y
    sta $0400, y    ; Store yellow at $0400 + Y
    sta $0500, y    ; Store yellow at $0500 + Y
    iny             ; Increment Y index
    bne loop        ; Repeat until Y overflows

Optimized Performance Calculation

InstructionCyclesCountTotal Cycles
lda #$07212
ldy #$00212
sta $0200,y52561280
sta $0300,y52561280
sta $0400,y52561280
sta $0500,y52561280
iny2256512
bne loop3 (branch taken)255765
2 (branch not taken)12

Total Cycles = 6403

Optimized Execution Time

  • Execution Time = 6,403 cycles / 1,000,000 cycles per second = 0.006403 seconds

Optimized Memory Usage

InstructionMemory (Bytes)
lda #$072
ldy #$002
sta $0200, y3
sta $0300, y3
sta $0400, y3
sta $0500, y3
iny1
bne loop2

Total Memory Usage = 19 bytes

Changing Colors

1) To change the color from yellow to light blue, we replace the value in the lda instruction

    ; Set a pointer in memory location $40 to point to $0200

    lda #$00        ; Load low byte of address

    sta $40         ; Store it at $40

    lda #$02        ; Load high byte of address

    sta $41         ; Store it at $41


    ; Set the color to light blue

    lda #$0E        ; Load color number for light blue

    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 with the page (256 pixels)


    inc $41         ; Increment to the next page

    ldx $41         ; Get the current page number

    cpx #$06        ; Compare with 6 (total number of pages)

    bne loop        ; Continue until all pages are filled



Optimized Performance Calculation

InstructionCyclesCountTotal Cycles
lda #$00212
sta $40313
lda #$02212
sta $41313
lda #$0E212
ldy #$00212
sta ($40), y62561536
iny2256512
bne loop3 (branch taken)255765
2 (branch not taken)12
inc $415420
ldx $413412
cpx #$06248
bne loop3 (branch taken)39

Total Cycles

Total Cycles=2+3+2+3+2+2+1536+512+765+2+20+12+8+9=1850\text{Total Cycles} = 2 + 3 + 2 + 3 + 2 + 2 + 1536 + 512 + 765 + 2 + 20 + 12 + 8 + 9 = 1850

Optimized Execution Time

Execution Time=1850 cycles1,000,000 cycles per second=0.00185 seconds\text{Execution Time} = \frac{1850 \text{ cycles}}{1,000,000 \text{ cycles per second}} = 0.00185 \text{ seconds}

Optimized Memory Usage

InstructionMemory (Bytes)
lda #$002
sta $402
lda #$022
sta $412
lda #$0E2
ldy #$002
sta ($40), y2
iny1
bne loop2
inc $412
ldx $412
cpx #$062
bne loop2

Total Memory Usage
Total Memory Usage=2+2+2+2+2+2+2+1+2+2+2+2+2=25 bytes


2) Different Color Per Page

ldy #$00       ; Initialize Y index to 0


loop:

lda #$05       ; Load green

sta $0200, y   ; Store at $0200 + Y (first page)

lda #$04       ; Load purple

sta $0300, y   ; Store at $0300 + Y (second page)

lda #$03       ; Load cyan

sta $0400, y   ; Store at $0400 + Y (third page)

lda #$02       ; Load red

sta $0500, y   ; Store at $0500 + Y (fourth page)

iny            ; Increment Y index

bne loop       ; Continue until Y wraps around


Cycle Count Calculation:

InstructionCyclesCountTotal Cycles
ldy #$00212
lda #$052256512
sta $0200, y52561280
lda #$042256512
sta $0300, y52561280
lda #$032256512
sta $0400, y52561280
lda #$022256512
sta $0500, y52561280
iny2256512
bne loop3255765

Total Cycles: 8437 cycles

Assuming a 1 MHz clock speed:

  • Execution time = 8437 µs
  • = 8.437 ms
  • = 0.008437 seconds

Memory Usage:

InstructionMemory (bytes)
ldy #$002
lda #$052
sta $0200, y3
lda #$042
sta $0300, y3
lda #$032
sta $0400, y3
lda #$022
sta $0500, y3
iny1
bne loop2

Total Memory Usage: 25 bytes

3) Different Color per Pixel

ldy #$00       ; Initialize Y index to 0

loop:
lda $fe        ; Load random colour from memory location $fe
sta $0200, y   ; Store at $0200 + Y (first page)
lda $fe        ; Load random colour
sta $0300, y   ; Store at $0300 + Y (second page)
lda $fe        ; Load random colour
sta $0400, y   ; Store at $0400 + Y (third page)
lda $fe        ; Load random colour
sta $0500, y   ; Store at $0500 + Y (fourth page)
iny            ; Increment Y index
bne loop       ; Continue until Y wraps around (256 pixels per page)

Cycle Count Calculation:

InstructionCyclesCountTotal Cycles
ldy #$00212
lda $fe310243072
sta $0200, y52561280
sta $0300, y52561280
sta $0400, y52561280
sta $0500, y52561280
iny2256512
bne loop3255765

Total Cycles: 10,471 cycles

Assuming a 1 MHz clock speed:

  • Execution time = 10,471 µs (microseconds)
  • = 10.471 ms (milliseconds)
  • = 0.010471 seconds

Memory Usage:

InstructionMemory (bytes)
ldy #$002
lda $fe2
sta $0200, y3
sta $0300, y3
sta $0400, y3
sta $0500, y3
iny1
bne loop2

Total Memory Usage: 19 bytes

Reflection
RefI found the hands-on work with 6502 assembly language both challenging and rewarding. Writing code to fill the screen with colors deepened my understanding of low-level programming and how memory manipulation works in an 8-bit architecture. Calculating cycle counts and optimizing the code provided insight into improving performance on limited hardware. Each modification—from changing colors to using random values—required thinking critically about memory use and CPU cycles, highlighting the precision needed at this level of coding. Overall, this lab enhanced my practical knowledge of assembly language and optimized programming techniques.



Comments

Popular posts from this blog

Project Stage 2

Project Stage 1