Skip to content

Instantly share code, notes, and snippets.

@mikesmullin
Last active June 13, 2016 11:38
Show Gist options
  • Save mikesmullin/6330894 to your computer and use it in GitHub Desktop.
Save mikesmullin/6330894 to your computer and use it in GitHub Desktop.
C to Linux x86-64 Assembly (ASM) examples
#include <stdio.h>
int example(int a, int b) {
return a + b + 3;
}
int main(void) {
printf("%i\n", example(1, 2));
return 0;
}
.globl example
.type example, @function
example:
pushq %rbp
movq %rsp, %rbp
movl %edi, -4(%rbp)
movl %esi, -8(%rbp)
movl -8(%rbp), %eax
movl -4(%rbp), %edx
addl %edx, %eax
addl $3, %eax
popq %rbp
ret
.LFE0:
.size example, .-example
.section .rodata
.LC0:
.string "%i\n"
.globl main
.type main, @function
main:
pushq %rbp
movq %rsp, %rbp
movl $2, %esi
movl $1, %edi
call example
movl %eax, %edx
movl $.LC0, %eax
movl %edx, %esi
movq %rax, %rdi
movl $0, %eax
call printf
movl $0, %eax
popq %rbp
ret
#include <stdio.h>
int example(int a, int b, int c, int d, int e, int f, int g, int h, int i) {
return a + b + c + d + e + f + g + h + i + 3;
}
int main(void) {
printf("%i\n", example(1, 2, 3, 4, 5, 6, 7, 8, 9));
return 0;
}
.globl example
.type example, @function
example:
pushq %rbp
movq %rsp, %rbp
movl %edi, -4(%rbp)
movl %esi, -8(%rbp)
movl %edx, -12(%rbp)
movl %ecx, -16(%rbp)
movl %r8d, -20(%rbp)
movl %r9d, -24(%rbp)
movl -8(%rbp), %eax
movl -4(%rbp), %edx
addl %edx, %eax
addl -12(%rbp), %eax
addl -16(%rbp), %eax
addl -20(%rbp), %eax
addl -24(%rbp), %eax
addl 16(%rbp), %eax
addl 24(%rbp), %eax
addl 32(%rbp), %eax
addl $3, %eax
popq %rbp
ret
.LFE0:
.size example, .-example
.section .rodata
.LC0:
.string "%i\n"
.globl main
.type main, @function
main:
pushq %rbp
movq %rsp, %rbp
subq $32, %rsp
movl $9, 16(%rsp)
movl $8, 8(%rsp)
movl $7, (%rsp)
movl $6, %r9d
movl $5, %r8d
movl $4, %ecx
movl $3, %edx
movl $2, %esi
movl $1, %edi
call example
movl %eax, %edx
movl $.LC0, %eax
movl %edx, %esi
movq %rax, %rdi
movl $0, %eax
call printf
movl $0, %eax
leave
ret
void _start(void) {
for (int i=0; i<10; i++) {
}
}
.global _start
_start:
pushq %rbp
movq %rsp, %rbp
movl $0, -4(%rbp)
jmp .L2
.L3:
addl $1, -4(%rbp)
.L2:
cmpl $9, -4(%rbp)
jle .L3
popq %rbp
int main(void) {
int a=1, b=2;
if (a < b)
return 0;
else
return 1;
}
.globl main
main:
pushq %rbp
movq %rsp, %rbp
movl $1, -8(%rbp)
movl $2, -4(%rbp)
movl -8(%rbp), %eax
cmpl -4(%rbp), %eax
jge .L2
movl $0, %eax
jmp .L3
.L2:
movl $1, %eax
.L3:
popq %rbp
ret
int main(void) {
struct {
int a;
char b;
char c[3];
} A = { a: 1, b: 'B', c: "Cat" };
return 0;
}
.globl main
main:
pushq %rbp
movq %rsp, %rbp
movl $1, -16(%rbp)
movb $66, -12(%rbp)
movw $24899, -11(%rbp)
movb $116, -9(%rbp)
movl $0, %eax
popq %rbp
ret
void _start(void) {
int i = 0;
while (i<10) {
i++;
}
}
.global _start
_start:
pushq %rbp
movq %rsp, %rbp
movl $0, -4(%rbp)
jmp .L2
.L3:
addl $1, -4(%rbp)
.L2:
cmpl $9, -4(%rbp)
jle .L3
popq %rbp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment