# Computer Systems Review Exercises
*These exercises are optional. You may do them in a group, complete them alone,
or skip them entirely.*
## Exercise 1
Write a function in C that finds the maximum value in a list. The signature
for the function should be:
```
// Max gives the max of `num_ints`
int max(int *ints, int num_ints);
```
Write and test this function (and only this function, no main function) in a
file named max.c.
## Exercise 2
Compile your max function into assembly-language form using the following
command:
```
gcc -S max.c
```
The result will be a file, max.s, which contains the assembly-language code
for the project.
Comment the code with enough detail so that a programmer who has no experience
with assembly could follow along. For example, detail what each operation
means, talks about where items will be laid out on the stack, what purpose
each register serves, etc.
Make sure you can answer the following questions:
1. Where is each of the arguments ints and num_ints passed (i.e., in which
register, on the stack, etc..)
2. Describe in words or as an ASCII-art diagram, the stack layout at the
invocation of the function max
3. Where is each local variable stored in the function, specifically in
relation to the base pointer?
## Exercise 3
I have provided a program, max_main.c, which will use
your max.s file. To compile max_main.c in a way such that it can use your
max.s code, first compile max.s from assembly to binary code:
```
gcc -c -ggdb max.s
```
The -c tells gcc to compile the file to binary code, but not to link it.
Linking is the process by which the compiler grabs up all of the different
functions, including the main function, and splices them all together into a
binary that you can actually run. But since max.s doesn’t have a main
function, gcc can’t fully link it. The -g tells gcc to compile the file with
debugging information (-ggdb tells it to do so for use by GDB).
Now you have a binary file max.o, which you can link with the main function in
max_main.c. The following command compiles max_main.c and links it with max.o:
```
gcc -ggdb max.o max_main.c -o max
```
The output is now a file you can run named max.
Read GDB’s documentation on the following commands:
* The ```x``` command, which allows you to examine memory.
* The ```layout ``` command, which allows you to interactively run the program
* The ```print``` command, which allows you to print out values and memory.
* The ```set``` command, which allows you to set memory.
* The ```info``` command, and some of the things it can do (print stack frames / registers).
* The ```break``` command, which allows you to set breakpoints.
Make sure you can answer the following questions:
1. How would you find the address of a program variable named string?
2. How would you set the integer value stored at address 0x10000000 to the
value 0x23?
3. How would you find out where the saved instruction pointer is stored?
4. How would you get GDB to disassemble the code starting at address %rip+200
and ending at %rip+400?
*Hints:* If you want to use gdb inside your max function, you may need to
recompile it with the ```-ggdb``` flag set. If you find that you can't access
variables, it is probably because you are using different versions of gcc and
gdb and thus gdb is unable to interpret the debugging information. Try updating
your programs to the same version.
#####Exercises developed by Kristopher Micinski.