intro to C64 programming
the mos technology 6510
registers
the 6502 has 6 of them:
3 can be directly manipulated
- 8-bit accumulator (A)
- 8-bit x-index register (X)
- 8-bit y index register (Y)
3 can be indirectly manipulated
- 8-bit status register (P)
- 8-bit stack pointer (S)
- 16-bit program counter (PC)
the accumulator (A)
// A is used for most mathematical operations on data
// some examples of instructions affecting A:
lda // LoaD A with a value
sta // STore A somewhere
adc // ADd (with Carry)
sbc // SuBtract (with Carry)
// load A with 5
lda #5
// store A in memory location $1000
sta $1000
// load A from memory location $1010, add 3 to it, and store it in memory location $1020
lda $1010
adc #3
sta $1020
have a go at working with the A register
1. store 15 at memory address $2000
2. store 28 at memory address $2001
3. add the previous two values together
4. store the result at $2002
verify your program's operation by using C64 debugger or the VICE monitor
$2000 should contain $0F (15)
$2001 should contain $1C (28)
$2002 should contain $2B (43)
example solution
BasicUpstart2($1000)
* = $1000
lda #15
sta $2000
lda #28
sta $2001
lda $2000
adc $2001
sta $2002
.break
jmp *
the X and Y index registers
// X and Y are mostly used for indexing into a list of data
// some instructions that affect X and Y:
LDX // LoaD X with a value
LDY // LoaD Y with a value
STX // STore the value in X in memory
STY // STore the value in Y in memory
INX // INcrement X
INY // INcrement Y
// indexing example
* = $2000 // put this code at memory address $2000
LDX #2 // load X with 2
LDA $1000, x // load A with tens_list[2]
* = $1000 // put this data at memory address $1000
.byte 10, 20, 30, 40, 50
looping with X/Y
// the index registers are also good for looping
// loop a piece of code 5 times:
ldx #0
loop:
/* code you want to loop goes here */
inx // INcrement X by one
cpx #5 // ComPare X with 5
bne loop // Branch if Not Equal; goto `loop`
/* all done! */
// there's a more efficient way that saves two bytes (from `cpx #5`)
ldx #5
loop:
/* code you want to loop goes here */
dex // DEcrement X by one
bne loop // Branch if Not Equal (to 0); goto `loop`
/* all done! */