DEV Community

Cover image for Exploring 6502 Assembly Language - Lab 1 (Part Two)
Juro Zaw
Juro Zaw

Posted on

Exploring 6502 Assembly Language - Lab 1 (Part Two)

Introduction

Hi everyone, this is a continuation of my Lab 1. You can find the first part here. We will continue to experiment with our code of filling an entire bitmap with one color.

📌 Modifying the Code

Now, we will take our optimized code and then modifying then to perform certain actions.

Fill the Display with Light Blue Instead of Yellow

This is easy as I just need to change the color code to Light Blue's code which is $0E.

       LDA #$0E        ; Load accumulator with color number $0E (Light Blue)
        LDY #$00        ; Initialize Y register to 0

fill_screen:
        STA $0200,Y
        STA $0300,Y
        STA $0400,Y
        STA $0500,Y
        INY
        BNE fill_screen
Enter fullscreen mode Exit fullscreen mode

Fill Each Page with a Different Color

Since our optimized code works on each page, we just need to LDA a difference color on for each page. This will make sure the bitmap is filled with a different color each page.

        LDA #$0E
        LDY #$00
fill_screen:
        LDA #$0E        ; Page 1: Light Blue
        STA $0200,Y
        LDA #$02        ; Page 2: Red
        STA $0300,Y
        LDA #$05        ; Page 3: Green
        STA $0400,Y
        LDA #$03        ; Page 4: Cyan
        STA $0500,Y
        INY             ; Increment Y
        BNE fill_screen
Enter fullscreen mode Exit fullscreen mode

Make each pixel a random color

This is more easily achievable with the Initial Code. There is a one-byte pseudo-random number generator (PRNG) at $FE. We will just use this value instead of our color value.

    LDA #$00        
    STA $40        
    LDA #$02        
    STA $41         
    LDY #$00        

loop: 
    LDA $fe         ; get a random number from the PRNG at $FE
    STA ($40),y     
    INY             
    BNE loop        
    INC $41         
    LDX $41         
    CPX #$06        
    BNE loop       
Enter fullscreen mode Exit fullscreen mode

📌 Experiments

Based on the Initial Code, I will be trying on various experiments. They are as follows:

☑️ Add this instruction after the loop: label and before the sta ($40),y instruction: tya

 loop:
  tya
  sta ($40),y   ; set pixel colour at the address (pointer)+Y
Enter fullscreen mode Exit fullscreen mode

☑️ What visual effect does this cause, and how many colours are on the screen? Why?

The visual effect is multiple vertical lines of different colors. There are 32 total colors on screen. But only 16 is original, the next 16 are repeated colors. This is because the values in Y register are put into the Accumulator. Yis supposed to be for use with index but now Aand Y values become the same. So it corresponds to each color code starting from $00, $01, ...

Square with 32 vertical lines of color

☑️ Add this instruction after the tya: lsr

 loop:
  tya
  lsr
  sta ($40),y   ; set pixel colour at the address (pointer)+Y
Enter fullscreen mode Exit fullscreen mode

☑️ What visual effect does this cause, and how many colours are on the screen? Why?

The visual effect is the same vertical lines, but now each color occupies two horizontal pixel across, leaving the screen to be filled with 16 different colors. The lsr shifts all bits in Accumulator one position to the right, effectively dividing the value by 2. This halves the range of values and therefore number of 'colored lines'.

Square with 16 vertical lines of color

☑️ Repeat the above tests with two, three, four, and five lsr instructions in a row. Describe and explain the effect in each case.

  • Two lsr: Halves the number of colors to 8 to fill one line (or row). However, the remaining 8 colors take over the next horizontal line, thus creating a checkered pattern.

Square with checkered color

  • Three lsr: Halves the number of colors to 4 to fill one row. 4 colors fill one row, and it now takes 4 rows in total to fill 16 distinct colors. They then repeat.

Square with checkered color

  • Four lsr: Now only 2 colors fill one row. It takes 8 rows to complete each of the 16 colors. lsr halves it.

Square with checkered color

  • Five lsr: Theoretically, 1 color should fill each row and take 16 rows. But, since each page only takes 8 horizontal rows, once a new page starts, the inner loop starts again. Making the bitmap filled with 8 distinct colors.

Square with checkered color

☑️ Repeat the tests using asl instructions instead of lsr instructions. Describe and explain the effect in each case.

asl shifts all bits in the Accumulator left by one position, effectively multiplying by 2 each time.

Number of asl Number of Distinct Colors Visual Effect
1 asl 8 Vertical bands of even-numbered colors
2 asl 4 Even wider bands, skipping every 7 colors
3 asl 2 Alternating large bands of 2 colors
4 asl 1 Single solid color
5 asl 1 Single solid color

☑️ The original code includes one iny instruction. Test with one to five consecutive iny instructions. Describe and explain the effect in each case.

The iny instruction increments the Y register, which determines where the next pixel will be written. By increasing inyrepetitions, the spacing between colored pixels will increase.

Number of iny Visual Effect Explanation
1 iny The screen is filled with the chosen color. Each pixel is written sequentially, meaning there are no gaps.
2 iny Every second pixel is skipped, creating thin vertical line gaps between colored pixels. Since Y is incremented twice per loop iteration, only every second pixel is modified.
3 iny Every third pixel is filled in order. Creating a checkered pattern, which then keeps repeating until the whole page is filled Y increases by 3 each iteration, so two pixels are skipped between every colored pixel.
4 iny Same as 2 iny but with wider gaps Even-numbered incrementations will create vertical lines with gaps.
5 iny Same as 3 iny but with wider gaps Odd-numbered incrementations will create checkered pattern which fills the whole page in that pattern.

Conclusion

In conclusion, this lab is a very effective introduction to the 6502 Assembly language. What I've learned is a completely new programming language and that it takes time to get used to how it can be written. By performing this lab, I am now familiar with the addressing modes, difference between each register of the 6502, and the common instructions used in this assembly language.

Thank you so much for reading! 😄

Top comments (0)