Lab01
Lab 1:
Introduction
Welcome to the first lab of our assembly language course, where we'll delve into the world of Mob Programming using the 6502 Emulator. Mob Programming is an extended form of Pair Programming, allowing a group to collaborate on coding tasks with designated roles like Driver and Presenter. This collaborative approach enhances problem-solving and knowledge sharing among group members. In this lab, we'll explore filling a bitmapped display with colour and optimizing code performance in assembly language.
Setting Up
- Open the 6502 Emulator at 6502 Emulator in another tab or window.
- Save your work periodically as the emulator does not auto-save. Use methods like copying code to a local file or using Git for version control.
Bitmap Code
We'll start by filling the emulator's bitmapped display with the color yellow using the following code:
assemblylda #$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 ; color number 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
- Paste this code into the emulator.
- Press Assemble and then Run to execute the code and see the results.
Calculating Performance
Calculating the execution time for the code is crucial to understanding its efficiency. Here's a breakdown:
- Clock speed: 1 MHz (1 cycle = 1 microsecond)
- Total cycles for the initial code: 11329 cycles
- Execution time: 11.329 milliseconds
Optimizing the Code
Loop Unrolling is a technique to optimize the code by doing more work in each iteration of the loop, thus reducing the total number of cycles.
Optimized Code with Loop Unrolling:
assemblylda #$07 ; color number ldy #$00 ; set index to 0 loop: sta $0200,y ; set pixel color at page 2+Y sta $0300,y ; set pixel color at page 3+Y sta $0400,y ; set pixel color at page 4+Y sta $0500,y ; set pixel color at page 5+Y iny ; increment index bne loop ; continue until done the page (256 pixels)
- Total cycles after optimization: 6403 cycles
- Execution time: 6.403 milliseconds
Modifying the Code
- Fill the display with light blue:
- Fill the display with different colors on each page:
assemblylda #$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 #$FE ; starting color number 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) adc #$11 ; change color for next 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
Experiments
- Adding
tya
Instruction:
Effect: The display shows a gradient of colors as tya
transfers the Y index value to the accumulator before storing it in memory.
- Using
lsr
Instruction:
Effect: The color intensity decreases as lsr
shifts bits to the right, creating a lighter gradient.
Conclusion
This lab was an excellent introduction to assembly language programming and performance optimization. The process of filling a bitmapped display and experimenting with different instructions provided a hands-on understanding of how low-level coding works.
Комментарии
Отправить комментарий