GZU 深入了解计算机系统基础OJ答案记录第三章(2) 平时作业
先附上答案,我是废物,理解等了解了再发
题目1:栈上的局部存储
下面的汇编代码是函数func1和func2编译后生成的:
func1:
movl (%rdi), %edx
movl (%rsi), %eax
leal (%rax,%rdx), %ecx
movl %ecx, (%rdi)
subl %eax, %edx
movl %edx, (%rsi)
ret
func2:
subq $16, %rsp
movl %edi, 12(%rsp)
movl %esi, 8(%rsp)
leaq 8(%rsp), %rsi
leaq 12(%rsp), %rdi
call func1
movl 12(%rsp), %edx
movl 8(%rsp), %eax
imull %eax, %eax
imull %edx, %edx
addl %edx, %eax
addq $16, %rsp
ret
main函数已经写好了,请根据上述汇编代码设计等价的func1和func2函数:
int main(){
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",func2(a,b));
return 0;
}
答案1:
#include<stdio.h>
#define L long
void func1(int* a, int* b)
{
int t = *a;
*a = *b + *a;
*b = t - *b;
}
int func2(int a, int b)
{ //%edi %esi
func1(&a, &b);
return b * b + a * a;
}
int main()
{
int a, b;
scanf("%d%d", &a, &b);
printf("%d\n", func2(a,b));
return 0;
}
题目2:函数调用时的参数传递
下面的汇编代码是函数func编译后生成的:
func:
leal (%rcx,%rdx), %edx
movslq %edx, %rdx
leaq (%rsi,%rdi), %rdi
movq %rdx, %rsi
imulq %rdi, %rsi
movswl %r9w, %r9d
movswl %r8w, %r8d
addl %r8d, %r9d
movslq %r9d, %r9
imulq %r9, %rsi
movsbl 16(%rsp), %edx
movsbl 8(%rsp), %eax
leal (%rdx,%rax), %eax
cltq
imulq %rsi, %rax
ret
main函数已经写好了,请根据上述汇编代码设计一个等价的C函数:
int main(){
int a,b;
scanf("%d%d",&a,&b);
printf("%ld\n",func(a,b,a+b,a-b,a*a,b*b,a*b,a*a-b));
return 0;
}
答案2:
#include<stdio.h>
#define Int long
int func(Int a, Int b, Int c, Int d, Int e, Int f, Int g, Int h)
{ //%edi %esi %edx %ecx %r8d %r9d
return (c + d) * (a + b) * (e + f) * (h + g);
}
int main()
{
int a, b;
scanf("%d%d", &a, &b);
printf("%ld\n", func(a, b, a + b, a - b, a * a, b * b, a * b, a * a - b));
return 0;
}
题目3:函数的递归调用2
下面的汇编代码是函数func编译后生成的:
func:
pushq %rbx
movl %edi, %ebx
movl $1, %eax
testl %edi, %edi
jle .L3
shrl $31, %edi
addl %ebx, %edi
sarl %edi
call func
leal 3(%rax,%rbx), %eax
.L3:
popq %rbx
ret
main函数已经写好了,请根据上述汇编代码设计一个等价的C函数:
int main(){
int a;
scanf("%d",&a);
printf("%d\n",func(a));
return 0;
}
答案3:
#include <stdio.h>
int func(int a) {
if (a <= 0) {
return 1;
}
int ebx = a;
int eax = 1;
a >>= 31;
a += ebx;
a >>= 1;
int result = func(a);
result = 3 + result + ebx;
return result;
}
int main() {
int a;
scanf("%d", &a);
printf("%d\n", func(a));
return 0;
}
题目4:函数的递归调用1
下面的汇编代码是函数func编译后生成的:
func:
pushq %rbx
movl %edi, %ebx
movl $1, %eax
cmpl $1, %edi
jle .L3
leal -1(%rbx), %edi
call func
imull %ebx, %ebx
addl %ebx, %eax
.L3:
popq %rbx
ret
main函数已经写好了,请根据上述汇编代码设计一个等价的C函数:
int main(){
int a;
scanf("%d",&a);
printf("%d\n",func(a));
return 0;
}
答案4:
#include<stdio.h>
#define Int long
int func(int a)
{
if(a <= 1) return 1;
return func(a - 1) + a * a;
}
int main()
{
int a;
scanf("%d", &a);
printf("%d\n", func(a));
return 0;
}
题目5:简单的函数调用
下面的汇编代码是函数func1和func2编译后生成的:
func1:
leal (%rsi,%rdi), %edi
movl %edx, %eax
imull %edi, %eax
ret
func2:
leal (%rsi,%rdi), %edx
addl $2, %esi
addl $1, %edi
call func1
rep
ret
main函数已经写好了,请根据上述汇编代码设计等价的func1和func2函数:
int main(){
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",func2(a,b));
return 0;
}
答案5:
#include<stdio.h>
#define Int long
int func1(int a, int b, int t){
return t * (a + b);
}
int func2(int a, int b){
return func1(a + 1, b + 2, a + b);
}
int main(){
int a, b;
scanf("%d%d", &a, &b);
printf("%d\n", func2(a, b));
return 0;
}