PPT of the training:
Here is the code:
// fib_n_rec main
#include <stdio.h>
int main()
{
int n;
printf("Please input the n for Fibonacci Program:\n");
scanf("%d", &n);
int result = fib_n_rec(n);
printf("The result is %d.\n",result);
return 0;
}
//fib_n_rec
int fib_n_rec(int n)
{
int i = 1;
int val = 1;
int nval = 1;
while (i < n) {
int t = val+nval;
val = nval;
nval = t;
i++;
}
return val;
}
/*fib_n_rec.s*/
.file "fib_n_rec.c"
.text
.globl _fib_n_rec
.def _fib_n_rec; .scl 2; .type 32; .endef
_fib_n_rec:
pushl %ebp
movl %esp, %ebp
subl $16, %esp
movl $1, -16(%ebp)
movl $1, -12(%ebp)
movl $1, -8(%ebp)
jmp L2
L3:
movl -8(%ebp), %edx
movl -12(%ebp), %eax
addl %edx, %eax
movl %eax, -4(%ebp)
movl -8(%ebp), %eax
movl %eax, -12(%ebp)
movl -4(%ebp), %eax
movl %eax, -8(%ebp)
addl $1, -16(%ebp)
L2:
movl -16(%ebp), %eax
cmpl 8(%ebp), %eax
jl L3
movl -12(%ebp), %eax
leave
ret
//fib_rec.c
int fib_rec(int n)
{
int prev_val, val;
if (n <= 2)
return 1;
prev_val = fib_rec(n-2);
val = fib_rec(n-1);
return prev_val + val;
}
//fib_rec_main.c
#include <stdio.h>
int main()
{
int n;
printf("Please input the n for Fibonacci Program:\n");
scanf("%d", &n);
int result = fib_rec(n);
printf("The result is %d.\n",result);
return 0;
}
/*fib_rec.s*/
.file "fib_rec.c"
.text
.globl _fib_rec
.def _fib_rec; .scl 2; .type 32; .endef
_fib_rec:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
cmpl $2, 8(%ebp)
jg L2
movl $1, -20(%ebp)
jmp L3
L2:
movl 8(%ebp), %eax
subl $2, %eax
movl %eax, (%esp)
call _fib_rec
movl %eax, -8(%ebp)
movl 8(%ebp), %eax
subl $1, %eax
movl %eax, (%esp)
call _fib_rec
movl %eax, -4(%ebp)
movl -4(%ebp), %edx
movl -8(%ebp), %eax
addl %edx, %eax
movl %eax, -20(%ebp)
L3:
movl -20(%ebp), %eax
leave
ret
//swapadd.c
int swap_add(int *xp, int *yp)
{
int x = *xp;
int y = *yp;
*xp = y;
*yp = x;
return x + y;
}
int caller(int arg1, int arg2)
{
//int arg1 = 534;
//int arg2 = 1057;
int sum = swap_add(&arg1, &arg2);
int diff = arg1 - arg2;
return sum * diff;
}
//swapadd_main.c
#include <stdio.h>
int main()
{
printf("Please input two not-zero integers:\n");
int arg1 = 0, arg2 = 0;
scanf("%d,%d", &arg1, &arg2);
int result = caller( arg1, arg2);
printf("The result is %d!\n", result);
return 0;
}
/*swapadd.s*/
.file "swapadd.c"
.text
.globl _swap_add
.def _swap_add; .scl 2; .type 32; .endef
_swap_add:
pushl %ebp
movl %esp, %ebp
subl $16, %esp
movl 8(%ebp), %eax
movl (%eax), %eax
movl %eax, -8(%ebp)
movl 12(%ebp), %eax
movl (%eax), %eax
movl %eax, -4(%ebp)
movl 8(%ebp), %edx
movl -4(%ebp), %eax
movl %eax, (%edx)
movl 12(%ebp), %edx
movl -8(%ebp), %eax
movl %eax, (%edx)
movl -4(%ebp), %edx
movl -8(%ebp), %eax
addl %edx, %eax
leave
ret
.globl _caller
.def _caller; .scl 2; .type 32; .endef
_caller:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
leal 12(%ebp), %eax
movl %eax, 4(%esp)
leal 8(%ebp), %eax
movl %eax, (%esp)
call _swap_add
movl %eax, -8(%ebp)
movl 8(%ebp), %edx
movl 12(%ebp), %eax
movl %edx, %ecx
subl %eax, %ecx
movl %ecx, %eax
movl %eax, -4(%ebp)
movl -8(%ebp), %eax
imull -4(%ebp), %eax
leave
ret
No comments:
Post a Comment