GZU 深入了解计算机系统基础OJ答案记录第三章(1) 平时作业
先附上答案,我是废物。。。理解等我理解了再写!!!
题目1:switch语句的翻译
下面的汇编代码是某个函数编译后生成的: movl $-1, %eax cmpl $80, %edi ja .L3 leal 7(%rdi), %eax testl %edi, %edi cmovs %eax, %edi sarl $3, %edi subl $6, %edi movl $1, %eax cmpl $4, %edi ja .L3 mov %edi, %edi movl CSWTCH.15(,%rdi,4), %eax .L3: rep ret 在编译器输出的.s文件中还有下面的和该函数汇编指令有关的部分: CSWTCH.15: .long 2 .long 3 .long 4 .long 5 .long 5 main函数已经写好了,请根据上述汇编代码设计一个等价的C函数: int main(){ int n; scanf("%d",&n); printf("%d\n",func(n)); return 0; } |
答案1:
#include <stdio.h>
int func(int n) {
int eax;
if (n >= 80) {
eax = n + 7;
if (n < 0) {
n = eax;
}
n >>= 3;
n -= 6;
return 5;
} else {
if (n > 30) {
switch (n) {
case 0:
return 2;
case 1:
return 3;
case 2:
return 4;
case 3:
return 5;
case 4:
return 5;
case 5:
return 5;
case 6:
return 5;
case 7:
return 5;
case 8:
return 5;
case 9:
return 5;
case 10:
return 5;
case 11:
return 5;
case 12:
return 5;
case 13:
return 5;
case 14:
return 5;
case 15:
return 5;
case 16:
return 5;
case 17:
return 5;
case 18:
return 5;
case 19:
return 5;
case 20:
return 5;
case 21:
return 5;
case 22:
return 5;
case 23:
return 5;
case 24:
return 5;
case 25:
return 5;
case 26:
return 5;
case 27:
return 5;
case 28:
return 5;
case 29:
return 5;
default:
return 4;
}
} else if (n < 0) {
return -1;
} else {
return 1;
}
}
}
int main() {
int n;
scanf("%d", &n);
printf("%d\n", func(n));
return 0;
}
题目2:循环结构的翻译4(循环的嵌套)
下面的汇编代码是某个函数编译后生成的:
pushq %rbx
movl %edx, %r9d
movl $0, %r8d
testl %edi, %edi
jle .L6
movl $10, %r10d
movl $1, %r11d
movl $1, %ebx
jmp .L3
.L10:
leal (%r10,%rcx), %edx
movl %edx, %eax
sarl $31, %edx
idivl %r9d
cmpl $1, %edx
adcl $0, %r8d
addl $1, %ecx
cmpl %ecx, %esi
jge .L10
.L8:
addl $1, %r11d
addl $10, %r10d
cmpl %r11d, %edi
jl .L6
.L3:
movl %ebx, %ecx
testl %esi, %esi
jg .L10
jmp .L8
.L6:
movl %r8d, %eax
popq %rbx
ret
main函数已经写好了,请根据上述汇编代码设计一个等价的C函数:
int main(){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
printf("%d\n",func(a,b,c));
return 0;
}
答案2:
#include<stdio.h>
int func(int a, int b, int c) {
int r8 = 0;
int r10 = 10;
int r11 = 1;
int ebx = 1;
int ecx = 1;
int edx, eax;
if (a <= 0) {
return r8;
}
do {
do {
edx = r10 + c;
eax = edx;
edx >>= 31;
edx = eax / b;
r8 += edx > 1 ? 1 : 0;
c++;
} while (c <= b);
r11++;
r10 += 10;
} while (r11 <= a);
return r8;
}
int main()
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
printf("%d\n",func(a,b,c));
return 0;
}
题目3:循环结构的翻译3(for)
下面的汇编代码是某个函数编译后生成的:
movl $1, %ecx
movl $0, %r8d
testl %edi, %edi
jle .L3
.L7:
movl %ecx, %edx
movl %ecx, %eax
sarl $31, %edx
idivl %esi
leal (%r8,%rcx), %eax
testl %edx, %edx
cmove %eax, %r8d
addl $1, %ecx
cmpl %ecx, %edi
jge .L7
.L3:
movl %r8d, %eax
ret
main函数已经写好了,请根据上述汇编代码设计一个等价的C函数:
int main(){
int n,m;
scanf("%d%d",&n,&m);
printf("%d\n",func(n,m));
return 0;
}
答案3:
#include<stdio.h>
#define L long
int func(int n, int m)
{ //%edi %esi
L ec = 1;
L r8 = 0;
int ea, ed;
if(n > 0)
{
do
{
ed = ec;
ea = ec;
ed >>= 31;
ed = ea % m;
ea = ea / m;
ea = r8 + ec;
if(ed == 0) r8 = ea;
ec ++;
}while(n >= ec);
}
ea = r8;
return r8;
}
int main()
{
int n, m;
scanf("%d%d", &n, &m);
printf("%d\n", func(n, m));
return 0;
}
题目4:循环结构的翻译2(do...while)
下面的汇编代码是某个函数编译后生成的:
movl $0, %eax
.L3:
leal -1(%rdi), %edx
movl %edx, %ecx
imull %edi, %ecx
subl $2, %edi
imull %ecx, %edi
addl %edi, %eax
testl %edx, %edx
jle .L2
movl %edx, %edi
jmp .L3
.L2:
rep
ret
main函数已经写好了,请根据上述汇编代码设计一个等价的C函数:
int main(){
int n;
scanf("%d",&n);
printf("%d\n",func(n));
return 0;
}
答案4:
#include<stdio.h>
#define L long
int func(int n)
{ //%edi
int ea = 0;
L ed, ec;
while(1)
{
ed = n - 1;
ec = ed;
ec = n * ec;
n -= 2;
n = n * ec;
ea += n;
if(ed <= 0) break;
n = ed;
}
return ea;
}
int main()
{
int n;
scanf("%d",&n);
printf("%d\n",func(n));
return 0;
}
题目5:循环结构的翻译1(while)
下面的汇编代码是某个函数编译后生成的:
testl %edi, %edi
jle .L2
movl $1, %edx
movl $1, %ecx
.L3:
movslq %edx, %rsi
imulq %rsi, %rcx
addq %rcx, %rax
addl $1, %edx
cmpl %edx, %edi
jge .L3
.L2:
rep
ret
main函数已经写好了,请根据上述汇编代码设计一个等价的C函数:
int main(){
int n;
scanf("%d",&n);
long result=func(n);
printf("%ld\n",result);
return 0;
}
答案5:
#include <stdio.h>
long func(int n) {
long result = 1;
if (n > 0) {
int i = 1;
while (i <= n) {
long temp = 1;
for (int j = 1; j <= i; j++) {
temp *= j;
}
result += temp;
i++;
}
}
return result-1;
}
int main() {
int n;
scanf("%d", &n);
long result = func(n);
printf("%ld\n", result);
return 0;
}
题目6:选择结构的翻译3
下面的汇编代码是某个函数编译后生成的:
cmpl %esi, %edi
jge .L2
movl %edx, %eax
subl %esi, %eax
movl %esi, %ecx
subl %edx, %ecx
cmpl %edx, %esi
cmovge %ecx, %eax
ret
.L2:
movl %edx, %eax
subl %edi, %eax
movl %edi, %ecx
subl %edx, %ecx
cmpl %edx, %edi
cmovge %ecx, %eax
ret
main函数已经写好了,请根据上述汇编代码设计一个等价的C函数:
int main(){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
printf("%d\n",func(a,b,c));
return 0;
}
答案6:
#include<stdio.h>
int func(int a, int b, int c) {
int result;
if (a < b) {
result = c - b;
if (c >= b)
result = b - c;
} else {
result = c - a;
if (c >= a)
result = a - c;
}
int s = -result;
return s;
}
int main() {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
printf("%d\n", func(a, b, c));
return 0;
}
题目7:选择结构的翻译2
下面的汇编代码是某个函数编译后生成的:
cmpl $9, %edi
jg .L2
leal (%rsi,%rdi), %eax
movl %edi, %edx
subl %esi, %edx
cmpl %esi, %edi
cmovge %edx, %eax
ret
.L2:
movl $13, %eax
imull %edi, %esi
cmpl $19, %edi
cmovle %esi, %eax
ret
main函数已经写好了,请根据上述汇编代码设计一个等价的C函数:
int main(){
int x,y;
scanf("%d%d",&x,&y);
printf("%d\n",func(x,y));
return 0;
}
答案7:
#include <stdio.h>
int func(int x, int y) {
int result;
if (x <= 9) {
result = x + y;
if (x >= y) {
result = x - y;
}
} else {
result = 13;
if (x <= 19) {
result = x * y;
}
}
return result;
}
int main() {
int x, y;
scanf("%d%d", &x, &y);
printf("%d\n", func(x, y));
return 0;
}
题目8:选择结构的翻译1
下面的汇编代码是某个函数编译后生成的:
movq %rdi, %rax
subq %rsi, %rax
leaq (%rsi,%rdi), %rdx
cmpq %rsi, %rdi
cmovle %rdx, %rax
ret
main函数已经写好了,请根据上述汇编代码设计一个等价的C函数:
int main(){
long a,b;
scanf("%ld%ld",&a,&b);
printf("%ld",func(a,b));
return 0;
}
答案8:
#include<stdio.h>
long func(long a, long b) {
long result = a - b;
if (a <= b) {
result = a + b;
}
return result;
}