Introduction to 6502 Assembly Language
Tags: lab1
Published: 2024-01-31 19:46:01

Welcome to the kickoff of our journey into the captivating world of assembly language programming! In this hands-on lab, dive into the basics of 6502 assembly language using the 6502 Emulator. Through experimentation and optimization, uncovered valuable insights that set the stage for mastering more complex assembly languages such as x86_64 and AArch64. Let's dive in and have fun!

**Setting Up**
Began the lab by opening the 6502 Emulator in a new tab or window. Kept this guide handy for reference as you navigate through the lab.
Note: Remember to save the work periodically using git or by copying the code to local files, as the emulator does not automatically save your progress.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a3uf73p5nc4iiqy2wknh.png)

**Bitmap Code**
Added the code provided in the lab into the emulator and then assembled and then ran on the site. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p267bob3dryxqp908tbq.png)

The code provided gives the color yellow. 

**Calculating Performance:**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ym5iueyeh8h7yrhzd9xp.png)

 Decrease the time taken to fill the screen with a solid colour:
To reduce the time needed to fill the screen with a solid color, we can optimize the loop structure and improve memory access efficiency.

lda #$00   ; set a pointer in memory location $40 to point $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

ldx #$00      ; set X register to 0
lda #$07      ; load accumulator with color number

fill_loop:
    sta ($40),x    ; set pixel color at the address (pointer)+X
    inx            ; increment index
    bne fill_loop  ; continue until X reaches 0 again
    inc $41        ; increment the page
    cpx #$06       ; compare with 6
    bne fill_loop  ; continue until done all pages


it utilize the X register to iterate through the 256 pixels of a page, eliminating the necessity for the Y register and its associated instructions.
By removing the Y register and its increment instruction, we save cycles.
Reducing the number of branching instructions is achieved by employing the X register to loop through the pixels.
The need to load Y with #$00 and increment it inside the loop has been eliminated.


Total cycles: 
5+(256×(5+2))+5+3×6=5+(256×7)+5+18=5+1797+5+18=1825 cycles


**Code now showing light blue instead of yellow**
modified the code by: lda #$e	; colour number

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vusx5y1hxx68nvfqph2c.png)

**Code now changing display with a different color on each page**

          lda #$00    ; set a pointer at $40 to point to $0200
        sta $40
        lda #$02
        sta $41

        lda #$06    ; colour number
        sta $10 ; store colour number to memory location $10

        ldy #$00    ; set index to 0

loop:    sta ($40),y    ; set pixel at the address (pointer)+Y

        iny ; increment index
        bne loop    ; continue until done the page

        inc $41 ; increment the page

        inc $10 ; increment the color number
        lda $10 ; colour number

        ldx $41 ; get the current page number
        cpx #$06 ; compare with 6
        bne loop ; continue until done all pages

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a1jshww324jbn31wao2k.png)







**Reflection:**


Lab 1 in assembly language programming (ALP) and the 6502 Emulator environment was an enlightening experience for a newcomer like myself. Setting up the emulator was straightforward, emphasizing the importance of file management and version control using tools like git. Exploring the provided bitmap code was both fascinating and challenging, requiring a deep understanding of each instruction's purpose and sequence. Calculating the code's performance metrics, including execution time and memory usage, was a new concept that demanded attention to detail and system constraints. Optimizing the code to decrease execution time underscored the significance of algorithmic efficiency and resource utilization in ALP. Modifying the code to experiment with different colors on the display expanded my understanding of memory manipulation and graphical output. Despite the challenges, Lab 1 sparked a sense of curiosity and eagerness to delve deeper into ALP and low-level programming. The hands-on nature of the lab provided valuable insights into system architecture and optimization techniques, laying a strong foundation for future exploration and learning in the realm of assembly language programming.