CS 31: Weekly Lab: Week 2

  1. Review of gdb print and C types for lab 1
  2. Overview of the logisim simulator for lab 2

I emailed each of you a CS machine assignment. As much as possible, when you ssh into CS please ssh into the machine to which you have been assigned. Start up a terminal and remotely log into your assigned machine on our system:
  ssh -Y your_user_name@your_machine_name.cs.swarthmore.edu
See the week 1 Mon/Tues lab for more information about remotely logging into CS lab machines.

Once remotely connected to cs, you can start other xterms on our system by:

   $ xterm &
Then create a week02 subdirectory in your weeklylab subdirectory and copy over some files (the ls and pwd commands just let you see where you are in the directory structure):
    cd cs31/weeklylab		
    pwd
    mkdir week02
    ls
    cd week02
    pwd
    cp ~newhall/public/cs31/week02/* .
    ls

Types, GDB print and examine commands

gdb print can be used to convert values from one representation to another. For example, you can print out the value of a numeric expression in its decimal, bit, or hexadecimal notation.

We are going to try this out on a C program with different variable types:

int main () {

   int x, i;
   unsigned int y;
   unsigned char uch;
   float f;
   int array[10];      // an array (like Python list) of 10 int values 
   char ch;
   char *s = "Hello There";  // a string in C  

   x = y = ch = uch = f = 1;

   array[0] = x;

   // C for loop:
   for(i = 0; i < 10; i++) {
       array[i] = i*i;
   }
	  
}
The C for loop:
   for(init_stmt;  loop_condition;  count_expr) { 
     loop body 
   }
is evaluated:
  1. execute the init_stmt once before enter the loop
  2. evaluate the loop_condition
    if true:
    1. execute the statements in the loop body
    2. execute the the count_expr
    3. goto (2)

    if false: drop out of the loop

Let's compile and run the a.out file inside the gdb debugger:

$ gcc -g types.c
$ gdb ./a.out
(gdb) break main
(gdb) run	
(gdb) break  20    # set a break point at line 20
(gdb) cont         # continue execution until hit a breakpoint

(gdb) print x
(gdb) print y
(gdb) print (x - 2)
(gdb) print (y - 2)

(gdb) next
(gdb) print x
(gdb) print/t x	        # print in binary (note: leading 0's are not printed
(gdb) print/x x         # print in hexidecimal

(gdb) p/t   x            # p is shorthand for print in gdb 

(gdb) p/t  2 + (8*23)    # you can print any arithmeitic expression

(gdb) p    0x2a + 10     # 0x is the prefix for a hexidecimal literal
(gdb) p    0b1010 + 10   # 0b is the prefix for a binary literal
(gdb) p    0b1010 + 0b10   

(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 

(gdb) set x = 10          # can change the value of a program variable

# the examine 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

The Logisim simulator

This week we will start using the Logisim simulator to create circuits and to see what the does given different input. We will learn more Logisim in class, but today I want to step through a few examples using it.

The Logisim web page has user documentation that will be very helpful for the next lab assignment. We are using version 2.7.

$ logisim    # start new project

$ logisim  proj_file.circ  # edit an existing project
We will start with firstcircuit.circ
$ logisim  firstcircuit.circ  
Next, step through the Beginner's tutorial and try building the XOR circuit.