Skip to content

Instantly share code, notes, and snippets.

@doughgle
Last active January 26, 2018 10:32
Show Gist options
  • Save doughgle/9d188253336fbfd712e3bdb544ca36b8 to your computer and use it in GitHub Desktop.
Save doughgle/9d188253336fbfd712e3bdb544ca36b8 to your computer and use it in GitHub Desktop.
.file "example.c" # filename
.text # store in text section
.globl myfunc # exported symbol
.type myfunc, @function #
myfunc: # function name
.LFB0:
.cfi_startproc #### PROLOGUE
pushq %rbp # push base pointer (64bit) onto the stack
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp # start a new stack frame,
# move the value of stack pointer to base pointer
.cfi_def_cfa_register 6
#### PROLOGUE END
movl %edi, -20(%rbp) # move 32bit reg value onto stack
movq $0, -16(%rbp) # move the 64bit value 0 into the address pointed to by rbp -16
movl $0, -4(%rbp) # zero a few bytes...?
jmp .L2 # jump to label
.L5:
movl -4(%rbp), %eax # put 0 in eax
imull -20(%rbp), %eax # multiply src x dest ; i.e. value * 0
cltq # sign-extend the value in eax to fill rax
addq %rax, -16(%rbp) # rax + 0
movl -4(%rbp), %eax # reset eax
andl $1, %eax # bitwise AND 0b1 & eax
testl %eax, %eax
je .L3 # goto L3
movq globl_cnt(%rip), %rax # rdx = (&rip + globl_cnt)
subq -16(%rbp), %rax # result = rax - (&rbp - 16)
movq %rax, globl_cnt(%rip) # store result
jmp .L4
.L3: # globl_cnt is a global counter of the type “long int” external to the function.
movq globl_cnt(%rip), %rdx # offset globl_cnt bytes from intruction pointer and move value to rdx
movq -16(%rbp), %rax # move basepointer -16 bytes to rax
addq %rdx, %rax # rdx + rax
movq %rax, globl_cnt(%rip) # move result to offset instruction pointer
.L4:
addl $1, -4(%rbp) # (rbp add -4 bytes) + 1 ; i.e. increment some variable
.L2: #
movl -4(%rbp), %eax # move the value 4 bytes below the base pointer to eax register
cmpl -20(%rbp), %eax # compare the value 0x20 bytes below the base pointer with the value in eax register (0?)
jl .L5 # jump to L5 if value < 0
nop
popq %rbp # pop the parent stack frame address
.cfi_def_cfa 7, 8
ret # pop return address and jump there
.cfi_endproc
.LFE0:
.size myfunc, .-myfunc
.ident "GCC: (GNU) 6.1.0"
.section .note.GNU-stack,"",@progbits
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment