Assembly Language Code Analysis
Published: 2022-01-28 01:12:32

## Introduction
As we already know, **Machine language** is the only form of code that can be executed by computers. But, the series of 0 and 1 are not so friendly to the human eyes. **Assembly language**, on the other hand, is human-readable (albeit harder to understand as compared to other high-level languages!) and very closely correspond to machine language. 

In this post, we are going to examine a simple code snippet that is written in assembly language on the 6502 processor by using [a simulator](http://6502.cdot.systems/). The 6502 processor is a great way to study the assembly language as it is much simpler processor in comparison with modern processors. 

## Code Example
We will examine the following program that simply fills the display with yellow color. 
![screen_filled_with_yellow_color](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9msjkkyrhs2508372cz3.png)
 
**Code:**
```
        lda #$00	; set a pointer at $40 to point to $0200
	    sta $40
	    lda #$02
	    sta $41

	    lda #$07	; colour number

	    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
	    ldx $41		; get the current page number
	    cpx #$06	; compare with 6
	    bne loop	; continue until done all pages       
```

## Calculating Performance
Using this [6502 table](https://www.masswerk.at/6502/6502_instruction_set.html), we can easily calculate the number of cycles that is required to execute the above program. 

|       |             | Cycles | Cycle Count | Alt Cycles | Alt Count | Total |
|-------|-------------|:------:|:-----------:|:----------:|:---------:|:-----:|
|       | lda #$00    |    2   |      1      |            |           |   2   |
|       | sta $40     |    3   |      1      |            |           |   3   |
|       | lda #$02    |    2   |      1      |            |           |   2   |
|       | sta $41     |    3   |      1      |            |           |   3   |
|       | lda #$07    |    2   |      1      |            |           |   2   |
|       | ldy #$00    |    2   |      1      |            |           |   2   |
| loop: | sta ($40),y |    6   |     1024    |            |           |  6144 |
|       | iny         |    2   |     1024    |            |           |  2048 |
|       | bne loop    |    3   |     1020    |      2     |     4     |  3068 |
|       | inc $41     |    5   |      4      |            |           |   20  |
|       | ldx $41     |    3   |      4      |            |           |   12  |
|       | cpx #$06    |    2   |      4      |            |           |   8   |
|       | bne loop    |    3   |      3      |      2     |     1     |   11  |

Assuming that the clock speed of the processor is 1MHz, we can also determine the time with the number of cycles we find.

| Total        | uS per Clock | Time             |
|--------------|--------------|------------------|
| 11325 cycles | 1            | 0.011325 seconds |

Let's take a look at another code snippet that also does the same job (i.e. fills the screen with the color) as the code above but with better performance.

**Alternative Code:**
```
	    lda #$07	; colour number
  	    ldx #$00	; set index to 0

loop:  	sta $0200,x	; set pixel at the absolute address + X
   	    sta $0300,x
   	    sta $0400,x
   	    sta $0500,x
   	    inx		    ; increment index
  	    bne loop        
```

The number of cycles that are required is drastically decreased as shown in the following table.

|       |             | Cycles | Cycle Count | Alt Cycles | Alt Count | Total |
|-------|-------------|:------:|:-----------:|:----------:|:---------:|:-----:|
|       | lda #$07    |    2   |      1      |            |           |   2   |
|       | ldx #$00    |    2   |      1      |            |           |   2   |
| loop: | sta $0200,x |    5   |     256     |            |           |  1280 |
|       | sta $0300,x |    5   |     256     |            |           |  1280 |
|       | sta $0400,x |    5   |     256     |            |           |  1280 |
|       | sta $0500,x |    5   |     256     |            |           |  1280 |
|       | inx         |    2   |     256     |            |           |  512  |
|       | bne loop    |    3   |     255     |      2     |     1     |  767  |

As we can see, the time that is required to execute the program is decreased by 43.46 percent.

| Total       | uS per Clock | Time             |
|-------------|--------------|------------------|
| 6403 cycles | 1            | 0.006403 seconds |

## Color Modification
By changing the color code in the program, we can change the color that fills the display.
```
lda #$0e	; change from color code #$07 
```
![screen_filled_with_light_blue_color](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9lkkfnp6o5hbxt80gjg0.png)
 
With a little bit of code changes, we can also do something like this!

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

	    lda #$07	; colour number

	    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
	    ldx $41		; get the current page number

        txa;		; transfer x to accumulator

	    cpx #$06	; compare with 6
	    bne loop	; continue until done all pages       
```

![screen_filled_with_four_colors](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2cm1i7fh86v8qnxgkjgu.png)

The logic is simple. The first quarter of the screen is filled with the color that is initialized with `#$07`. Whenever the program increments the page, the value stored in `X index` is transferred to the `Accumulator` and changes the color for each page.

## Conclusion
Learning Assembly language is not easy but helps to understand how the program works under the hood. Using Assembly language on 6502 processor, we learned to write a simple code to display the screen filled with color. We also learned how two different codes with the same result can lead to varying machine cycles. Additionally, we saw how to modify the code to display the screen filled with different colors. In [the next post](https://dev.to/seungwooji/assembly-language-code-analysis-part-2-2k3m), we are going to further explore the code above for other interesting results.