Skip to content

Instantly share code, notes, and snippets.

@nikita-petko
Last active August 24, 2022 21:43
Show Gist options
  • Save nikita-petko/d3bfaae21e8360afbc4986f8890ce76e to your computer and use it in GitHub Desktop.
Save nikita-petko/d3bfaae21e8360afbc4986f8890ce76e to your computer and use it in GitHub Desktop.
x64 fast call stack allocation.
four_or_less_integer_parameters:
; our function to call:
; blah(int, int, int, int)
; allocate space for the 4 home volatile registers (rcx, rdx, r8 and r9) and aling to 16 bytes.
sub rsp, 0x28
; move your argument from left to right into registers
mov r9d, 0x10
mov r8d, 0x20
mov rdx, 0x30
mov rcx, 0x40
call blah
; free up space
add rsp, 0x28
; exit sub-routine
retn
four_or_less_mixed_type_arguments:
; our function to call:
; blah(int, float, int, int)
; still allocate space for 4 home registers (rcx, rdx, r8, r9) and align to 16 bytes
sub rsp, 0x28
; move your argument from left to right into registers
mov r9d, 0x10
mov r8d, 0x20
mov xmm1, dword ptr ds:[actual_float_address]
mov ecx, 0x30
call blah
; clean up stack
add rsp, 0x28
; exit the sub-routine
retn
five_or_more_integer_arguments:
; our function to call:
; blah(int, int, int, int, int)
; still allocate space for 4 home registers (rcx, rdx, r8, r9), 2 stack arguments and align to 16 bytes.
sub rsp, 0x38
; setup stack arguments and register arguments
mov dword ptr ss:[rsp+0x20], 0x10
mov r9d, 0x20
mov r8d, 0x30
mov rdx, 0x40
mov rcx, 0x50
call blah
; clean up stack
add rsp, 0x38
; exit sub-routine
retn
two_or_less_variables:
; allocate space for two QWORDs and another QWORD to align to 16 bytes.
sub rsp, 0x18
; write your local variables
mov qword ptr ss:[rsp], 0x10
mov qword ptr ss:[rsp+0x8], 0x20
; free up the allocated space
add rsp, 0x18
; exit the sub-routine
retn
four_or_less_variables
; allocate space for 4 QWORDs and another QWORD to align to 16 bytes.
sub rsp, 0x28
; write your local variables
mov qword ptr ss:[rsp], 0x10
mov qword ptr ss:[rsp+0x8], 0x20
mov qword ptr ss:[rsp+0x10], 0x30
mov qword ptr ss:[rsp+0x18], 0x40
; free up allocated space
add rsp, 0x28
; exit the sub-routine
retn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment