CAS CS 210 – Computer Systems
Fall 2012
PROBLEM SET 2 (PS2) (PROGRAM REPRESENTAITON)
OUT: TUESDAY, OCT 8
DUE: TUESDAY, OCT 23, 1:30 PM
NO LATE SUBMISSIONS WILL BE ACCEPTED
To be completed individually. For all questions, show your work in obtaining the final answers.
Page 1 of 7
1 Problem 1: 6 Points
Match each of the assembler routines on the left with the equivalent C function on the right.
foo1:
pushl %ebp
movl %esp,%ebp
movl 8(%ebp),%eax
sall $4,%eax
subl 8(%ebp),%eax
movl %ebp,%esp
popl %ebp
ret
foo2:
pushl %ebp
movl %esp,%ebp
movl 8(%ebp),%eax
testl %eax,%eax
jge .L4
addl $15,%eax
.L4:
sarl $4,%eax
movl %ebp,%esp
popl %ebp
ret
foo3:
pushl %ebp
movl %esp,%ebp
movl 8(%ebp),%eax
shrl $31,%eax
movl %ebp,%esp
popl %ebp
ret
int choice1(int x)
{
return (x < 0);
}
int choice2(int x)
{
return (x << 31) & 1; }
int choice3(int x)
{
return 15 * x;
}
int choice4(int x)
{
return (x + 15) /4
}
int choice5(int x)
{
return x / 16;
}
int choice6(int x)
{
return (x >> 31);
}
Fill in your answers here:
foo1 corresponds to choice .
foo2 corresponds to choice .
foo3 corresponds to choice .
Page 2 of 7
2 Problem 2: 10 Points
Consider the following assembly representation of a function foo containing a for loop:
foo:
pushl %ebp
movl %esp,%ebp
pushl %ebx
movl 8(%ebp),%ebx
leal 2(%ebx),%edx
xorl %ecx,%ecx
cmpl %ebx,%ecx
jge .L4
.L6:
leal 5(%ecx,%edx),%edx
leal 3(%ecx),%eax
imull %eax,%edx
incl %ecx
cmpl %ebx,%ecx
jl .L6
.L4:
movl %edx,%eax
popl %ebx
movl %ebp,%esp
popl %ebp
ret
Fill in the blanks to provide the functionality of the loop:
int foo(int a)
{
int i;
int result = _____________;
for( ________; ________; i++ ) {
__________________;
__________________;
}
return result;
}
Page 3 of 7
3 Problem 3: 20 Points
Consider the following C code
1 # i n c l u d e 10 { and the following dissasembly:
1 Dump of a s s e m b l e r code f o r f u n c t i o n m y c a l l e r : 10 0 x080483e5 Page 4 of 7 Given the above update the memory diagram on the next page assuming the following starting values and execution pc = 0x080483c5 Memory values not updated maybe left blank. Remember that an int value is 4 bytes located with the least significant 0xbfffec10: 04030201 Individual bytes of an int that whose value are unknown should be specifed as ??.
Page 5 of 7 Address int hex value Description 0xbfffea50
0xbfffea4c
0xbfffea48
0xbfffea44
0xbfffea40
0xbfffea3c
0xbfffea38
0xbfffea34
0xbfffea30
0xbfffea2c
0xbfffea28
0xbfffea24
0xbfffea20
0xbfffea1c
In the descriptions be sure to indicate if an address corresponds to a specific variable and its value or if an address is Page 6 of 7 4 Problem 5:
Consider the following incomplete definition of a C struct along with the incomplete code for a function func given typedef struct node {
_______________ x;
_______________ y;
struct node *next;
struct node *prev;
} node_t;
node_t n;
void func() {
node_t *m;
m = ______________________;
m->y /= 16;
return; When this C code was compiled on an IA-32 machine running Linux, the following assembly code was generated func: Given these code fragments, fill in the blanks in the C code given above. Note that there is a unique answer.
The types must be chosen from the following table, assuming the sizes and alignment given.
Type Size (bytes) Alignment (bytes) unsigned short 2 2 unsigned int 4 4 Page 7 of 7
2
3 vo id m y c a l l e e ( i n t s , i n t e , c h a r ∗ buf )
4 {
5 f o r ( ; s
11 i n t i ;
12 c h a r buf [ 1 6 ] ;
13
14 m y c a l l e e ( 9 , 1 6 , buf ) ;
15 f o r ( i =0 ; i <16; i ++) p r i n t f (”%d : %d\n ” , i , buf [ i ] ) ;
16 r e t u r n ;
17 }
18
19 i n t main ( vo id )
20 {
21 m y c a l l e r ( ) ;
22 r e t u r n 0 ;
23 }
2 0 x080483c4
3 0 x080483c5
4 0 x080483c7
5 0 x080483ca
6 0 x080483cd
7 0 x080483d1
8 0 x080483d9
9 0 x080483e0
11 . . .
upto 0x080483e5:
esp = 0xbfffea58
byte at the address and the remaining 3 bytes in the successive byte addresses. Eg. If we know that six bytes starting
at 0xbfffec10 is 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 then we would have to write down :
0xbfffec14: ????0605
0xbfffea5c 0x08048432 return address for call to mycaller
0xbfffea58 0xbfffea68 old frame pointer
0xbfffea54
a return address and its value.
below.
}
for function func.
pushl %ebp
movl n+12,%eax
movl 16(%eax),%eax
movl %esp,%ebp
movl %ebp,%esp
shrw $4,8(%eax)
popl %ebp
ret
char 1 1
short 2 2
int 4 4
double 8 4