fill in the table in the end of the question

CAS CS 210 – Computer Systems
Fall 2012

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper

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.

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper

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
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

10 {
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 }

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 :
2 0 x080483c4 : push %ebp
3 0 x080483c5 : mov %esp ,% ebp
4 0 x080483c7 : sub $0x38 ,% esp
5 0 x080483ca : l e a −0x14(%ebp ) ,% eax
6 0 x080483cd : mov %eax , 0 x8(% esp )
7 0 x080483d1 : movl $0x10 , 0 x4(% esp )
8 0 x080483d9 : movl $0x9 ,(% esp )
9 0 x080483e0 : c a l l 0 x80483a4

10 0 x080483e5 : movl $0x0 ,−0x4(%ebp )
11 . . .

Page 4 of 7

Given the above update the memory diagram on the next page assuming the following starting values and execution
upto 0x080483e5:

pc = 0x080483c5
esp = 0xbfffea58

Memory values not updated maybe left blank. Remember that an int value is 4 bytes located with the least significant
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 :

0xbfffec10: 04030201
0xbfffec14: ????0605

Individual bytes of an int that whose value are unknown should be specifed as ??.

Page 5 of 7

Address int hex value Description
0xbfffea5c 0x08048432 return address for call to mycaller
0xbfffea58 0xbfffea68 old frame pointer
0xbfffea54

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
a return address and its value.

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
below.

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
for function func.

func:
pushl %ebp
movl n+12,%eax
movl 16(%eax),%eax
movl %esp,%ebp
movl %ebp,%esp
shrw $4,8(%eax)
popl %ebp
ret

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)
char 1 1
short 2 2

unsigned short 2 2
int 4 4

unsigned int 4 4
double 8 4

Page 7 of 7

Still stressed from student homework?
Get quality assistance from academic writers!

Order your essay today and save 25% with the discount code LAVENDER