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:
Instruction | Cycles | Count | Total Cycles |
---|---|---|---|
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 |
ldy #$00 | 2 | 1 | 2 |
sta ($40),y | 6 | 1024 | 6144 |
iny | 2 | 1024 | 2048 |
bne loop | 3 (branch taken) | 1020 | 3060 |
2 (branch not taken) | 4 | 8 | |
inc $41 | 5 | 4 | 20 |
ldx $41 | 3 | 4 | 12 |
cpx #$06 | 2 | 4 | 8 |
bne loop | 3 (branch taken) | 3 | 9 |
2 (branch not taken) | 1 | 2 |
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:
Instruction | Memory (Bytes) |
---|---|
lda #$00 | 2 |
sta $40 | 2 |
lda #$02 | 2 |
sta $41 | 2 |
lda #$07 | 2 |
ldy #$00 | 2 |
sta ($40),y | 2 |
iny | 1 |
bne loop | 2 |
inc $41 | 2 |
ldx $41 | 2 |
cpx #$06 | 2 |
bne loop | 2 |
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.
Optimized Performance Calculation
Instruction | Cycles | Count | Total Cycles |
---|---|---|---|
lda #$07 | 2 | 1 | 2 |
ldy #$00 | 2 | 1 | 2 |
sta $0200,y | 5 | 256 | 1280 |
sta $0300,y | 5 | 256 | 1280 |
sta $0400,y | 5 | 256 | 1280 |
sta $0500,y | 5 | 256 | 1280 |
iny | 2 | 256 | 512 |
bne loop | 3 (branch taken) | 255 | 765 |
2 (branch not taken) | 1 | 2 |
Total Cycles = 6403
Optimized Execution Time
- Execution Time =
6,403 cycles / 1,000,000 cycles per second = 0.006403 seconds
Optimized Memory Usage
Instruction | Memory (Bytes) |
---|---|
lda #$07 | 2 |
ldy #$00 | 2 |
sta $0200, y | 3 |
sta $0300, y | 3 |
sta $0400, y | 3 |
sta $0500, y | 3 |
iny | 1 |
bne loop | 2 |
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
Instruction | Cycles | Count | Total Cycles |
---|---|---|---|
lda #$00 | 2 | 1 | 2 |
sta $40 | 3 | 1 | 3 |
lda #$02 | 2 | 1 | 2 |
sta $41 | 3 | 1 | 3 |
lda #$0E | 2 | 1 | 2 |
ldy #$00 | 2 | 1 | 2 |
sta ($40), y | 6 | 256 | 1536 |
iny | 2 | 256 | 512 |
bne loop | 3 (branch taken) | 255 | 765 |
2 (branch not taken) | 1 | 2 | |
inc $41 | 5 | 4 | 20 |
ldx $41 | 3 | 4 | 12 |
cpx #$06 | 2 | 4 | 8 |
bne loop | 3 (branch taken) | 3 | 9 |
Total Cycles
Optimized Execution Time
Optimized Memory Usage
Instruction | Memory (Bytes) |
---|---|
lda #$00 | 2 |
sta $40 | 2 |
lda #$02 | 2 |
sta $41 | 2 |
lda #$0E | 2 |
ldy #$00 | 2 |
sta ($40), y | 2 |
iny | 1 |
bne loop | 2 |
inc $41 | 2 |
ldx $41 | 2 |
cpx #$06 | 2 |
bne loop | 2 |
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:
Instruction | Cycles | Count | Total Cycles |
---|---|---|---|
ldy #$00 | 2 | 1 | 2 |
lda #$05 | 2 | 256 | 512 |
sta $0200, y | 5 | 256 | 1280 |
lda #$04 | 2 | 256 | 512 |
sta $0300, y | 5 | 256 | 1280 |
lda #$03 | 2 | 256 | 512 |
sta $0400, y | 5 | 256 | 1280 |
lda #$02 | 2 | 256 | 512 |
sta $0500, y | 5 | 256 | 1280 |
iny | 2 | 256 | 512 |
bne loop | 3 | 255 | 765 |
Total Cycles: 8437 cycles
Assuming a 1 MHz clock speed:
- Execution time = 8437 µs
- = 8.437 ms
- = 0.008437 seconds
Memory Usage:
Instruction | Memory (bytes) |
---|---|
ldy #$00 | 2 |
lda #$05 | 2 |
sta $0200, y | 3 |
lda #$04 | 2 |
sta $0300, y | 3 |
lda #$03 | 2 |
sta $0400, y | 3 |
lda #$02 | 2 |
sta $0500, y | 3 |
iny | 1 |
bne loop | 2 |
Total Memory Usage: 25 bytes
3) Different Color per Pixel
Cycle Count Calculation:
Instruction | Cycles | Count | Total Cycles |
---|---|---|---|
ldy #$00 | 2 | 1 | 2 |
lda $fe | 3 | 1024 | 3072 |
sta $0200, y | 5 | 256 | 1280 |
sta $0300, y | 5 | 256 | 1280 |
sta $0400, y | 5 | 256 | 1280 |
sta $0500, y | 5 | 256 | 1280 |
iny | 2 | 256 | 512 |
bne loop | 3 | 255 | 765 |
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:
Instruction | Memory (bytes) |
---|---|
ldy #$00 | 2 |
lda $fe | 2 |
sta $0200, y | 3 |
sta $0300, y | 3 |
sta $0400, y | 3 |
sta $0500, y | 3 |
iny | 1 |
bne loop | 2 |
Total Memory Usage: 19 bytes
Comments
Post a Comment