Created
October 1, 2011 06:45
-
-
Save jgreco/1255702 to your computer and use it in GitHub Desktop.
AVR assembly macros for making register safe call on stack functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; global registers: | |
; r0 - no man's land | |
; r1 - return address | |
; X (r26,r27) - frame pointer | |
;function frame: | |
;--------------------------- | |
; return address | |
; arg0 | |
; ... | |
; argn | |
;--------------------------- | |
; parent frame pointer | |
; for accessing the stack pointer | |
.equ SPH = 0x5E | |
.equ SPL = 0x5D | |
;call before loading args onto stack | |
.macro func_start_args | |
; save FP | |
push r26 | |
push r27 | |
; new FP | |
lds r26, SPL | |
lds r27, SPH | |
.endmacro | |
; load arguments | |
.macro fun_arg | |
push @0 | |
.endmacro | |
.macro fun_arg_i | |
pushi @0 | |
.endmacro | |
.macro fun_arg_16 | |
push16 @0, @1 | |
.endmacro | |
.macro fun_arg_16_i | |
push16i @0 | |
.endmacro | |
; get args (does not remove from stack) | |
.macro arg_get | |
push @0 | |
lpm @0, X+ | |
.endmacro | |
.macro arg_get16 | |
push @0 | |
push @1 | |
lpm @0, X+ | |
lpm @1, X+ | |
.endmacro | |
; unload args (call before returning for every arg) | |
.macro arg_unload | |
pop r0 | |
pop r0 | |
.endmacro | |
.macro arg_unload16 | |
pop r0 | |
pop r0 | |
pop r0 | |
pop r0 | |
.endmacro | |
; call function | |
.macro func_call | |
call @0 | |
; restore FP | |
pop r27 | |
pop r26 | |
.endmacro | |
; place at entry of function | |
.macro func_ent | |
pop r1 ; keep ret address for later | |
.endmacro | |
; place at exit of function | |
.macro func_ret | |
push r1 ; restore ret address | |
ret | |
.endmacro |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment