(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.

Resources

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.


6. Find one or more 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 is nearly twice as fast as the original version shown above!

we can change addressing mode by using "Absolute Indexed Addressing" instead of "Indirect Indexed Addressing" in this case. 


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


9. Make each pixel a random colour. (Hint: use the psudo-random number generator mentioned on the 
6502 Emulator page).


Experiments

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? 
    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.
  3. Add this instruction after the tyalsr

  4. 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.
  5. 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.
  6. Repeat the tests using asl instructions instead of lsr instructions. Describe and explain the effect in each case. 


    Similar to the effect of LSR but in the opposite way.
  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.



    Adding multiple INY instructions increases Y faster, causing the loop to "skip" over certain offsets, leaving gaps in the pattern.
  9. Revert to the original code.

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

;red line across top 
  ldy #$00
toploop: lda #$02; red 
      sta $0200,y; set pixel colour at the top
            iny; increment index
            cpy #$20
bne toploop; continue until done the page (256 pixels)

;green line across bottom
ldy #$00
buttomloop: lda #$05; green
           sta $05e0,y
           iny
           cpy #$20
bne buttomloop; continue until done the page (256 pixels)

;purple line accrose left side
  ldy #$00
leftloop: lda #$04; purple
             sta $0200,y       
       sta $0300,y
       sta $0400,y
       sta $0500,y
        tya; Transfer Y to accumulator
        clc; Clear carry flag
        adc #$20; Move to the next line (step by 32 bytes for vertical alignment)
        tay; Transfer accumulator back to Y
bne leftloop; continue until done the page (256 pixels)

 ;blue line accrose right side
  ldy #$00
rightloop:lda #$06 ;blue
               sta $021f,y
         sta $031f,y
         sta $041f,y
         sta $051f,y
         tya; Transfer Y to accumulator
         clc; Clear carry flag
         adc #$20; Move to the next line (step by 32 bytes for vertical alignment)
         tay; Transfer accumulator back to Y
bne rightloop; continue until done the page (256 pixels)

display:

My experiences with this lab

Through this lab1, I have gained a general understanding of assembly language. During the lab, I often had to refer to documentation to memorize specific instructions. Achieving a particular result can involve different coding approaches. However, depending on the scenario, such as aiming for shorter execution time, different methods need to be selected. For instance, when dealing with fixed memory locations, choosing Absolute Addressing saves time compared to accessing memory through indirect addressing. On the other hand, when working with complex data structures or dynamic arrays, using pointers can efficiently accomplish the task. 

Comments

Popular posts from this blog

(Lab4) GCC Build Lab

(lab2)6502 Math Lab