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


In this directory there are several C files containing data structure manipulation.
  1. Compile and execute the programs and/or compile into assembly language
  2. Examine the resulting .s file to understand the resulting assembly code.
  3. Reference the slides linked above and/or the textbook for a relevant explanation.
  4. Write notes in your file explaining how the assembly code implements the control strucure.
Do the following experiments:

  1. 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.
  2. Modify struct.c by re-arranging the order of the elements in the stack to optimize for space.
  3. 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.
  4. 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.
  5. Modify array.c to use an array of shorts or an array of ints. Repeat the experiment.
  6. Compile and then disassemble with the option -fno-stack-protector to make it easier.
  7. 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:
  1. Modify array.c to use a 2D array and observe how the calculations of memory addresses take place.
  2. 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.