Beginning 65816 Assembly
From SNESdev Wiki
Contents |
CPU Registers
On the 65816 the Accumulator and/or Index registers can be set to perform 16-bit or 8-bit operations.
Accumulator (A)
This register is used to hold data while doing memory operations.
Index (X/Y)
These are 2 other registers, they can extend memory operations by adding offsets to memory addresses. They can also hold any other data if you wish. They're capability is a bit limited compared to the accumulator though.
Status
The status register contains 8 bits of flags to tell the CPU how to operate.
nvmxdizc n : negative flag (set if the result of an operation has its MSB set)
v : overflow flag
m : 8-bit/16-bit accumulator ( 1=8-bit, 0=16-bit )
x : 8-bit/16-bit index (1=8-bit, 0=16-bit )
d : decimal mode (?)
i : interrupt control (1=disable interrupts)
z : zero flag (set if the result of an operation is zero)
c : carry flag (used for various operations)
Basic Assembly Language
Assembly language is the lowest-level language ( beside manually typing in hex digits in a binary, hehe ). It usually comes in a basic syntax like this:
instruction operand, operand, etc... ; comment
[instruction] is a code to represent one of the operations the CPU can perform. They are usually named with small 3 letter words. Here are a couple basic instructions for 65816:
LDA source Load Accumulator STA destination Store Accumulator
Here is an example of moving 1 byte of data to somewhere else.
lda $0001 ; load byte of memory at address $0001 into the accumulator ; a byte of data is now in the accumulator sta $0002 ; copy the byte to $0002 ; the byte of data was copied to $0002
Addition/Subtraction
Here are a few mathematical operations:
ADC source ; add with carry SBC source ; subtract with carry CLC ; clear carry flag SEC ; set carry flag
ADC will add the source operand to the accumulator AND it will add 1 more if the carry flag of the status register is set. If the accumulator overflows then it will set the carry flag, otherwise it will clear it. SBC will subtract the source operand from the accumulator AND it will subtract 1 more if the carry flag is cleared. If the accumulator goes past zero it will wrap and the carry flag will be cleared, otherwise it will be set.
Examples:
; $0003 = $0001 + $0002 lda $0001 ; load accumulator with memory at $0001 clc ; clear the carry flag (to avoid the 1 being added) adc $0002 ; add accumulator with memory at $0002 sta $0003 ; store the result
; $0003 = $0005 - $0010 lda $0005 sec ; set the carry flag (to avoid the 1 being subtracted) sbc $0010 ; subtract value sta $0003 ; store the result
Program Flow
To get some real work done in assembly language you must break down large operations into very simple tasks to be written in asm (asm is short for assembly language).
TO BE CONTINUED... by someone else ;)

