GZU 深入了解计算机系统基础OJ答案记录第三章(3) 平时作业
附上答案,我是废物,理解等理解了再发
题目1:一维数组的访问1
下面的汇编代码是函数func编译后生成的:
func:
movl $0, %eax
testl %esi, %esi
jle .L3
subl $1, %esi
leaq 4(,%rsi,4), %rcx
movl $0, %edx
.L4:
addl (%rdi,%rdx), %eax
addq $4, %rdx
cmpq %rcx, %rdx
jne .L4
.L3:
rep
ret
main函数已经写好了,请根据上述汇编代码设计一个等价的函数:
int main(){
int n,i;
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("%d\n",func(a,n));
return 0;
}
答案1:
#include<stdio.h>
int func(int a[], int n)
{
int sum = 0;
for(int i = 0; i < n; i ++)
sum += a[i];
return sum;
}
int main(){
int n;
scanf("%d", &n);
int a[n];
for(int i = 0; i < n; i ++)
scanf("%d", &a[i]);
printf("%d\n", func(a, n));
return 0;
}
题目2:一维数组的访问2
下面的汇编代码是函数func编译后生成的:
func:
cmpl $2, %esi
jle .L4
subl $3, %esi
leaq 4(,%rsi,4), %rcx
movl $0, %eax
.L3:
movl (%rdi,%rax), %edx
addl 4(%rdi,%rax), %edx
movl %edx, 8(%rdi,%rax)
addq $4, %rax
cmpq %rcx, %rax
jne .L3
.L4:
rep
ret
main函数已经写好了,请根据上述汇编代码设计一个等价的函数:
int main(){
int n,i;
scanf("%d",&n);
int a[n];
int sum=0;
a[0]=a[1]=1;
func(a,n);
for(i=0;i<n;i++)
sum+=a[i];
printf("%d\n",sum);
return 0;
}
答案2:
#include<stdio.h>
void func(int* a, int n)
{ //%edi //%esi
if(n <= 2) return;
int ans = 0;
for(int i = 2; i < n; i ++)
a[i] = a[i - 1] + a[i - 2];
return;
}
int main(){
int n;
scanf("%d", &n);
int a[n], sum = 0;
a[0] = a[1] = 1;
func(a, n);
for(int i = 0; i < n; i ++)
sum += a[i];
printf("%d\n", sum);
return 0;
}
题目3:二维数组的访问
下面的汇编代码是函数func编译后生成的:
func:
pushq %r12
pushq %rbp
pushq %rbx
testl %edi, %edi
jle .L6
movslq %esi, %rbx
salq $2, %rbx
movl $0, %r11d
leal -1(%rsi), %eax
leaq 4(,%rax,4), %r10
movl $0, %r12d
movl $0, %ebp
jmp .L3
.L4:
movl %ecx, (%r9,%rax)
addl %r8d, %ecx
addq $4, %rax
cmpq %r10, %rax
jne .L4
.L5:
addl $1, %r11d
addq %rbx, %rdx
cmpl %edi, %r11d
je .L6
.L3:
testl %esi, %esi
jle .L5
movq %rdx, %r9
movl %r11d, %r8d
movq %r12, %rax
movl %ebp, %ecx
jmp .L4
.L6:
popq %rbx
popq %rbp
popq %r12
ret
main函数已经写好了,请根据上述汇编代码设计一个等价的函数:
int main(){
int m,n,i,j;
scanf("%d%d",&m,&n);
int a[m][n];
func(m,n,a);
int sum=0;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
sum+=a[i][j];
printf("%d\n",sum);
return 0;
}
答案3:
#include<stdio.h>
void func(int m, int n, int* a){
for(int i = 0; i < m; i ++)
{
int t = 0;
for(int j = 0; j < n; j ++)
{
*(a + i * n + j) = t;
t += i;
}
}
}
int main(){
int n, m;
scanf("%d%d", &m, &n);
int a[m][n];
func(m, n, *a);//指向一维数组
int sum = 0;
for(int i = 0; i < m; i ++)
for(int j = 0; j < n; j ++)
sum += a[i][j];
printf("%d\n", sum);
return 0;
}
题目4:结构体的访问
下面的汇编代码是函数func编译后生成的:
func:
movsbl (%rdi), %edx
movswl 8(%rdi), %eax
leal (%rdx,%rax), %eax
addl 4(%rdi), %eax
cltq
imulq 16(%rdi), %rax
ret
main函数已经写好了,请根据上述汇编代码设计一个等价的函数并推导出结构体类型的定义:
int main(){
struct A aa;
int m,n;
scanf("%d%d",&m,&n);
aa.a=m;
aa.b=m+n;
aa.c=n;
aa.d=m*n;
printf("%ld",func(&aa));
return 0;
}
答案4:
#include<stdio.h>
struct A{
long a, b, c, d;
};
long func(struct A* a){
return ((a->a + a->c) + a->b) * a->d;
}
int main(){
struct A aa;
int m, n;
scanf("%d%d", &m, &n);
aa.a = m;
aa.b = m + n;
aa.c = n;
aa.d = m * n;
printf("%ld", func(&aa));
return 0;
}