Lab 1 - 6502 Assembly Language Lab
Lab 1 - 6502 Assembly Language Lab
Origin code:
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 #$07 ; colour number
ldy #$00 ; set index to 0
loop: sta ($40),y ; set pixel colour at the address (pointer)+Y
iny ; increment index
bne loop ; continue until done the page (256 pixels)
inc $41 ; increment the page
ldx $41 ; get the current page number
cpx #$06 ; compare with 6
bne loop ; continue until done all pages
Testing the code:
Calculating Performance:
Execution time:
As per my finding, the only way to reduce the execution time is reducing to use of BNE command. It takes the most cycles. Therefore, I tried to fill the bitmap four times a loop to reduce the use of BNE command. It is 20% faster.
Another way to reduce the use of BNE command is to use more pointer. I used 4 pointers pointing to each page and filling all the page at the same time. In this case, we can only one loop to finish the operation. It is 31% faster.
After a few attempts, I found that using Y-indexed absolute addressing mode for STA command will only cost 5 cycles, while using pointer takes 6 cycles. Also, the code look more simple. It is 50% faster!!!
Furthermore, I found that the fastest way is to store the color into the address by absolute addressing mode one by one from $0200 to $05FF. For example:
LDA #$07
STA $0200
STA $0201
STA $0202
...
STA $05FF
LDA #$07 will take 2 cycles and each STA $nnnn will take 4 cycles.
2 cycles * 1 + 4 cycles * 1 * 256 * 4 pages = 4098 cycles
It only takes 4098 cycles. However, it is impossible for us to write 1024 lines of command with STA $nnnn format.
Modifying the Code:
- Change the code to fill the display with light blue instead of yellow.
- Add this instruction after the loop: label and before the sta ($40),y instruction: tya
- What visual effect does this cause, and how many colours are on the screen? Why?
The bitmap displayed a vertical colored line. There are 16 colors in total, repeated once. I believe it is because TYA command transfers the Y value to accumulator. We also increase the Y value in each loop. Therefore, the color change from $0 Black to #f light grey. It only shows 16 color because there are only 16 colors in 6502.
- Add this instruction after the tya: lsr
- What visual effect does this cause, and how many colours are on the screen? Why?
The vertical colored lines is two pixels per column and there are only 16 color with no repeated column. It is because LSR will shift all the bits to right by 1 bit. In this case, we are shifting the value in the accumulator which means the color code. The result remain unchanged for every two addition to Y because the rightmost bit has been shifted out. For example, 00000010 and 000000011. They are representing 2 and 3. However, the result will be the same when we do the LSR, 00000001 and 00000001. So, each color will occupy two column of pixels.
- Repeat the above tests with two, three, four, and five lsr instructions in a row. Describe and explain the effect in each case.
There is something special when we use 5 LSR commands. The bitmap only shows 8 colors. Since we have 5 LSR commands, each color will display on 2^5 = 32 pixels, 1 row. After the eighth color, 1 page has been filled, 32 pixels * 8 rows = 256. Y will start from 0 again when we start to fill the next page. So, each page can only display 8 colors and repeated in 4 pages.
- Repeat the tests using asl instructions instead of lsr instructions. Describe and explain the effect in each case.
When we use ASL command instead of LSR command, the colors displayed in a column at first. Then, it only showed black in the entire bitmap from fourth ASL command. ASL is the opposite of LSR. It shift all the bit to left by 1 bit. As I mentioned before, each bit contain 2 values, 0 and 1. So, each ASL will make the color jump 2 color codes. For example, 1 ASL command will make the color jumping from $0 Black to $2 Red, $2 Red to $4 Purple, and so on; 2 ASL commands will make the color jumping from $0 Black to $4 Purple, $4 Purple to $8 Orange, and so on. It will change the color code by 2^n, with n equal the number of ASL commands. From the 4th ASL, it jumps 2^4 = 16 colors. However, we only have 16 color in 6502. So, it will be always black color after we have more 3 ASL command.
- The original code includes one iny instruction. Test with one to five consecutive iny instructions. Describe and explain the effect in each case.
For each odd number of INY command we use, the bitmap will be fully filled. For each even number of INY command we use, the color will be filled with n - 1 pixels of interval, with n equal the number of INY command.
For odd number of INY command, it will fill the entire bitmap because it will reach each number between 0 to 255 at some points when it add up to the number of 255 x n, with n equal the number of INY. For even number of INY command, it is always divided perfectly. This is why it will show as a pattern.
Challenges:
- Set all of the display pixels to the same colour, except for the middle four pixels, which will be drawn in another colour.
LDA #$07
LDX #$00
LOOP: STA $0200, X
STA $0300, X
STA $0400, X
STA $0500, X
INX
BNE LOOP
LDA #$02
STA $03EF, X
STA $03F0, X
STA $040F, X
STA $0410, X
- Write a program which draws lines around the edge of the display:
A red line across the top
A blue line across the right side.
LDY #$06
LDX #$1F
LOOP: TYA
STA $0200, X
STA $0300, X
STA $0400, X
STA $0500, X
TXA
ADC #$20
TAX
CPX #$1F
CLC
BNE LOOP
A purple line across the left size.
LDY #$04
LDX #$00
LOOP: TYA
STA $0200, X
STA $0300, X
STA $0400, X
STA $0500, X
TXA
ADC #$20
TAX
CPX #$00
CLC
BNE LOOP
Reflection:
Assembly language looks simple, but it is harder than I thought. Most of the operation is manipulating the accumulator, however, it is not convenience when we need to do some arithmetic. We need to load the value into accumulator all the time. Furthermore, the address are work on hexadecimal. I have to calculate it everytime before I use it. I am so happy that the current programming language is not assembly language. More human likely programming language, like Python, is much more easier to work with.
Comments
Post a Comment