6502 Assembly Language Exercise
6502 Assembly Language Exercise
com
The following exercises are designed to introduce you to the most common 6502 instructions
that we’ll use in the beginning of our course.
I am aware that these exercises will sound a bit pointless, and it might seem like we are simply
moving values around the machine. But don’t worry; very soon we’ll put all these instructions
together and create something more meaningful, like painting pixels on the screen or keeping
the score of a player or the number of enemies in our games.
For now, all I want is for you to get familiarized with the basic instructions of the 6502 CPU.
These exercises will cover things like different addressing modes, loading values into registers,
storing values in memory, checking processor flags, and creating loops.
Exercise 1
Your goal here is to simply load the processor registers A, X, and Y with some values.
processor 6502
seg Code ; Define a new segment named "Code"
org $F000 ; Define the origin of the ROM code at memory address $F000
Start:
; TODO:
; Load the A register with the literal hexadecimal value $82
; Load the X register with the literal decimal value 82
; Load the Y register with the value that is inside memory position $82
org $FFFC ; End the ROM by adding required values to memory position $FFFC
.word Start ; Put 2 bytes with the reset address at memory position $FFFC
.word Start ; Put 2 bytes with the break address at memory position $FFFE
Exercise 2
Your goal here is to just store some values into zero-page memory positions.
processor 6502
seg Code ; Define a new segment named "Code"
org $F000 ; Define the origin of the ROM code at memory address $F000
Start:
; TODO:
; Load the A register with the hexadecimal value $A
; Load the X register with the binary value %11111111
; Store the value in the A register into memory address $80
; Store the value in the X register into memory address $81
org $FFFC ; End the ROM by adding required values to memory position $FFFC
.word Start ; Put 2 bytes with the reset address at memory position $FFFC
.word Start ; Put 2 bytes with the break address at memory position $FFFE
Exercise 3
processor 6502
seg Code ; Define a new segment named "Code"
org $F000 ; Define the origin of the ROM code at memory address $F000
Start:
; TODO:
; Load the A register with the literal decimal value 15
; Transfer the value from A to X
; Transfer the value from A to Y
; Transfer the value from X to A
; Transfer the value from Y to A
org $FFFC ; End the ROM by adding required values to memory position $FFFC
.word Start ; Put 2 bytes with the reset address at memory position $FFFC
.word Start ; Put 2 bytes with the break address at memory position $FFFE
Exercise 4
This exercise is about adding and subtracting values. Adding and subtracting are math
operations that are done by the processor ALU (arithmetic-logic-unit). Since the ALU
can only manipulate values from the (A)ccumulator, all these additions and subtractions
must be performed with the values in the A register.
processor 6502
seg Code ; Define a new segment named "Code"
org $F000 ; Define the origin of the ROM code at memory address $F000
Start:
; TODO:
; Load the A register with the literal decimal value 100
org $FFFC ; End the ROM by adding required values to memory position $FFFC
.word Start ; Put 2 bytes with the reset address at memory position $FFFC
.word Start ; Put 2 bytes with the break address at memory position $FFFE
Exercise 5
The ADC and SBC instructions can also be used with different addressing modes. The
above exercise used ADC with immediate mode (adding a literal value directly into the
accumulator), but we can also ask ADC to add a value from a (zero page) memory
position into the accumulator.
processor 6502
seg Code ; Define a new segment named "Code"
org $F000 ; Define the origin of the ROM code at memory address $F000
Start:
; TODO:
; Load the A register with the hexadecimal value $A
; Load the X register with the binary value %1010
; Store the value in the A register into (zero page) memory address $80
; Store the value in the X register into (zero page) memory address $81
org $FFFC ; End the ROM by adding required values to memory position $FFFC
.word Start ; Put 2 bytes with the reset address at memory position $FFFC
.word Start ; Put 2 bytes with the break address at memory position $FFFE
Exercise 6
This exercise covers the increment and decrement instructions of the 6502.
processor 6502
seg Code ; Define a new segment named "Code"
org $F000 ; Define the origin of the ROM code at memory address $F000
Start:
; TODO:
; Load the A register with the decimal value 1
; Load the X register with the decimal value 2
; Load the Y register with the decimal value 3
; Increment X
; Increment Y
; Increment A
; Decrement X
; Decrement Y
; Decrement A
This exercise covers the increment and decrement using zero-page addressing mode.
The zero-page addressing mode helps us directly increment and decrement values
inside memory positions. The “zero page” in the 6502 are addresses between 0 and 255.
These addresses are special for the 6502 processor because we can store them using
only 1 byte (8 bits), which also means they can be performed relatively fast by the CPU.
processor 6502
seg Code ; Define a new segment named "Code"
org $F000 ; Define the origin of the ROM code at memory address $F000
Start:
; TODO:
; Load the A register with the decimal value 10
; Store the value from A into memory position $80
Exercise 8
Your goal here is to create a loop that counts down from 10 to 0. You should also fill the
memory addresses from $80 to $8A with values from 0 to A.
$80 $81 $82 $83 $84 $85 $86 $87 $88 $89 $8A
0 1 2 3 4 5 6 7 8 9 A
processor 6502
seg Code ; Define a new segment named "Code"
org $F000 ; Define the origin of the ROM code at memory address $F000
Start:
ldy #10 ; Initialize the Y register with the decimal value 10
Loop:
; TODO:
; Transfer Y to A
; Store the value in A inside memory position $80+Y
; Decrement Y
; Branch back to "Loop" until we are done
Your goal in this exercise is to create a simple loop that goes from 1 to 10. If possible, try
using the CMP instruction. This instruction that can be used to compare the value of
the accumulator with a certain literal number. Once the comparison is done, the
processor flags will be set (zero if the compared values are equal, non-zero if different).
processor 6502
seg Code ; Define a new segment named "Code"
org $F000 ; Define the origin of the ROM code at memory address $F000
Start:
lda #1 ; Initialize the A register with the decimal value 1
Loop:
; TODO:
; Increment A
; Compare the value in A with the decimal value 10
; Branch back to loop if the comparison was not equals (to zero)