Created
April 7, 2021 13:35
-
-
Save jnguyen1098/061d4c0ad64858cf60d484acc746db88 to your computer and use it in GitHub Desktop.
C / ASM versions of fac, gcd, and sort (CIS*4650)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
/* A program to compute the factorial value of an input */ | |
int main(void) { | |
int x; int fac; | |
scanf("%d", &x); | |
fac = 1; | |
while (x > 1) { | |
fac = fac * x; | |
x = x - 1; | |
} | |
printf("%d\n", fac); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.file "fac.c" | |
.text | |
.section .rodata | |
.LC0: | |
.string "%d" | |
.LC1: | |
.string "%d\n" | |
.text | |
.globl main | |
.type main, @function | |
main: | |
.LFB0: | |
.cfi_startproc | |
pushq %rbp | |
.cfi_def_cfa_offset 16 | |
.cfi_offset 6, -16 | |
movq %rsp, %rbp | |
.cfi_def_cfa_register 6 | |
subq $16, %rsp | |
leaq -8(%rbp), %rax | |
movq %rax, %rsi | |
leaq .LC0(%rip), %rdi | |
movl $0, %eax | |
call __isoc99_scanf@PLT | |
movl $1, -4(%rbp) | |
jmp .L2 | |
.L3: | |
movl -8(%rbp), %eax | |
movl -4(%rbp), %edx | |
imull %edx, %eax | |
movl %eax, -4(%rbp) | |
movl -8(%rbp), %eax | |
subl $1, %eax | |
movl %eax, -8(%rbp) | |
.L2: | |
movl -8(%rbp), %eax | |
cmpl $1, %eax | |
jg .L3 | |
movl -4(%rbp), %eax | |
movl %eax, %esi | |
leaq .LC1(%rip), %rdi | |
movl $0, %eax | |
call printf@PLT | |
movl $0, %eax | |
leave | |
.cfi_def_cfa 7, 8 | |
ret | |
.cfi_endproc | |
.LFE0: | |
.size main, .-main | |
.ident "GCC: (Debian 8.3.0-6) 8.3.0" | |
.section .note.GNU-stack,"",@progbits |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
/* A program to perform Euclid's | |
algorithm to compute a gcd */ | |
int gcd(int u, int v) { | |
if (v == 0) return u; | |
else return gcd(v, u - u/v*v); | |
} | |
int main(void) { | |
int x; int y; | |
scanf("%d", &x); | |
scanf("%d", &y); | |
printf("%d\n", gcd(x, y)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.file "gcd.c" | |
.text | |
.globl gcd | |
.type gcd, @function | |
gcd: | |
.LFB0: | |
.cfi_startproc | |
pushq %rbp | |
.cfi_def_cfa_offset 16 | |
.cfi_offset 6, -16 | |
movq %rsp, %rbp | |
.cfi_def_cfa_register 6 | |
subq $16, %rsp | |
movl %edi, -4(%rbp) | |
movl %esi, -8(%rbp) | |
cmpl $0, -8(%rbp) | |
jne .L2 | |
movl -4(%rbp), %eax | |
jmp .L3 | |
.L2: | |
movl -4(%rbp), %eax | |
cltd | |
idivl -8(%rbp) | |
movl -8(%rbp), %eax | |
movl %edx, %esi | |
movl %eax, %edi | |
call gcd | |
.L3: | |
leave | |
.cfi_def_cfa 7, 8 | |
ret | |
.cfi_endproc | |
.LFE0: | |
.size gcd, .-gcd | |
.section .rodata | |
.LC0: | |
.string "%d" | |
.LC1: | |
.string "%d\n" | |
.text | |
.globl main | |
.type main, @function | |
main: | |
.LFB1: | |
.cfi_startproc | |
pushq %rbp | |
.cfi_def_cfa_offset 16 | |
.cfi_offset 6, -16 | |
movq %rsp, %rbp | |
.cfi_def_cfa_register 6 | |
subq $16, %rsp | |
leaq -4(%rbp), %rax | |
movq %rax, %rsi | |
leaq .LC0(%rip), %rdi | |
movl $0, %eax | |
call __isoc99_scanf@PLT | |
leaq -8(%rbp), %rax | |
movq %rax, %rsi | |
leaq .LC0(%rip), %rdi | |
movl $0, %eax | |
call __isoc99_scanf@PLT | |
movl -8(%rbp), %edx | |
movl -4(%rbp), %eax | |
movl %edx, %esi | |
movl %eax, %edi | |
call gcd | |
movl %eax, %esi | |
leaq .LC1(%rip), %rdi | |
movl $0, %eax | |
call printf@PLT | |
movl $0, %eax | |
leave | |
.cfi_def_cfa 7, 8 | |
ret | |
.cfi_endproc | |
.LFE1: | |
.size main, .-main | |
.ident "GCC: (Debian 8.3.0-6) 8.3.0" | |
.section .note.GNU-stack,"",@progbits |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
/* A program to perform selection sort | |
on a 10 element array */ | |
int x[10]; | |
int minloc(int a[], int low, int high) { | |
int i; int x; int k; | |
k = low; | |
x = a[low]; | |
i = low + 1; | |
while (i < high) { | |
if (a[i] < x) { | |
x = a[i]; | |
k = i; | |
} | |
i = i + 1; | |
} | |
return k; | |
} | |
void sort(int a[], int low, int high) { | |
int i; int k; | |
i = low; | |
while (i < high - 1) { | |
int t; | |
k = minloc(a, i, high); | |
t = a[k]; | |
a[k] = a[i]; | |
a[i] = t; | |
i = i + 1; | |
} | |
} | |
int main(void) { | |
int i; | |
i = 0; | |
while (i < 10) { | |
scanf("%d", &x[i]); | |
i = i + 1; | |
} | |
sort(x, 0, 10); | |
i = 0; | |
while (i < 10) { | |
printf("%d\n", x[i]); | |
i = i + 1; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.file "sort.c" | |
.text | |
.comm x,40,32 | |
.globl minloc | |
.type minloc, @function | |
minloc: | |
.LFB0: | |
.cfi_startproc | |
pushq %rbp | |
.cfi_def_cfa_offset 16 | |
.cfi_offset 6, -16 | |
movq %rsp, %rbp | |
.cfi_def_cfa_register 6 | |
movq %rdi, -24(%rbp) | |
movl %esi, -28(%rbp) | |
movl %edx, -32(%rbp) | |
movl -28(%rbp), %eax | |
movl %eax, -12(%rbp) | |
movl -28(%rbp), %eax | |
cltq | |
leaq 0(,%rax,4), %rdx | |
movq -24(%rbp), %rax | |
addq %rdx, %rax | |
movl (%rax), %eax | |
movl %eax, -8(%rbp) | |
movl -28(%rbp), %eax | |
addl $1, %eax | |
movl %eax, -4(%rbp) | |
jmp .L2 | |
.L4: | |
movl -4(%rbp), %eax | |
cltq | |
leaq 0(,%rax,4), %rdx | |
movq -24(%rbp), %rax | |
addq %rdx, %rax | |
movl (%rax), %eax | |
cmpl %eax, -8(%rbp) | |
jle .L3 | |
movl -4(%rbp), %eax | |
cltq | |
leaq 0(,%rax,4), %rdx | |
movq -24(%rbp), %rax | |
addq %rdx, %rax | |
movl (%rax), %eax | |
movl %eax, -8(%rbp) | |
movl -4(%rbp), %eax | |
movl %eax, -12(%rbp) | |
.L3: | |
addl $1, -4(%rbp) | |
.L2: | |
movl -4(%rbp), %eax | |
cmpl -32(%rbp), %eax | |
jl .L4 | |
movl -12(%rbp), %eax | |
popq %rbp | |
.cfi_def_cfa 7, 8 | |
ret | |
.cfi_endproc | |
.LFE0: | |
.size minloc, .-minloc | |
.globl sort | |
.type sort, @function | |
sort: | |
.LFB1: | |
.cfi_startproc | |
pushq %rbp | |
.cfi_def_cfa_offset 16 | |
.cfi_offset 6, -16 | |
movq %rsp, %rbp | |
.cfi_def_cfa_register 6 | |
subq $32, %rsp | |
movq %rdi, -24(%rbp) | |
movl %esi, -28(%rbp) | |
movl %edx, -32(%rbp) | |
movl -28(%rbp), %eax | |
movl %eax, -4(%rbp) | |
jmp .L7 | |
.L8: | |
movl -32(%rbp), %edx | |
movl -4(%rbp), %ecx | |
movq -24(%rbp), %rax | |
movl %ecx, %esi | |
movq %rax, %rdi | |
call minloc | |
movl %eax, -8(%rbp) | |
movl -8(%rbp), %eax | |
cltq | |
leaq 0(,%rax,4), %rdx | |
movq -24(%rbp), %rax | |
addq %rdx, %rax | |
movl (%rax), %eax | |
movl %eax, -12(%rbp) | |
movl -4(%rbp), %eax | |
cltq | |
leaq 0(,%rax,4), %rdx | |
movq -24(%rbp), %rax | |
addq %rdx, %rax | |
movl -8(%rbp), %edx | |
movslq %edx, %rdx | |
leaq 0(,%rdx,4), %rcx | |
movq -24(%rbp), %rdx | |
addq %rcx, %rdx | |
movl (%rax), %eax | |
movl %eax, (%rdx) | |
movl -4(%rbp), %eax | |
cltq | |
leaq 0(,%rax,4), %rdx | |
movq -24(%rbp), %rax | |
addq %rax, %rdx | |
movl -12(%rbp), %eax | |
movl %eax, (%rdx) | |
addl $1, -4(%rbp) | |
.L7: | |
movl -32(%rbp), %eax | |
subl $1, %eax | |
cmpl %eax, -4(%rbp) | |
jl .L8 | |
nop | |
leave | |
.cfi_def_cfa 7, 8 | |
ret | |
.cfi_endproc | |
.LFE1: | |
.size sort, .-sort | |
.section .rodata | |
.LC0: | |
.string "%d" | |
.LC1: | |
.string "%d\n" | |
.text | |
.globl main | |
.type main, @function | |
main: | |
.LFB2: | |
.cfi_startproc | |
pushq %rbp | |
.cfi_def_cfa_offset 16 | |
.cfi_offset 6, -16 | |
movq %rsp, %rbp | |
.cfi_def_cfa_register 6 | |
subq $16, %rsp | |
movl $0, -4(%rbp) | |
jmp .L10 | |
.L11: | |
movl -4(%rbp), %eax | |
cltq | |
leaq 0(,%rax,4), %rdx | |
leaq x(%rip), %rax | |
addq %rdx, %rax | |
movq %rax, %rsi | |
leaq .LC0(%rip), %rdi | |
movl $0, %eax | |
call __isoc99_scanf@PLT | |
addl $1, -4(%rbp) | |
.L10: | |
cmpl $9, -4(%rbp) | |
jle .L11 | |
movl $10, %edx | |
movl $0, %esi | |
leaq x(%rip), %rdi | |
call sort | |
movl $0, -4(%rbp) | |
jmp .L12 | |
.L13: | |
movl -4(%rbp), %eax | |
cltq | |
leaq 0(,%rax,4), %rdx | |
leaq x(%rip), %rax | |
movl (%rdx,%rax), %eax | |
movl %eax, %esi | |
leaq .LC1(%rip), %rdi | |
movl $0, %eax | |
call printf@PLT | |
addl $1, -4(%rbp) | |
.L12: | |
cmpl $9, -4(%rbp) | |
jle .L13 | |
movl $0, %eax | |
leave | |
.cfi_def_cfa 7, 8 | |
ret | |
.cfi_endproc | |
.LFE2: | |
.size main, .-main | |
.ident "GCC: (Debian 8.3.0-6) 8.3.0" | |
.section .note.GNU-stack,"",@progbits |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment