The AVR instruction set is a machine language for Atmel AVR, a modified Harvard architecture 8-bit RISC chip microcontroller.

Resources

Subpages

Program Structure

.section .data
; global variables
 
.section .text
.global main ; entry point
 
main:
    ret
 
; other functions
 
.end

Data Sections

.section .data
number: .byte 42           ; Single byte with the value 42
result: .space 42          ;  42 bytes
array:  .byte 1, 2, 3, 4, 5, 6
s:      .string "42"       ; null-terminated string

General Purpose Registers

AVR has 32 general-purpose 8-bit registers from R0 to R31. All arithmetic and logic operations operate on those registers. Only load and store instructions access RAM.

Pointers

AVR assembly has three pointer registers for memory addressing. Those are mapped to two register pairs:

  • X (R27:R26)
  • Y (R29:R28)
  • Z (R31:R30) To point a pointer to a label, we can do something like:
.section .data
label: .byte 42
 
.section .text
.global asm_function
 
asm_function:
  ; point X to label
  ldi r27, hi8(label)
  ldi r26, hi8(label)
 
.end

Stack Pointer

The stack pointer is a special register stored in 0x3E:0x3D. It can be read with in and modified with out:

asm_function:
    ; load stack pointer to Y
    in r28, 0x3D     ; low byte
    in r29, 0x3E     ; high byte
 
    ; do stuff
    ; e.g. load the first value on the stack (excluding return address)
    ldd r0, Y+3
 
    ; store Y into stack pointer
    out 0x3D, r28
    out 0x3E, r29
    ret
.end