Assembly (AVR)
References
Cheat Sheet
Assembly Cheat Sheet
Commonly Seen Assembly Mistakes
-
Missing .text after .data. Machine code in .data instead
of in .text leads to infinite resets. If you see
"Initialized stdout." repeatdly or if you see the compile note:
"warning: internal error: out of range error" you are missing or
you have misplaced the .text directive.
-
Forgetting to save a register (observe caller-save / callee-save convention)
-
Using mov with an immediate (e.g., you meant ldi r18, 1, but used mov r18, 1)
-
Using cp or cpc with immediate (e.g., cp r18, 10 is wrong); you need two registers (e.g., cp r18, r20))
-
Forgetting to add ret to a function.
-
Incorrect order or number of pop instructions after push instructions.
For example:
push r18
push r19
; task here
pop r18 ; WRONG (you needed pop r19)
pop r19 ; WRONG (you needed pop r18)
-
Mixing up high and low bytes. For example, if you are using r24 and r25,
r24 will have the low byte and r25 the high byte.
-
Be careful with numeric labels and make sure you are jumping to the correct address.
-
Incorrect use of branch instruction (e.g., brlt (signed) vs. brlo (unsigned)).
- If you need to compare a register against a constant
(e.g., r25 and 0) you need to use cpi (notice the i).
For example, cpi r25, 0.
Using GDB Text User Interface (TUI)
To use TUI follow these steps:
- Run the program in gdb
- Set a breakpoint in a function (e.g., b main)
- tui enable
- layout asm
- layout regs
- Press c to continue
- Display the function code (l function_name)
- Use n to execute the next instruction
- Note: you may need to increase the terminal size in
order to see all the registers
Reference: https://ftp.gnu.org/old-gnu/Manuals/gdb/html_chapter/gdb_19.html