Examples
glibc function call fprintf
Assembly
# Prints "25" to stdout
# build: gcc -static print_example.s
.globl main
.section .data
fmt_int:
.ascii "%d\n\0"
.section .text
main:
movq stdout, %rdi # arg1: FILE* stdout
movq $fmt_int, %rsi # arg2: format string
movq $25, %rdx # arg3: integer to print
movq $0, %rax # no floating-point args (required by ABI)
call fprintf # fprintf(stdout, "%d\n", 25)
mov $0, %rax
retLiner search
Assembly
.globl main
.section .data
array_size:
.quad 5
array:
.quad 1, 3, 5, 7, 9
target:
.quad 1
.section .text
main:
jmp linear_search
linear_search:
mov array_size, %rcx # counter
mov target, %rdi # target
mov $0, %rbx # zero our index i
loop_start:
mov array(,%rbx,8), %rax # if array[i] == target
cmp %rdi, %rax # jmp to found
je found # else loop
loop_control:
inc %rbx # i++
loop loop_start # dec %rcx (real counter)
jmp notfound # logically not needed, just safer
found:
mov %rbx, %rdi
jmp final
notfound:
mov $-1, %rdi
jmp final
final:
mov $60, %rax
syscall
retDisassembly of Go
func main(){
x := 1
y := x + 1
_ = y
}push %rbp # save the callers frame pointer
mov %rsp,%rbp # establish our frame
sub $0x10,%rsp # allocate 16 bytes on the stack for local variables
movq $0x1,0x8(%rsp) # x = 1
movq $0x2,(%rsp) # y = 2 (x+1 folded at compile time)
add $0x10,%rsp # relese the 16 bytes from the stack
pop %rbp # restore the callers frame pointer
ret