Elementary C Programming
Elementary C Programming
Goals: This laboratory is designed to:
-
Provide some experience working with digital logic at the
bit level,
-
Give practice writing simple C programs, and
-
Provide experience using the
gcc compiler.
Steps for this Lab:
Since C does not contain a designated Boolean data type, integers are
used. By convention, the integer value 0 represents false, and any
nonzero integer value is interpreted as true.
Program ~walker/211/labs/prog1.c includes code to print part
of a truth table.
-
Copy this program to your account, and compile it with the statement:
gcc -o prog1 prog1.c
Then run the program by typing prog1 .
Note: The compiling and running steps may combined with the command:
gcc -o prog1 prog1.c && prog1
-
Add the names of those in your group as comments at the start of the
program. Then complete the truth table by defining and using the following
functions:
void nor (int a, int b, int * res)
void and (int a, int b, int * res)
int or (int a, int b)
In each case, the function should perform the designated Boolean operation.
Specifically, define nor directly with C operations. Then
write and and or by calling any or all of
not, nand, and nor.
-
Write a program that reads two 2-bit numbers (a1a0 b1b0) and computes their
sum using only the operations
not, nor, nand, and and
or as defined in the previous program. That is, follow the logic
for a gate-level adder to perform the addition.
To further simulate gate-level circuitry, consider each variable as the
name of a separate wire in a circuit diagram. That is, follow these rules
in writing your program:
To simplify input, you may assume each 2-bit number is entered with a space
between the bits, so reading of a number may be accomplished with the
statement :
scanf ("%d %d", &x1, &x0);
Work to be turned in:
-
The program and appropriate test runs from parts 2 and 3. In each case,
use the following format.
Form for listing, compiling, and running programs:
-
Use the
submit command to record the activity in a
dtterm window.
-
Use
cat filename to display the program in the dtterm
window.
-
Compile the program with the
gcc command.
-
Run the program sufficient times to show it runs correctly.
-
After printing the above session, write (by hand) on the output a
justification of why your testing demonstrates that your code is correct.
This document is available on the World Wide Web as
http://www.math.grin.edu/~walker/courses/211/labs/lab.c-programming.html
created September 26, 1997
last revised October 1, 1997