Assembly

See atmel-0856-avr-instruction-set-manual.pdf>page=10.

The Stack

If you are using the stack, the stackpointer should be initialized like this:

LDI R16, HIGH(RAMEND)
OUT SPH, R16
LDI R16, LOW(RAMEND)
OUT SPL, R16
PUSH

Push data to the stack and decrease the stack pointer by one.

PUSH register
POP

Load data from the stack and increase the stack pointer by one.

POP register

Subrutines

“Functions” in assembly.

CALL

Call a subrutine. The stack pointer is reduced by two.

CALL subrutine
RCALL

Like >CALL by with relative a jump instead of absolute.

RCALL subrutine
RET

Return from the subrutine. This uses the stack to know where it came from.

RET

Directives

Directives are resolved by the assembler, and therefore take zero clock cycles to resolve in the processor.

.EQU

A name will be substituted with a number by the assembler.

.EQU name=nr
.SET

Same as >.EQU but can be reassigned.

.SET name=address
.DEF

Alias for a register.

.DEF name=register
.ORG

Sets the code address. The program will now execute from here.

.ORG address
.INCLUDE

Include other files. Mostly used to define values with >.EQU.

.INCLUDE file

Instructions

LDI - Load Immediate

Load a hardcoded value to a register.

Opcode: $1110\ \text{kkkk}\ \text{dddd}\ \text{kkkk}$

LDI reg value
LDS - Load from Data Space

Loads an 8-bit number into a register.

Opcode: $1001\ 000\text{d}\ \text{dddd}\ 0000\ \text{kkkk}\ \text{kkkk}\ \text{kkkk}\ \text{kkkk}$

MOV

Copy the content of a register to another.

MOV addr reg

Opcode: $0010\ 11\text{rd}\ \text{dddd}\ \text{rrrr}$

INC - Increment

Increment a value in a register

INC Rd

Opcode: $1001\ 010\text{d}\ \text{dddd}\ 0011$

ADD

Add two registers togeather and place them in a third register.