Exercise 3 - Winter 2012 (Answers)
-
A function f has the following overall structure:
int f(unsigned x) { int n = 0; while( _______ /* A.*/) { __________; /* B.*/ __________; /* C. */ } return _____; /* D. */ }
The gcc C compiler generates the following assembly code:
f: pushl %ebp xorl %eax, %eax movl %esp, %ebp movl 8(%ebp), %edx testl %edx, %edx je .L4 .L5: addl $1, %eax shrl %edx jne .L5 .L4: popl %ebp ret
Determine the register usage.
That is, determine which register is used for variable n and which register is used for variable x.
Using this register to variable correspondence, fill in the 4 missing parts of the C code.
Answers:
- A.
- B.
- C.
- D.
-
What value would be returned by each function call below if the IA32 assembly code for f is:
f: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx cmpl $10, %edx jle .L2 leal -2(%edx), %eax jmp .L4 .L2: leal 1(%edx,%edx,2), %eax .L4: popl %ebp ret
Function Call Return value f(12) f(2) -
Consider the following struct declarations on an IA32 machine.
Assume the alignment rules for Windows. That is, each primitive data type (char, short, int, float, double) of size X must begin at an address that is a multiple of X.
Assume
- sizeof(char) = 1
- sizeof(short) = 2
- sizeof(int) = 4
- sizeof(double) = 8
- sizeof(any pointer type) = 4
For each struct declaration, fill in
- the offset in bytes from the beginning of the struct for each member field
- number of padding bytes (0 or more) after each member field
- the total number of bytes allocated for the struct
- the alignment for the struct; that is, what its beginning address must be a multiple of: 1, 2, 4, or 8
Example
struct p { char ch1; char *str; char ch2; };
member offset member
sizepadding
(0 if none)ch1 0 1 3 str 4 4 0 ch2 8 1 3 total struct size 12 struct alignment
(1,2,4,or 8)4 -
struct p1 { char ch; short s; int n; };
member offset member
sizepadding
(0 if none)ch s n total struct size struct alignment
(1,2,4,or 8) -
struct p2 { char ch; int n; short s; };
member offset member
sizepadding
(0 if none)ch n s total struct size struct alignment
(1,2,4,or 8) -
struct p3 { int *ip; double d; char ch; };
member offset member
sizepadding
(0 if none)ip d ch total struct size struct alignment
(1,2,4,or 8)