SPO600 - LAB 1: 6502 - Assembly Language Lab (Part 2) and Reflection

This blog discusses my experience with the 6502 assembly language through a series of experiments aimed at manipulating a bitmap display. In this lab, I investigated the effects of various assembly instructions on screen output, focussing on how shifting and incrementing values alter the colours displayed. I also completed additional challenges such as changing specific pixels to different colours and drawing lines around the screen's edge.

Experiment

1. Add tya Before sta ($40),y

The first experiment involves placing the tya instruction before the sta ($40),y. This transfers the value from the Y register to the A register, which determines the colour used to fill the screen.


2, 

For each pixel, iny increments the Y register, and with tya, the value in the Y register is copied into the A register. The A register controls the pixel colour in the sta ($40),y instruction, so the screen is filled with colours based on the value of Y.

This means that the colour of each pixel is directly proportional to the Y value, resulting in a gradient effect across the screen, beginning with the lowest Y value at the top and progressing to the highest at the bottom.

I saw 16 distinct colors because only the lower 4 bits of the Y register is used for color.


3. Ading lsr After tya

I included the lsr instruction after tya. This shifts the bits in A to the right, effectively dividing its value (and thus Y) by two.

The right shift operation causes the colour to change more slowly, resulting in fewer distinct colours on the screen (because the value of A is reduced by half).


4.

The lsr instruction shifts the bits in A to the right, effectively halving its value. This limits the number of possible colours because some of the lower bits of the Y value are lost.

As a result, the screen had fewer distinct colours than if only tya is used. The colour gradient appeared less smooth as a result of the shift operation, which reduces many smaller colour variations.

Number of colours:

After one lsr, the number of distinct colours is reduced by half. The original system had 16 colours (based on 4 bits), I now only see 8.


5. Adding Multiple lsr Instructions

I repeated the experiment with two, three, four, and five lsr instructions in succession to see how additional shifting affects the display.

Add Two lsr Instructions


With two lsr instructions, the value of A is shifted right twice, meaning the value is now divided by 4. This reduce the number of distinct colors even further, resulting in wider bands of the same color across the screen.

The color transitions is even smoother, but with fewer color changes between each band.

Add three lsr Instructions


Three lsr instructions move the value of A to the right three times, dividing it by eight. This means that even fewer colours appear on the screen. The colour bandsbe significantly larger, with each colour taking up more screen space.

Four lsr instructions shift the value of A right four times, dividing it by 16. At this point, I only saw one colour on screen. 

Five lsr instructions cause the value of A to be shifted right five times, resulting in a division by 32. This further reduces the number of colours.

6. Replacing lsr with asl

Next, I replaced the lsr instruction with asl (arithmetic shift left), which moves the bits in A to the left, effectively doubling its value.

The screen transitioned colours more quickly. The colour values increased rapidly, resulting in a faster gradient and more frequent colour changes across the display.

Shifting left by one bit doubles the value in A, causing the colours to change faster across the screen. The colour bands will become narrower as the value increases at a faster rate.

The screen is likely to cycle through colours faster, with more rapid transitions.

Effect of Two Asl: Shifting left twice multiplies the value in A by four. The colours change even faster across the screen, creating a more rapid cycling effect.

Effect of Three asl: Three shifts multiply the value by 8, causing the screen to display even faster colour transitions and very narrow colour bands.

Number of colours:

The number of colours remains constant (depending on the system's colour depth), but colour transitions occur more quickly because the value in A increases faster.

7. Revert to the original code


8. Testing Multiple iny Instructions

I experimented with using multiple iny instructions in a row. Each additional iny instruction increases the Y register by one, so multiple iny instructions result in more pixels skipped in each loop.


With two iny instructions, the screen displayed colour bands that were further apart, resulting in gaps between the coloured pixels. As I increased the number of iny instructions, the gaps became wider.

Adding more iny instructions caused the program to skip pixels, resulting in fewer coloured pixels but more space between them. This experiment demonstrated how adjusting the Y register's increment rate affects the density of coloured pixels on the screen.

9. Revert to original code

Challenge:

 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 $FE        ; Sets random colour (number) to accumulator
    ldx #$06    ; get the current page number
    ldy #$00    ; set index to 0

loop:   

    STA ($40), y
    iny
    BNE loop

    inc $41     ; increment the page
    cpx $41         ; compare current page to x (0600)
    bne loop    ; continue until done all pages

    ADC #$01    ; Add one to the colour code

    STA $03EF   ; Storing the new coloured pixels at middle location
    STA $03F0
    STA $040F
    STA $0410

My solution to this challenge was to manually add each of the middle pixel locations and assign them +1 to the colour chosen 





REFLECTION - My Experience with the 6502 Assembly Language Lab

This lab provided valuable hands-on experience with assembly language, allowing for a better understanding of how computers work at the hardware level. Working with 6502 assembly was a stark contrast to higher-level programming languages, as it required precise control over memory, registers, and loops.

Impressions of Assembly Language
1. Low-Level Control
Assembly programming provides direct access to memory and hardware, demonstrating how each instruction affects the processor at a granular level.
2. Precision and Simplicity
The instructions were simple, but the logic for managing memory addresses and flow control was complex. This highlighted the importance of careful planning and attention to detail.

What I Learned
1. Understanding the Role of Registers
One of the most fundamental lessons was how the 6502 registers (A, X, and Y) work and how important they are in programming logic. For example, using tya to transfer the value of the Y register to the A register taught me the significance of controlling data flow between registers. This is fundamentally different from how variables work in higher-level languages, but it is required for assembly programming.
2. Bitwise Operations
I had to deal with bitwise instructions like lsr (logical shift right) and asl (arithmetic shift left). These operations directly manipulate the bits in a register, and the results on the display are immediately visible. This was a more tactile approach to understanding bitwise operations, particularly how colour patterns changed when bits were shifted.
3. Memory Addressing Modes
Understanding how indexed indirect addressing (($40),y) works was essential. I discovered how a combination of memory pointers and index registers can point to specific memory locations. It made me appreciate how powerful yet simple these addressing modes are when working with the 6502's limited instruction set.
4. Debugging Assembly Code
Debugging in assembly is far more involved than in higher-level languages. I had to manually track the register values and understand how each instruction affected memory. This process taught me patience and precision when testing and analysing code.

Reflections on the Process.
1. Practicality and Relevance
While assembly programming can be tedious and error-prone, it is extremely useful, especially when working with hardware. This lab helped me understand why assembly is still important today, particularly in fields such as embedded systems, where efficient resource control is critical.
2. Importance of Planning and Precision
In assembly language, every instruction is important. In contrast to higher-level languages, where minor errors can be ignored, any error in assembly can cause your entire program to fail. This taught me the value of being methodical and precise in my reasoning and instruction flow.
3. Visual Feedback
Seeing the results of my code in the form of coloured pixels was a rewarding experience. It turned abstract concepts like memory management and register manipulation into visual representations. The color-changing experiments provided immediate feedback, making the learning process more engaging.

Conclusion
Overall, this lab has increased my comfort with assembly language programming and given me a better understanding of the underlying mechanisms of computers. Working with the 6502 assembly language was humbling, as it reminded me how complex even basic hardware operations can be. I now have a better understanding of how computers process instructions at the lowest level, as well as how assembly language serves as the foundation for everything else in higher-level languages.

This lab not only helped me improve my problem-solving skills, but it also taught me to think more critically about resource management, memory, and programming efficiency.

Nhận xét

Bài đăng phổ biến từ blog này

Project Stage 2 (Part 2): Set Up GCC for My Clone-Pruning Pass

SPO600 - Lab 3: 6502 Program Lab: Inches to Feet Converter

Project Stage 2 (Part 3): Implementing the Clone-Pruning Logic