Homework 2
Homework 2
Homework 2
Introduction
The purpose of written homework assignments is to get you thinking about the topics being covered
in lecture and in readings in the textbook which are not represented in the hands-on, programming
lab assignments. It is worth noting that the book contains many practice problems similar to the
problems we ask here. The solutions for those practice problems are located at the end of each
chapter and should give you a feel for the kind of answers we expect you to turn in for these kind of
assignments.
Logistics
Written homeworks will not be turned in for credit like the programming lab assignments. Solutions
will NOT be provided, but the forums are open to all discussion about the homework. We encourage
you to complete the homework to the best of your ability and then discuss your findings/questions
with your peers on the forums.
Questions
Answer the following problems:
1 call next
2 next:
3 popl %eax
https://class.coursera.org/hwswinterface-001/wiki/view?page=Homework2 1/5
7/10/13 Wiki - Homework 2 | The Hardware/Software Interface
We see that just three registers ( %ebx, %esi, and %edi) are saved on the stack (lines 2-4).
THe program modifies these and three other registers ( %eax, %ecx, and %edx). At the end of
the procedure, the values of registers %edi, %esi, and %ebx are restored (not shown), while
the other three are left in their modified states.
Explain this apparent inconsistency in the saving and restoring of register states.
1 int proc(void)
2 {
3 int x,y;
4 scanf("%x %x", &y, &x);
5 return x-y;
6 }
1 proc:
2 pushl %ebp
3 movl %esp, %ebp
4 subl $40, %esp
5 leal -4(%ebp), %eax
6 movl %eax, 8(%esp)
7 leal -8(%ebp), %eax
8 movl %eax, 4(%esp)
9 movl $.LC0, (%esp)
10 call scanf
Diagram stack frame at this point
11 movl -4(%ebp), %eax
12 subl -8(%ebp), %eax
13 leave
14 ret
Assume that procedure proc starts executing with the following register values:
Register Value
%esp 0x800040
https://class.coursera.org/hwswinterface-001/wiki/view?page=Homework2 2/5
7/10/13 Wiki - Homework 2 | The Hardware/Software Interface
%ebp 0x800060
Suppose proc calls scanf (line 10), and that scanf reads values 0x46 and 0x53 from the
standard input. Assume that the string "%x %x" is stored at memory location 0x300070.
struct {
char *a;
short b;
double c;
char d;
float e;
char f;
long long g;
void *h;
} foo;
suppose it was compiled on a Windows machine, where each primitive data type of K bytes
must have an offset that is a multiple of K.
A. What are the byte offsets of all the fields in the structure?
B. What is the total size of the structure?
C. Rearrange the fields of the structure to minimize wasted space, and then show the byte
offsets and total size fo the rearranged structure.
D. What was your strategy (if any) to reduce wasted space? Would this always work? If not,
can you describe a general strategy such that the space the struct uses is minimized?
5. Practice Problem 3.51 (pg 289)
For the C program
https://class.coursera.org/hwswinterface-001/wiki/view?page=Homework2 3/5
7/10/13 Wiki - Homework 2 | The Hardware/Software Interface
A. Draw a diagram indicating the stack locations used by this function and their offsets relative
to the stack pointer.
B. Annotate the assembly code to describe the offect of each instruction.
C. What interesting feature does this example illustrate about the x86-64 stack discipline?
D. What value is returned if the function is called with i=6?
6. Homework Problem 3.64 (pg 302)
For this exercise, we will examine the code generated by GCC for functions that have structures
as arguments and return values, and from this see how these language features are typically
implemented.
The following C code has a function word_sum having structures as argument and return values,
and a function prod that calls word_sum:
typedef struct {
int a;
int *p;
} str1;
typedef struct {
int sum;
int diff;
} str2;
https://class.coursera.org/hwswinterface-001/wiki/view?page=Homework2 4/5
7/10/13 Wiki - Homework 2 | The Hardware/Software Interface
s1.p = &y;
s2 = word_sum(s1);
return s2.sum * s2.diff;
}
1 word_sum: 1 prod:
2 pushl %ebp 2 pushl %ebp
3 movl %esp, %ebp 3 movl %esp, %ebp
4 pushl %ebx 4 subl $20, %esp
5 movl 8(%ebp), %eax 5 leal 12(%ebp), %edx
6 movl 12(%ebp), %ebx 6 leal -8(%ebp), %ecx
7 movl 16(%ebp), %edx 7 movl 8(%ebp), %eax
8 movl (%edx), %edx 8 movl %eax, 4(%esp)
9 movl %ebx, %ecx 9 movl %edx, 8(%esp)
10 subl %edx, %ecx 10 movl %ecx, (%esp)
11 movl %ecx, 4(%eax) 11 call word_sum
12 addl %ebx, %edx 12 subl $4, %esp
13 movl %edx, (%eax) 13 movl -4(%ebp), %eax
14 popl %ebx 14 imull -8(%ebp), %eax
15 popl %ebp 15 leave
16 ret $4 16 ret
The instruction ret $4 is like a normal return instruction, but it increments the stack pointer by
8 (4 for the return address plus 4 additional), rather than 4.
A. We can see in lines 5-7 of the code for word_sum that it appears as if three values are being
retrieved from the stack, even though the function has only a single argument. Describe
what these three values are.
B. We can see in line 4 of the code for prod that 20 bytes are allocated in the stack frame.
These get used as five fields of 4 bytes each. Describe how each of these fields gets used.
C. How would you describe the general strategy for passing structures as arguments to a
function?
D. How would you describe the general strategy for handling a structure as a return value from
a function?
https://class.coursera.org/hwswinterface-001/wiki/view?page=Homework2 5/5