CS31 Weekly Lab Week 2

gdb, prinf, computer teardown
Log in and create a cs31/weeklylab/week02 subdirectory and copy over some files from my public/cs31/week02/ directory into your subdirectory:
    cd
    cd cs31
    cd weeklylab
    mkdir week02
    cd week02
    pwd
    cp ~newhall/public/cs31/week02/* .
    ls
    Makefile  printtypes.c	testprog.c

Goals for this week:

  1. Quick overview of Bit-wise operators.
  2. Learn more C types and printf formats (on your own)
  3. Learn how to use gdb print different representations and to convert between representations
  4. Discover the parts of a computer
C bit-wise operators
Bit-wise Operations:  AND (&) , OR (|), NOT (~), XOR(^)

        A       B       A & B     A | B     ~A         A ^ B
        ------------------------------------------------------
        0       0         0         0        1           0
        0       1         0         1        1           1
        1       0         0         1        0           1
        1       1         1         1        0           0


smallest unit is really a byte so do bitwise ops on some number of bytes

     01010101           01101010      10101010        ~10101111
   | 00100001         & 10111011    ^ 01101001         ---------
   -----------         ----------     ---------        01010000
     01110101           00101010      11000011


Bit shift:

   val  << amt   
   val  >> amt

        10110111  << 2   is 11011100

        000111 >>2   is 00000001

signed (arithmetic) right shift: keeps sign bit

       100011111 >>2   is 111000111

in a C program would usually shift a variable value:

int y, x = 10234;
    y = x >>2;


C types and printf
The file printtypes.c has some examples of printf format strings for printing out values of different types. We are not going to spend much time looking at this today, but use it as a reference for printing out values in the lab 2 assignment. Below is just a little info about printf and C types.

C has many different types for storing integer values. These types differ by the number of bytes they use to store values, and by weather or not they store both positive and negative values.

1 byte: 2 bytes: 4 bytes: 4 or 8 bytes:
(depends on arch)
8 bytes:
char short int long long long
unsigned char unsigned short unsigned int unsigned long unsigned long long

When you allocate a variable of a specific type, you get a storage location of the appropriate number of bytes associated with that variable name:

int x;             // 4 bytes of storage space to store a signed integer value 
unsigned char ch;  // 1 byte of storage space to store an unsigned integer value

printf formatted output

printf has different placeholders for specifying how a value should be printed (how its series of bytes be interpreted). See the printtypes.c for examples. Here is a brief summary:
### Specifying the numberic representation:
   %d: print out value in decimal  (base 10)
   %u: print out value in unsigned decimal  (base 10)
   %x: print out value in hexidecimal  (base 16) 
   %o: print out value in octal (base 8)
   (there is no formatting option to display the value in binary)

### Specifying the type:
   %d:   int     (to print numeric values of int, short, and char args)
   %ld:  long int
   %lld: long long int
   %u:   unsigned 
   %lu:  long unsigned
   %llu: long long unsigned
   %p:   an address value 
   %f:   float or double 
   %lf:  double 
   %e:   float or double in scientific notation 
   %g:   float in either %e or %f format 
   %c:   char (ex. 'x') 
   %s:   string  (ex.  "hello there")

### Specifying field width: 
   %5d: print out the value in decimal in a field of with 5
   %-5d: print out the value in decimal in a field of with 5, left justified
   %6.4f: print out a float in a field with of 6 with a precision of 4 

C debugger: gdb
The GNU debugger, gdb, is the C debugger we will use in this class. Usually, we will use gdb to debug a program, but this week we are going to use gdb as calculator.

gdb's print command can be used to print the value of a expression in different representations (binary, decimal, hex); you can use it as a simple calculator to verify answers to hex, binary, and decimal arithmetic. For this use of gdb, we don't have to have an executable to run gdb on. We can just run gdb, and then call its print command:

$ gdb 
# print an expression in different representations:
# (/t in binary, /x  in hexidecimal, default is decimal):
(gdb) print/t 1234       # print/t: print decimal value 1234 in binary format
                         # p is shorthand for print
(gdb) p/x 1234           # p/x: print value in hexidecimal format
(gdb) p/d 1234           # p/d: print value in decimal format (the default)

# 0x is the prefix for a hexidecimal literals
# 0b is the prefix for a binary literals
# no prefix:  for decimal literals
(gdb) p 0xabf1           # print the hex value abf1 as decimal 
(gdb) p 0b0101           # print the binary value 0101 as decimal 
(gdb) p/t 0x1234         # print the hex value 0x1234 as binary
                         # (note: leading 0's are not printed out)
(gdb) p/d 0b1010         # print the binary value 01010 as decimal
(gdb) p/x  0b10100000    # print a binary value in hex  
(gdb) p/t 0b101001001 + 0xa2  # add a binary and a hex value, print result in binary

# you can re-cast a value as a specific C type:
(gdb) p/t (char)(12)    # tell gdb that the value 12 is a char (1 byte) and print as binary
(gdb) p/t (char)(-12)   # print -12 char value (1 byte) in binary

GDB with an executable file, print, set, and examine commands

You can also use gdb with an executable. In fact, this is usually how you will use gdb--to debug a C program. For lab 2, running gdb with an executable may be useful if you want to use program variables in an expression.

Today we will try out gdb on an executable file, learn some basic gdb commands. Over the course of the semester we will learn a lot more gdb. Let's open up testprog.c in vim, and take a look at it. Next, let's compile it and run it inside gdb (note: we want to compile with -g when using gdb):

$ make
$ gdb ./testprog
(gdb) break main   # sets a breakpoint in main
(gdb) run	
(gdb) list         # list out program source code around breakpoint
(gdb) break  31    # set a break point at line 31 
(gdb) cont         # continue execution until hit a breakpoint
(gdb) next         # execute the next instruction, then gdb gets control again
(gdb) list

(gdb) print x      # print out the values of program variables and expressions 
(gdb) p (y - 2)

(gdb) p/t  x	             # some bit-wise operators
(gdb) p/t  !x
(gdb) p/t  x & 0b10101010   
(gdb) p/t  x | 0b10101010
(gdb) p/t  x ^ 0b10101010
(gdb) p/t  x << 2     # bit shift
(gdb) p    x << 2     

(gdb) p    uch          # operations on  different C types
(gdb) p/c  uch
(gdb) p    uch+257        # this converts uch to an int, then adds to 257, the result is an int
(gdb) p   (char)(uch+257)  # re-cast the result as a char 

# set: to change the value of program variables in gdb:
(gdb) set x = 10          
(gdb) p x
(gdb) p/x x - 30

# the examine (or x) command is similar to print but takes a memory address 
# (or a variable location) as its argument
(gdb) x &x          # examine memory at the memory address of the variable x
(gdb) x &y    
(gdb) x/s s         # the name of a string or array is a synonym for its address
(gdb) x/d s
(gdb) x/d array
(gdb) x/d &array[3]   # the memory address of the 3rd bucket in array

(gdb) quit


Computer Teardown
Log out, and we are going to walk over to the Robot Lab. You have been randomly assigned to one of four groups. Each group is assigned a computer that you can take apart. The goal is to find as many parts of a computer as you can. We have tools available to remove parts from your computer, and here are a few links that may be helpful:

Try to identify some of the following:

Before leaving lab, please clean-up all spare parts, screws, etc. that you have removed by puting them in the boxes. You do not need to put the cases in the boxes, just all loose parts. CPU thermal glue is toxic, so just to be extra safe I'd recommend washing your hands after lab today.