# Computer Science 237 Computer Organization

### Williams College Fall 2005

Lab 1: Binary Numbers and C Practice
Due: Monday, September 19, 2005 at 11:00 AM

Working with Numbers

Please answer each of the following problems, and turn in your solutions on this paper. (Don't forget to write your name on it.)

1. Represent each of the following quantities using each of the 8 bit representations, if possible. All values are base 10 (unless noted), and characters are ASCII (hint: see man ascii):

 Signed Quantity Unsigned S. Mag. 1's Comp 2's Comp 0 11 -42 42 -127 127 -128 128 255 DB16 (a positive) '6' (see man ascii)

2. Compute the following binary 2's complement problems in 4 bits. Circle those answers that are suspect.

``` 0001      0001      1001      1111
+0101     +0111     +1111     +0101
----      ----      ----      ----

```
3. Compute the following binary 1's complement problems in 4 bits. Circle those answers that are suspect.

```  0001      0001      0000      1000
+0101     +0111     +1111     +1111
----      ----      ----      ----

```
4. The following problems are expressed in 8 bit, hexadecimal, 2's complement. Compute the result as 16 bit 2's complement expressed in hex. Again, circle suspect answers.

```  FF        FF        01        4c       7f       80
+ 11      + FF      + 7F      x 04     x 7f     x 80
----      ----      ----      ----     ----     ----

```
5. True or false: 0.1 (base 10) can be represented as a floating point number within the computer. Justify your answer.
6. True or false: The signed product of two signed n-bit numbers can always be correctly expressed as a signed 2n-bit number. Justify your answer.

Unix and C practice

If you are not familiar with the CSLab Mac and FreeBSD systems, spend some time getting familiar with those systems. We will be using both during the semester. There is nothing to turn in for this part of the lab.

1. Go through the Unix tutorial handout. This will teach you how to log in and out of the machines, use basic Unix commands, and edit files with Emacs.
2. Log into a Mac in TCL 216. Start X11, the X Window System, by clicking on the rectangular while icon with an X on it in the dock at the bottom of your desktop. This will bring up an "xterm" command window.
3. Log into a FreeBSD system in TCL 312. You have a separate account there (separate password, home directory). Log into one of the systems (you can go up to the lab or use ssh). There are several options for the X11 environment here. Most students use KDE.
4. To set up your account to find the tools we, you should enter the command
```source /usr/cs-local/bin/237
```
in the command window each time you log in. Rather than type this command every time, you can make it happen automatically by adding it to the file .local_bashrc in your home directory.
5. Identify the function of and experiment with these Unix Commands:
```
ls         cd         cp         mv         rm         mkdir      pwd
man        chmod      cat        more       grep       head       tail
ln         find       rmdir      wc         diff       tar
```

Identify the function of and experiment with these Emacs Commands:

```
C-x C-s    C-x C-c    C-x C-f    C-x C-w    C-g        C-a        C-e
C-d        C-_        C-v        M-v        C-s        C-r        M-%
```

Learn these commands - you will use them often. Hints can be found in the Unix and Emacs web pages on the course website.

6. Make a directory in your account on both the Macs and the FreeBSD systems for CS 237 work (perhaps "237" or "cs237" might be reasonable). Use the chmod command to restrict access to this directory so only you can read the files. Ask a classmate, TA, or instructor to verify that he or she cannot see the contents of this directory.
7. Write a "Hello, world!" program in C and run it on both the Mac and FreeBSD systems. In each case, the C compiler "gcc" should be available.

C Programming Assignments

The last part of the lab is to write two C programs. Approach these problems as best you can as a computer scientist. Make sure that your code is well written and documented.

1. Write a C program prime.c that reads an integer from the input and prints out whether the number itself if prime, then prints the primes that are closest to it (next smaller and next larger). You might use this in a data structures course if you're looking to size a hash table. Note that there no primes less than 2. Once you have the program working, modify it so that it accepts its input as a command-line parameter. If no command-line parameter is supplied, the program should prompt for it, as was done in the original version.
2. Write a C program access.c that reads in a five digit lab access code as a decimal number that writes out each of the five digit access codes that are equivalent (there are 32). Be aware that 1 and 2 are on the same button, as are 3 and 4, etc. 9 and 0 are on the same button as well. Make sure that you print any leading zeros. Again, your program should first check for the input on the command line. If none is provided, it should prompt for it.

You may assume that the value on the input is any integer.

Submit these from the CS Lab FreeBSD systems using the turnin utility:

```    turnin -c 237 prime.c
turnin -c 237 access.c
```
Your programs will be graded based on correctness, documentation, structure, and performance.