SPO600 Lab1 (Pt.5) - Challenges Question 2 Modified solution and reflection

Challenge 2 - Write a program which draws lines around the edge of the display with 6502 processor instruction:

A red line across the top

A green line across the bottom

A blue line across the right side.

A purple line across the left side.

Modified solution

The previous solution was using indirect addressing/ pointer and nested loop. However, since the screen has only 4 pages, it could be more readable and efficient by just using direct addressing and a single loop for the line across the left side/ right side.

Here is the revised solution.
;add a purple line accrose left side
        ldy #$00	; set index to 0
leftloop:  lda #$04 ;purple
        sta $0200,y
        sta $0300,y
        sta $0400,y
        sta $0500,y
 	tya
        clc
 	adc #$20
	tay
 	bne leftloop

 ;add a blue line accrose right side
 	ldy #$00	; set index to 0
rightloop: lda #$06 ;purple
        sta $021f,y
 	sta $031f,y
 	sta $041f,y
 	sta $051f,y
 	tya
        clc
 	adc #$20
	tay
 	bne rightloop
	
;add a red line across top 
;and a green line across bottom
 	ldy #$00	; set index to 0
xloop:  lda #$02	; red colour number
 	sta $0200,y	; set pixel colour at the top
        lda #$05        ; green colour number  
 	sta $05e0,y	; set pixel colour at the address $0000 +Y	
        iny		; increment index
        cpy #$20
 	bne xloop       ; continue until done the page (256 pixels)

Reflection:

In this lab, I experienced how to code with assembly language. One important consideration to code efficiently is to make good use of the X, Y register and the accumulator base on their functionality. Pointer could be helpful during the coding, but sometime it could cause inefficiency. It is predictable that when the program is more complex, more pointer will be used, but we should still reasonably make good us of the X,Y registers and the accumulator to enhance efficiency. 

Meanwhile, I found it interesting that we can us 2-bytes pointer in assembly language. We put the low byte on the first address and high byte on the second address. Now, we can use indirect addressing method to use the stored 2-bytes address by using the address of the first byte located. Although sometime indirect addressing is not efficient as direct addressing, indirect addressing is still extremely helpful for coding, for exampling in a loop. The offset feature for the STA instruction is also great and helpful. I cannot wait to learn more about assembly language.

Comments

Popular posts from this blog

SPO600 Project Stage 1 (Pt.1) - Create a GCC Pass

SPO600 Project Stage 2 (Pt.1) - GCC pass locating clone function

SPO600 Project Stage 2 (Pt.2) - GCC pass locating clone function -modified version