Skip to content

Instantly share code, notes, and snippets.

@afternoon
Created November 28, 2016 18:38
Show Gist options
  • Save afternoon/f5cf3f2cb894fae92653735dcde75462 to your computer and use it in GitHub Desktop.
Save afternoon/f5cf3f2cb894fae92653735dcde75462 to your computer and use it in GitHub Desktop.
.section __TEXT,__text,regular,pure_instructions
.macosx_version_min 10, 12
.globl _main
.p2align 4, 0x90
_main: ## @main
.cfi_startproc
## BB#0:
pushq %rbp
Ltmp0:
.cfi_def_cfa_offset 16
Ltmp1:
.cfi_offset %rbp, -16
movq %rsp, %rbp
Ltmp2:
.cfi_def_cfa_register %rbp
leaq L_.str(%rip), %rdi
xorl %eax, %eax
callq _printf
xorl %eax, %eax
popq %rbp
retq
.cfi_endproc
.section __TEXT,__cstring,cstring_literals
L_.str: ## @.str
.asciz "Hello World!\n"
.subsections_via_symbols
@afternoon
Copy link
Author

What does all this mean? What's the leaq call on line 16 doing to %rip? What are all the directives?!

@patshaughnessy
Copy link

Hi I'll take a crack at trying to explain this...

Lines 8, 13: Prepare a new stack frame (save the old base pointer, and set
the new base pointer to the current stack pointer)

Lines 16: Get the address of your string (lea = load effective address)
and put it into the rdi register as a parameter to printf. %rip is the instruction
pointer, and the code uses that to find the string literal below.

Line 17: Not sure why this is needed - I believe it sets eax to zero. Must be another parameter to printf

Line 18: Call printf

Line 19: Same thing here - set eax to zero, which is now the return value of the function

Lines 20,21: Restore the old stack frame and return to the caller

Line 26: The string literal referenced by leaq.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment