EECS213, Fall 2015
HOMEWORK 2 SOLUTION
Problem 1 (20 points)
For each IA32 assembly code sequence below on the left, fill in the missing portion of corresponding C line on the
right. Simplify the C code as much as p
...
EECS213, Fall 2015
HOMEWORK 2 SOLUTION
Problem 1 (20 points)
For each IA32 assembly code sequence below on the left, fill in the missing portion of corresponding C line on the
right. Simplify the C code as much as possible.
foo:
pushl %ebp
movl %esp,%ebp
movl 8(%ebp),%eax
sall $5,%eax
subl 8(%ebp),%eax
movl %ebp,%esp
popl %ebp
ret
int foo(int x)
{
return 31*x; // (x<<5) x
}
foo:
pushl %ebp
movl %esp,%ebp
movl 8(%ebp),%eax
testl %eax,%eax
jge .L4
addl $3,%eax
.L4:
sarl $2,%eax
movl %ebp,%esp
popl %ebp ret
int foo(int x)
{
return x/4; // (x>=0 ? x:(x+3)) >> 2;
}
foo:
pushl %ebp
movl %esp,%ebp
movl 8(%ebp),%eax
shrl $16,%eax
movl %ebp,%esp
popl %ebp
ret
int foo(int x)
{
return (int) ((unsigned int)x >> 16);
}
foo:
pushl %ebp
movl %esp,%ebp
movl 8(%ebp),%eax
sall $4,%eax
addl 8(%ebp),%eax
addl %eax,%eax
movl %ebp,%esp
popl %ebp
ret
int foo(int x)
{
return 34*x; // 2*(x<<4 + x);
}
foo:
pushl %ebp
movl %esp,%ebp
movl 8(%ebp),%edx
movl 12(%ebp),%eax
movl %ebp,%esp
movl (%edx),%edx
addl %edx,(%eax)
movl %edx,%eax
popl %ebp
ret
int foo(int *xp, int *yp)
{
int x = *xp;
*yp += *xp; // or *yp += x;
return x;
}
This study source was downloaded by 100000784424693 from CourseHero.com on 04-28-2021 01:53:06 GMT -05:00
https://www.coursehero.com/file/14225010/HW2sol/
his stu
ed via
y re | ourse
urce wa
Hero.co
sha
Problem 2 (20 points)
Consider the following assembly representation of a function forloop containing a loop:
forloop:
pushl %ebp
movl %esp, %ebp
subl $16, %esp
movl $1, 4(%ebp)
movl $0, 8(%ebp)
jmp .L2
.L3:
movl 8(%ebp), %eax
addl %eax, %eax
addl $5, %eax
addl %eax, 4(%ebp)
addl $1, 8(%ebp)
.L2:
movl 8(%ebp), %eax
cmpl 8(%ebp), %eax
jl .L3
movl 4(%ebp), %eax
leave
ret
Fill in the blanks to provide the functionality of the loop. Note that 8(%ebp) stores the argument a.
intforloop ( inta)
{
inti;
intresult =1;
for (i = 0 ;i <a ;i ++ ){
result += ( 2 * i + 5 );
}re
turnresult;
} W
hat is the size of stack frame in bytes (including %ebp and %esp)?
20
...
a
ret address
forloop
%ebp
result
[Show More]