CS 370 Lab 2 - x86-64 disassembly


This lab is to be done individually.

Starting in a Terminal window (which by default sets the working directory to your home directory), make a new directory called CS370, unless one already exists.  In either case, change to this directory and type pwd (simply displays the current working directory).  Now create a new directory, Lab2.

Download the file showBytes.c into your new directory.

Compile showBytes.c in the usual way and run it to see what it does.

Now compile the showBytes.c program twice more, renaming the assembly file before doing the second compilation.  So, in this order:
  1. gcc -S showBytes.c
  2. mv showBytes.s showBytes.noOpt.s
  3. gcc -Og -S showBytes.c
Now using a text editor, compare the output of these compilation steps and answer these questions:
  1. Using showBytes.noOpt.s look in the function show_bytes: for an instruction that uses the immediate addressing mode. Write down this instruction.

  2. Still using showBytes.noOpt.s look in the function show_bytes: for an instruction that uses the normal addressing mode. Write down this instruction.

  3. Now look in the function show_bytes: for an instruction that uses a displacement addressing mode. Write down this instruction.

  4. In the function main: there is a call to a function that does not appear to be called in the C code. Which function is it? Why do you think this might be happening?
  5. Also in  main: there is a form of mov that we haven't seen, with a register we also haven't seen before (because we've only looked at general-purpose registers). Write down the instruction and Google the register name to see what it's purpose is (or as close as you can figure).

  6. In showBytes.noOpt.s look for a pattern to the first few instructions - ignoring statements that begin with .cfi - in each function (show_bytes, show_int, etc. and main). 
    Write down the pattern:

  7. Now look for the pattern in showBytes.s instead. What changed for most of the functions?

  8. How does the assembly deal with string literals (such as format strings used by printf) in the C code? Give an example.

  9. At what optimization level (-O1, -O2, -O3, -Og, -Ofast) does an if statement NOT get compiled into a conditional move like cmovle? Compile this code (absdiff.c) with each optimization flag and examine the assembly to find out. Hint: do you know how to use grep?

  10. Look at the main: function in showBytes.noOpt.s. There is a huge (just yuuuuge) number being moved into %rax, you can't miss it. Based on which function is next called, speculate on what this number represents.

  11. Extra Credit (which most everyone could use): In a new C file, write a function that includes this while loop transformed into a goto. That is, do the transformation by hand, and compile the transformed loop. Save and compile with the -S and -Og flags. Email the C program and resulting assembly file to me.
    long transformedLoop(long n) {
       long answer = 1;
       while (n > 1) {
          answer *= n;
          n = n - 1;
    return answer;