These problems should be done on your own. We're not going to be grading them strictly (we'll mainly look at whether you attempted them). But they will be reinforcing knowledge and skills, so you should totally work through them carefully.
Problem 1:
This is a brief C exercise. (The labs will require comfort and fluency in C.) Below is a snippet of code:
void op1(int a, int b) {
a = a + b;
}
void op2(int* a, int* b) {
*a = *a + *b;
}
int main() {
int a = 1, b = 1, c = 1, d = 1;
op1(a, b);
op2(&c, &d);
return 0;
}
What are the values of a
, b
, c
, and d
right before the program exits? Since the purpose of this exercise is to build your skills, you should do this problem without compiling and running the program.
Problem 2:
Below is another snippet of C code:
int a[5];
a[0] = 0;
a[1] = 10;
a[2] = 100;
//COMMENT 1
a[3] = *a + 2;
a[4] = *(a + 2);
//COMMENT 2
What are the values stored in the array a
when the program reaches COMMENT 1? (Do we know all of the values?) What about when the program reaches COMMENT 2?
Problem 3:
In the following code snippet, where comments indicate line numbers:
uint64_t x = 2;
uint64_t y = 5;
uint64_t mul(uint64_t a, uint64_t b) {
uint64_t e = a * b; // L0
return e; // L1
}
uint64_t comp(uint64_t n) {
uint64_t e = 0; // L2
uint64_t f = 0; // L3
e = mul(x, n); // L4
f = mul(n, y); // L5
return e + f; // L6
}
int main() {
comp(2); // L7
}
Draw the stack, and show where the base pointer (
%rbp
) and the stack pointer (%rsp
) point right before L4 begins executing.Draw the stack, and show where the base pointer (
%rbp
) and the stack pointer (%rsp
) point right before thecall
instruction that transfers control to themul
function is executed (that is, after arguments have been placed in the appropriate registers). What line of source code does the instruction pointer (%rip
) correspond to in this case? Note that some lines of C can expand into multiple instructions; your answer should specify the line of C code that led to the compiler producing the instruction at the address pointed to by%rip
.Draw the stack right after
mul
’s prologue has finished executing. You can assume thatmul
was called from L4.Draw the stack right after
mul
’s epilogue has finished executing, but before theret
instruction has returned control to the caller. Also state what line of C source code the instruction pointer (%rip
) corresponds to in this case. You should assume thatmul
was called from line L4. Similar to part (b), your answer should specify the line of C that led to the instruction pointed to by%rip
being produced.
Problem 4:
Operations on files, such as read()
, write()
, and close()
, are typically implemented as system calls. Describe two of the benefits of having these operations managed by the operating system instead of individual processes.
Handing in the homework
Use Gradescope; you can enroll in our course with entry code 4J462V.