top of page
Search
  • skhan4059

Intro to 6502 Assembly

Welcome back to another part of my blog, for anybody new to the blog, I will post the link to my first blog to give everyone a context as to what I am doing here and why.


Link to First Blog:


This time we are dealing with 6502 assembly code, we have been given some 6502 assembly code and we will deconstruct it and analysis it to understand the commands. The source code is here:

	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

and a reference page to follow along with the lab is right here:


I was tasked with analyzed the speed of which I have done using an excel sheet to calculate the amount of time it takes using the assumption of a 1MHz clock speed. Clock speed is the amount of times the system synchronizes per second, 1MHz is 1,000,000 cycles per second. tallying up all the instructions and the amount of cycles by referencing a 6502 opcode documentation. In short the total amount of cycles that I calculated for this sample code was 11335 cycles, which when we convert the cycles to seconds, is 0.011335 seconds. The excel sheet I am using is able to calculate the time it takes with different clock speeds as well.


One thing I was asked was how I could possibly make optimizations to the runtime of the code, to which I thought of what would most likely be the most bare bones way to display color on the screen would be to manually load the number into every single individual memory location which would theoretically give us a cycle count of 4098, this would give us a time of 0.004098 seconds and though extremely impractical, is much faster in terms of speed.


Now we will be modifying the code to test our understanding of assembly, the first task I was given was to modify the color of the screen change to a light blue color. Easy, all we need to to is simply change the value in the accumulator on line 06 to the value of $0e which represents that color.


The resultant code:


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

	lda #$0e	; 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

Resultant Display:



The second task we need to do is to modify the code so that a new color is used for each page of the display. Now I thought of a few ways I should do this, but the simplest way was most likely to store the color as a variable, and then use a pointer to call it into the accumulator and increment the number in memory every time the page is changed.


The resultant code:



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

		lda #$02	; colour number
		sta $45	; color location

		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 $45	; increment color by one
		lda $45	; load accumulator with new color
		inc $41	; increment the page
		ldx $41	; get the current page number
		cpx #$06	; compare with 6
		bne loop	; continue until done all pages

Resultant Display:



That concludes the task for this blog, I hope everyone is learning as much as I am, I find it really interesting how programs need to find a balance between optimizing runtime and programming efficiency and obviously many many other things. I am finding the 6502 architecture rather interesting and learning quite a bit of how to manage my resources and how to write very basic assembly code. Thank you for joining me this time and I hope to see you on my next blog which will be on 6502 mathematical operations. Links to all the resources I used will be below.




Clock Speed Analysis Spreadsheet:



7 views0 comments

Recent Posts

See All
bottom of page