Difference between revisions of "6502 Assembly Language Lab"

From CDOT Wiki
Jump to: navigation, search
(Lab 1)
m (Resources)
 
(16 intermediate revisions by the same user not shown)
Line 7: Line 7:
 
* [[6502]] Wiki Page
 
* [[6502]] Wiki Page
 
* [[6502 Emulator]]
 
* [[6502 Emulator]]
 +
* [[Assembler Basics]]
 
* [[6502 Emulator Example Code]]
 
* [[6502 Emulator Example Code]]
 
* Opcode/Instruction References
 
* Opcode/Instruction References
** [http://www.6502.org/tutorials/6502opcodes.html 6502 Opcodes with Register Definitions]
+
** On this Wiki
** [https://www.masswerk.at/6502/6502_instruction_set.html 6502 Opcodes with Detailed Operation Information]
+
*** [[6502 Instructions - Introduction]]
 +
*** [[6502 Addressing Modes]]
 +
** External
 +
*** [http://www.6502.org/tutorials/6502opcodes.html 6502 Opcodes with Register Definitions]
 +
*** [https://www.masswerk.at/6502/6502_instruction_set.html 6502 Opcodes with Detailed Operation Information]
 +
* [https://docs.google.com/spreadsheets/d/1a1-ZZ1opY8xcuUHNxj3YW75dxOPynSuP2-QGSvZGzYY/edit?usp=sharing Spreadsheet of bitmapped display memory locations]
  
 
== Lab 2 ==
 
== Lab 2 ==
Line 17: Line 23:
 
1. Open the [[6502 Emulator]] at http://6502.cdot.systems in another tab or window, keeping this lab open.
 
1. Open the [[6502 Emulator]] at http://6502.cdot.systems in another tab or window, keeping this lab open.
  
{{Admon/important|Save Your Work|The emulator '''does not''' save your work automatically. Remember to periodically save it to a file (copy-and-paste the code or use the <code>Save</code> button).}}
+
{{Admon/important|Save Your Work|The emulator '''does not''' save your work automatically. Remember to periodically save it to a file (copy-and-paste the code or use the <code>Save</code> button to create local files). Recommendation: save your files to a directory, and use [https://git-scm.com/ git] to manage that directory.}}
  
 
=== Bitmap Code ===
 
=== Bitmap Code ===
 
2. The following code fills the emulator's bitmapped display with the colour yellow. Paste this code into the emulator:
 
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 at $40 to point to $0200
+
  lda #$00 ; set a pointer in memory location $40 to point to $0200
  sta $40
+
  sta $40 ; ... low byte ($00) goes in address $40
  lda #$02
+
  lda #$02
  sta $41
+
  sta $41 ; ... high byte ($02) goes into address $41
 
   
 
   
 
  lda #$07 ; colour number
 
  lda #$07 ; colour number
Line 31: Line 37:
 
  ldy #$00 ; set index to 0
 
  ldy #$00 ; set index to 0
 
   
 
   
  loop: sta ($40),y ; set pixel at the address (pointer)+Y
+
  loop: sta ($40),y ; set pixel colour at the address (pointer)+Y
 
   
 
   
 
  iny ; increment index
 
  iny ; increment index
  bne loop ; continue until done the page
+
  bne loop ; continue until done the page (256 pixels)
 
   
 
   
 
  inc $41 ; increment the page
 
  inc $41 ; increment the page
Line 42: Line 48:
 
   
 
   
  
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.  
+
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 ===
 
=== Calculating Performance ===
4. Calculate how long it takes for the code to execute, assuming a 1 MHz clock speed.
+
4. Calculate how long it takes for the code to execute, assuming a 1 MHz clock speed (Watch [https://web.microsoftstream.com/video/ed7aedf1-fe6f-4b72-bbf1-c9b4e6e80af9 this video] if you would like a refresher on how to do this). Also calculate the total memory usage for the program plus any pointers or variables.
around
+
 
5. Consider ways to decrease the time taken to clear the screen. Calculate the execution time of the fastest version of this program that you can create.
+
5. Consider ways to decrease the time taken to fill the screen with a solid colour. Calculate the execution time of the fastest version of this program that you can create. Challenge: the fastest version of this program is more than twice as fast as the original version shown above!
  
 
=== Modifying the Code ===
 
=== Modifying the Code ===
Line 70: Line 76:
 
=== Challenges (Optional, Recommended) ===
 
=== 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.
 
# Set all of the display pixels to the same colour, except for the middle four pixels, which will be drawn in another colour.
# Write a program which draws lines at the top and bottom of the display:
+
# Write a program which draws lines around the edge of the display:
 
#* A red line across the top
 
#* A red line across the top
 
#* A green line across the bottom
 
#* A green line across the bottom
 +
#* A blue line across the right side.
 +
#* A purple line across the left size.
  
== Write-Up ==
+
=== Write-Up ===
 
Post an entry on your blog describing your experiments in this lab. Follow the [[Blog Guidelines]]. Include code as text (and not screenshots), but feel free to include screenshots of the bitmapped display.
 
Post an entry on your blog describing your experiments in this lab. Follow the [[Blog Guidelines]]. Include code as text (and not screenshots), but feel free to include screenshots of the bitmapped display.
  
Line 82: Line 90:
 
# The results of the Optional sections, if you performed them, and your explaination for each observed result.
 
# The results of the Optional sections, if you performed them, and your explaination for each observed result.
 
# Your experiences with this lab -- your impressions of Assembly Language, what you learned, and your reflections on the process.
 
# Your experiences with this lab -- your impressions of Assembly Language, what you learned, and your reflections on the process.
 +
 +
Remember that labs are marked on a scale of 0-3:
 +
* 0 - Lab not completed, or significant errors.
 +
* 1 - Very basic completion, or some small errors.
 +
* 2 - Satisfactory completion of the lab.
 +
* 3 - Lab completed with additional experiments, investigation, research, or explanation. For example, you could do some of the optional sections in this lab, and include those in your blog writeup -- or you could create your own experiments.
  
 
Remember to follow the [[Blog Guidelines]] as you write.
 
Remember to follow the [[Blog Guidelines]] as you write.

Latest revision as of 18:22, 15 September 2022

Lab icon.png
Purpose of this 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.

This is a lab for the SPO600 course.

Resources

Lab 2

Setup

1. Open the 6502 Emulator at http://6502.cdot.systems in another tab or window, keeping this lab open.

Important.png
Save Your Work
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

4. Calculate how long it takes for the code to execute, assuming a 1 MHz clock speed (Watch this video if you would like a refresher on how to do this). Also calculate the total memory usage for the program plus any pointers or variables.

5. Consider ways to decrease the time taken to fill the screen with a solid colour. Calculate the execution time of the fastest version of this program that you can create. Challenge: the fastest version of this program is more than twice as fast as the original version shown above!

Modifying the Code

6. 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).

7. 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 (Optional, Recommended)

Go back to the bitmap code above, and try these experiments:

  1. Add this instruction after the loop: label and before the sta ($40),y instruction: tya
  2. What visual effect does this cause, and how many colours are on the screen? Why?
  3. Add this instruction after the tya: lsa
  4. What visual effect does this cause, and how many colours are on the screen? Why?
  5. Repeat the above tests with two, three, four, and five lsr instructions in a row. Describe and explain the effect in each case.
  6. Repeat the tests using asl instructions instead of lsr instructions. Describe and explain the effect in each case.
  7. Revert to the original code.
  8. The original code includes one iny instruction. Test with one to five consecutive iny 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.
  9. Revert to the original code.
  10. Make each pixel a random colour. (Hint: use the psudo-random number generator mentioned on the 6502 Emulator page).

Challenges (Optional, Recommended)

  1. Set all of the display pixels to the same colour, except for the middle four pixels, which will be drawn in another colour.
  2. 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.

Write-Up

Post an entry on your blog describing your experiments in this lab. Follow the Blog Guidelines. Include code as text (and not screenshots), but feel free to include screenshots of the bitmapped display.

Include in your blog post:

  1. An introduction, so that someone who happens across your blog will understand the context of what you're writing about.
  2. The results from the Calculating Performance and Modifying Code portions of the lab, including the code, a description of how the code works, and the results produced.
  3. The results of the Optional sections, if you performed them, and your explaination for each observed result.
  4. Your experiences with this lab -- your impressions of Assembly Language, what you learned, and your reflections on the process.

Remember that labs are marked on a scale of 0-3:

  • 0 - Lab not completed, or significant errors.
  • 1 - Very basic completion, or some small errors.
  • 2 - Satisfactory completion of the lab.
  • 3 - Lab completed with additional experiments, investigation, research, or explanation. For example, you could do some of the optional sections in this lab, and include those in your blog writeup -- or you could create your own experiments.

Remember to follow the Blog Guidelines as you write.