复旦大学计算机体系结构期末考试B卷

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

(1)考虑下面这个for循环的汇编代码looper:

pushl %ebp

movl %esp, %ebp

subl $16, %esp

movl $0, -8(%ebp)

movl $0, -4(%ebp)

jmp .L2

.L4:

movl -4(%ebp), %eax

sall $2, %eax

addl 12(%ebp), %eax

movl (%eax), %eax

cmpl -8(%ebp), %eax

jle .L3

movl -4(%ebp), %eax

sall $2, %eax

addl 12(%ebp), %eax

movl (%eax), %eax

movl %eax, -8(%ebp)

.L3:

addl $1, -8(%ebp)

addl $2, -4(%ebp)

.L2:

movl -4(%ebp), %eax

cmpl 8(%ebp), %eax

jl .L4

movl -8(%ebp), %eax

leave

ret

根据以上的汇编代码,完成相应的for循环C程序代码(注意该程序有返回值)。

int looper(int n, int *a)

{

int i;

int x=0;

for(i= 0 ; i

{______________________________________

if(a[i]>x)//2分

x=a[i];// 2分

x++;//1分

}______________________________________

return x;// 1分

}

(2)

根据已有的结构(structure)和联合(union)的声明以及C函数void test(int i, s2 *bp)的IA32 汇编代码,推断出结构s1的完整声明和s2中CNT的值;之后再根据推断结果以及IA32汇编代码,推断补齐相应的C程序代码。

下面是要使用到的结构和联合的声明,函数void test(int i, s2 *bp)及其IA32 汇编代码。

struct s1 { struct s2 { union u1 {

int left; struct s1 *h;

s1 a[CNT]; struct s2 *i;

int right; char j;

}; union u1 b; };

char c;

};

void test(int i, s2 *bp)

{

int n=bp->left+bp->right;

s1 *ap=&bp->a[i];

ap->x[ap->idx]=n;

}

其IA32汇编代码为:

test:

pushl %ebp

movl %esp, %ebp

subl $16, %esp

movl 12(%ebp), %eax

movl (%eax), %edx

movl 12(%ebp), %eax

movl 184(%eax), %eax

leal (%edx,%eax), %eax

movl %eax, -4(%ebp)

movl 12(%ebp), %eax

leal 4(%eax), %ecx

movl 8(%ebp), %edx

movl %edx, %eax

sall $2, %eax

addl %edx, %eax

sall $2, %eax

leal (%ecx,%eax), %eax

movl %eax, -8(%ebp)

movl -8(%ebp), %eax

movl (%eax), %edx

movl -8(%ebp), %eax

movl -4(%ebp), %ecx

movl %ecx, 4(%eax,%edx,4)

leave

ret

1. CNT=___9_____//2分

2. 写出s1的完整声明。

____struct s1{

int idx;//2分

int x[4];//2分

___}____________________________________

_______________________________________

_______________________________________

3. 对于下面左边的每个IA32 汇编代码,请在右边补全相应的C程序代码。(请注意下面的函数都需要返回值)

A. proc1:

pushl %ebp

movl %esp, %ebp

movl 8(%ebp), %eax

movzbl 188(%eax), %eax

popl %ebp

ret char proc1(struct s2 *x)

{

______________________________ return x->b.j;

(4分)

______________________________ ______________________________ ______________________________ }

相关文档
最新文档