Skip to content

Instantly share code, notes, and snippets.

@sonictk
Created July 12, 2018 07:52
Show Gist options
  • Save sonictk/4d8db5bc3872c23f8d9e4420f7f8e0de to your computer and use it in GitHub Desktop.
Save sonictk/4d8db5bc3872c23f8d9e4420f7f8e0de to your computer and use it in GitHub Desktop.
default rel
bits 64
segment .text
global main
extern _CRT_INIT
extern ExitProcess
; Define constants to refer to the function arguments as offsets from RSP/RBP
a equ 0
b equ 8
foo:
push rbp ; Set up a stack frame for the function
mov rbp, rsp ; Continuing the linked list here, by pointing rbp to this stack frame
sub rsp, 32 ; Must align on 16 byte boundary
mov [rbp + a], rcx ; save parameter a (rcx)
mov [rbp + b], rdx ; save parameter a (rdx)
leave ; Undo the stack frame (it sets rsp to rbp, then pops rbp.)
ret ; return to calling procedure (main).
main:
call _CRT_INIT ; Needed since our entry point is not _DllMainCRTStartup. See https://msdn.microsoft.com/en-us/library/708by912.aspx
push rbp ; Set up a stack frame
mov rbp, rsp ; base pointer now points to stack frame
sub rsp, 32 ; Leave room for shadow parameters (4 for windows)
mov rcx, 100 ; First parameter to function
mov rdx, 200
call foo ; Invoke function
xor eax, eax ; return 0
leave
call ExitProcess ; On Windows, terminates the process.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment