CS31 Weekly Lab: Week 6

Week 6 lab topics:

  1. switch statements in C
  2. tools for examining binary files

Start up a terminal and ssh into your CS machine:
  ssh -Y you@your_machine.cs.swarthmore.edu
Once remotely connected to cs, you can start other xterms on our system by:
   $ xterm &
Then create a week06 subdirectory in your weeklylab subdirectory and copy over some files:
    cd cs31/weeklylab		
    pwd
    mkdir week06
    ls
    cd week06
    pwd
    cp ~newhall/public/cs31/week06/* .
    ls
C Switch Statements
Let's open up swtich_stmt_example.c in vim (or emacs). A switch statement can sometimes be used to replace a chaining if-else if statement. Often a switch statement is easier to write than a chaining if-else if, and it is more efficient to execute. However, not every chaining if-else if can be replaced be a switch statement. The restriction is that each case expression must be a constant expression (e.g. it cannot be an arithmetic or logical expression).

Here is the general form of a switch statement:

switch ( expression ) {
  case constant-expression1 : 
      statements when expression ==  constant-expression1 
      break; // a break statement is often used at the end to break out 
              // of the body of the switch statement  (execute next_stmt next) 
              // if no break statement here, then the very next instruction in sequential order
              // will be executed next (the stmt immediately after case constant-expression2:) 
  case constant-expression2 : 
      statements when expression ==  constant-expression2 
      break;
  ...
  case constant-expressionN : 
      statements when expression ==  constant-expressionN 
      break;
  default :   // default is optional, like the last else is in if-else
       default statements
}
next_stmt;
Here is an example:
switch(x+8-y) {   
  case 3:         // start executing code here when (x+8-y) == 3
    x = x + 10;
    break;
  case 5:        // start executing code here when (x+8-y) == 5
    x = y + 10;
    break;
  case 7:        // start executing code here when (x+8-y) == 7
    x = y - x;
    break;
  default:       // executed when (x+8-y) is not equal to one of 3, 5, or 7
    x = 2*y - 6;
}


Tools for examining binary files Redux
A reminder of some of the tools for examining binary files:
  1. strings: dumps all the strings in a binary file
      strings a.out
    
  2. nm (or objdump -t) to list symbol table contents
      nm --format sysv a.out          # dump the symbol table in the a.out file
      objdump -t  a.out               # dump the symbol table in the a.out file
    
  3. ddd and gdb: examine runtime state, registers, individual instructions, memory contents

    See the week 4 lab page for more information and examples. Here are a summary of some of the most useful commands:

      ddd a.out
      (gdb) break main
      (gdb) run  6                # run with the command line argument 6
      (gdb) disass main
      (gdb) break theSwitchWay2   # set a break point at the begining of a function
      (gdb) cont
      (gdb) break *0x0804851a     # set a break point and memory address 0x0804851a
      (gdb) ni                    # execute the next instruction
      (gdb) si                    # step into a function call (step instruction)
      (gdb) info registers
      (gdb) p  *(int *)($ebp + 8)   # print out the value of an int at addr 0x88760
      (gdb) x/d $ebp + 8            # examine the contents of memory at the given address 
                                    # (interpret it as an int)
      (gdb) p %eax
    
Some more information on Tools for examining .o, .so, and a.out files
Some more information on gdb and ddd: gdb Guide

Figure 3.30 on p.255 of the textbook lists gdb commands.