6502 - Assembly Language Practice
Tags: assembly
Published: 2021-10-01 01:44:59

In this blog, I would like to present simple 6502 assembly code and give a brief explanation/comments on how it works.
The Bitmap code is the basic code I use, which will paint the whole page to yellow. And I will try to create line across the page locate on different places.  

## Bitmap 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

```

## Draw a green line across the top
```
lda #$00             ; set a pointer at $40 to point to $0200
sta $40
lda #$02
sta $41

lda #$05             ; set color green

ldy #$00             ; set index to 0
top:sta ($40), y         ; set pixel

iny                  ; increment index

cpy #$20            ; compare y value with 20. If the index is 20, end the loop

bne top    

```
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9esj52q5bkxtpovzm1pf.png)
 

## Draw a blue line across the bottom
```
lda #$05
sta $41   
lda #$03             ; set color blue

ldy #$e0             ; set the index to last line

bottom:  sta ($40), y      ; set pixel
iny                      ; increment index
bne bottom         ; continue until done the line
	      
```
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u5whu63zwzb1b76ouxkr.png)
 

## Draw a yellow line down the left side, and a purple line down the right side
```
lda #$00             ; set a pointer at $40 to point to $0200
sta $40
lda #$02
sta $41

both_side: lda #$07         ;set the color yellow
        
sta ($40), y  

tax                       ; transfer A to X
tya                       ; transfer Y to A
clc                       ; clear carry flag to do ADC (add with carry)
adc #$1f              ; add with #$1f to indicates the last line

tay                       ; transfer A to Y
txa                       ; transfer X to A

lda #$04              ; set the color purple
     
sta ($40), y          ; 
iny                       ; increment index y
bne both_side      ; continue until done the page

inc $41                ; increment the page
ldx $41                ; get the page
cpx #$06             ; compare with 6, the last page is 05, when it reaches to 06, it means all pages are already painted
bne both_side       ; continue until done all page

```

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m3bz234ydel5vakzp3nq.png)

## Thoughts about 6502 Assembly Language
 Assembly language is the only low-level language I have learned so far. This language is a symbolic representation of machine language. Learning this language makes me understand more on how to communicate with machine. I think assembly language is a necessary bridge between the underlying hardware of a computer and the higher-level programming languages like Python. This language helps programmers to write human-readable code that is almost similar to machine language. After having a basic knowledge of this kind of machine language actually helps me to get rid of the fear of writing code. 

## Commands

### TYA Command
"tya" stands for "Transfer Y to A". It means that every time the value in Y register is increased, the value in A register is incremented as well. 

### LSA Command
"lsa" stands for "Logically Shift Right". This command shifts the color one bit to the right.

### ASL Command
"asl" stands for "Arithmetically Shift Left". ASL shifts all bits left one position.