(lab1)6502 Assembly Language Lab
In this lab, you will learn some of the basics of 6502 assembly language, in preparation for learning more complex x86_64 and AArch64 assembly language.
In this lab, you will learn some of the basics of 6502 assembly language, in preparation for learning more complex x86_64 and AArch64 assembly language.
Resources
- 6502 Wiki Page
- Opcode/Instruction References
- On this Wiki
- External
- 6502 Wiki Page
- Opcode/Instruction References
- On this Wiki
- External
Setup
1. Open the 6502 Emulator at http://6502.cdot.systems in another tab or window, keeping this lab open.
Important: The emulator does not save your work automatically. Remember to periodically save it to a file (copy-and-paste the code or use the Save
button to create local files). Recommendation: save your files to a directory, and use git to manage that directory.
Bitmap Code
2. The following code fills the emulator's bitmapped display with the colour yellow. Paste this code into the emulator:
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
3. Test the code by pressing the Assemble
button, then the Run
button. If the there are any errors assembling (compiling) the code, they will appear in the message area at the bottom of the page. Make sure the code is running correctly and that you understands how it works. Don't be afraid to experiment!
Calculating Performance
This is the key part of the lab.
4. Calculate how long it takes for the code to execute, assuming a 1 MHz clock speed.
Tip: The most common problem with performance analysis is the incorrect calculation of the number of times each instruction will execute. Check that part of your work carefully.
5. Also calculate the total memory usage for the program code plus any pointers or variables.
Modifying the Code
7. Change the code to fill the display with light blue instead of yellow. (Tip: you can find the colour codes on the 6502 Emulator page).
8. Change the code to fill the display with a different colour on each page (each “page” will be one-quarter of the bitmapped display).
Experiments
Go back to the bitmap code above, and try these experiments:
- What visual effect does this cause, and how many colours are on the screen? Why?TYA-This instruction moves the value that is in the index register Y to accumulator A without disturbing the content of the register Y. With TYA, the accumulator value changes as Y increments during the loop. Each row of pixels gets a different value depending on Y and pixels in the same column (same Y value) will have the same color.
- What visual effect does this cause, and how many colours are on the screen? Why?The LSR (Logical Shift Right) instruction performs a bitwise right shift on the value in the accumulator (A).This effectively divides the value in A by 2, causing a loss of precision in lower bits. Wider stripes of the same color because adjacent Y values now map to the same reduced color value.
- Repeat the above tests with two, three, four, and five
lsr
instructions in a row. Describe and explain the effect in each case.With each LSR instruction, the range of possible values in the accumulator (A) is reduced exponentially: one LSR reduces the values from 256 to 128, two LSR reduce it to 64, three to 32, four to 16, and five to 8. Adding more LSR instructions decreases the number of colors displayed on the screen and widens the stripes. - Repeat the tests usingSimilar to the effect of LSR but in the opposite way.
asl
instructions instead oflsr
instructions. Describe and explain the effect in each case. - Revert to the original code.
- The original code includes one
iny
instruction. Test with one to five consecutiveiny
instructions. Describe and explain the effect in each case. Note: it is helpful to place the Speed slider is on its lowest setting (left) for these experiments.Adding multiple INY instructions increases Y faster, causing the loop to "skip" over certain offsets, leaving gaps in the pattern. - Revert to the original code.
Challenges (Optional, Recommended)
- Set all of the display pixels to the same colour, except for the middle four pixels, which will be drawn in another colour. These hard-coded addresses are a simple way to solve this problem. However, I think a better approach might be to solve it by dynamically calculating the addresses...
LDX #$00 LDA #$03 LOOP: STA $0200, X ; Store the main color in the first row at column X STA $0300, X ; Store the main color in the second row at column X STA $0400, X ; Store the main color in the third row at column X STA $0500, X ; Store the main color in the fourth row at column X INX ; Increment the X index to move to the next column BNE LOOP ; Repeat the loop until X reaches $00 again (end of 256 columns) ;set the middle 4 pixels to a different color LDA #$04 STA $03EF ; Set the second row, column 15 (middle pixel 1) STA $03F0 ; Set the second row, column 16 (middle pixel 2) STA $040F ; Set the third row, column 15 (middle pixel 3) STA $0410 ; Set the third row, column 16 (middle pixel 4)
- Write a program which draws lines around the edge of the display:
- A red line across the top
- A green line across the bottom
- A blue line across the right side.
- A purple line across the left size.
;red line across topldy #$00toploop: lda #$02; redsta $0200,y; set pixel colour at the topiny; increment indexcpy #$20bne toploop; continue until done the page (256 pixels);green line across bottomldy #$00buttomloop: lda #$05; greensta $05e0,yinycpy #$20bne buttomloop; continue until done the page (256 pixels);purple line accrose left sideldy #$00leftloop: lda #$04; purplesta $0200,ysta $0300,ysta $0400,ysta $0500,ytya; Transfer Y to accumulatorclc; Clear carry flagadc #$20; Move to the next line (step by 32 bytes for vertical alignment)tay; Transfer accumulator back to Ybne leftloop; continue until done the page (256 pixels);blue line accrose right sideldy #$00rightloop:lda #$06 ;bluesta $021f,ysta $031f,ysta $041f,ysta $051f,ytya; Transfer Y to accumulatorclc; Clear carry flagadc #$20; Move to the next line (step by 32 bytes for vertical alignment)tay; Transfer accumulator back to Ybne rightloop; continue until done the page (256 pixels)
Comments
Post a Comment