CS140 Lab 10 - Machine Level Representation of Programs, Part III
Objective
- Experiment machine level representations of programs.
- Use gcc, gdb, and objdump tools
- Understand struct and array data representation and calculations
|
Background and Perspective
|
Perspective:
All data is stored in memory addresses. Complex data structures require the compiler
to calculate where an element is stored in memory.
This lab focuses on understanding the stack as well as struct and array storage mechanisms.
Background:
This material is from Chapter 3 in CS:APP textbook.
You will find the following reference slides useful:
This lab picks up where Lab 08 and Lab 09 left off. Please refer to the previous lab's instructions as needed.
|
Required Tasks
Experiment with Data Structures
- Log into your Linux account on one of the CS machines.
- Create a directory for your lab10 experiments.
- Create a text or word document for your notes (turn-in DUE at the end of lab!)
- You will want some PAPER to draw memory layouts.
- Keep detailed notes of each experiment!
In this directory there are several C files containing data structure manipulation.
- Compile and execute the programs and/or compile into assembly language
- Examine the resulting .s file to understand the resulting assembly code.
- Reference the slides linked above and/or the textbook for a relevant explanation.
- Write notes in your file explaining how the assembly code implements the control strucure.
Do the following experiments:
- struct.c Begin by running and executing the program as-is. Add code to the program to
print the memory offsets of each element in the struct. Explain WHY the offsets are the way they are.
- Modify struct.c by re-arranging the order of the elements in the stack to optimize for space.
- struct2.c Compile and understand what's happening in stack2.c. Examine the assembly code and/or disassemble an executable in gdb and step through it. Write down an equation for calculating the elements in an array of structs.
- array.c Using what you have learned from the above experiments, write an equation to calculate where the memory address is located. Write an equation for a memory address.
- Modify array.c to use an array of shorts or an array of ints. Repeat the experiment.
- Compile and then disassemble with the option -fno-stack-protector to make it easier.
- Execute the program and see if you can find the location of the return address on the stack. Where is it? What address is stored there?
If you have time:
- Modify array.c to use a 2D array and observe how the calculations of memory addresses take place.
- Modify the above to use a 2D array as an array-of-arrays and observe how the calculations are different. See the Data slides for examples.
Check out
When you are done with the lab exercises, turn in your notes on Moodle. This is due at the end of the lab session today (1:15pm). Just turn in what you have done for lab credit even if you did not finish.