Computer Science 220
Assembly Language & Computer Architecture
Fall 2010, Siena College
For this lab assignment, you will learn how to write and run a simple C program, and then write a few more programs to try out some of its bitwise operators.
You may work alone or with a partner on this lab.
The first few sections of this lab handout contains some examples that you should work through during lab. Once you have completed those, you can move on to the C programming assignments. However, even if you have not gone through the examples (maybe you're in the Friday lab), you can start thinking about how to solve these problems. Maybe even solve them using your favorite programming language and convert them to C later.
A Very Simple C Program
We will begin by seeing how to compile and run a very simple C program (hello.c) in a Unix environment.
See Example:
~jteresco/shared/cs220/examples/hello
We will assume that we are working at the Unix command line. If you have not already done so, you will need to boot into Linux and open a Terminal window.
For you to run this, you will want to copy the example to your own directory. But first, we'll create a directory for it:
mkdir ~/hello
This creates a new directory (folder) in your home directory
(indicated by the ~
) called hello.
To copy the example from the shared area to your new directory:
cp ~jteresco/shared/cs220/examples/hello/hello.c ~/hello
We now change directory to the copy in your own directory:
cd ~/hello
And compile and run it:
gcc hello.c ./a.out
Things to note from this simple example:
gcc hello.c
In this case, we're asking gcc to compile a C program found in the file hello.c.
Since we didn't specify what to call the executable program produced, gcc produces a file a.out. The name is a.out for historical reasons.
./a.out
gcc -o hello hello.c
Here, the executable file produced is called hello.
\n
results
in a new line.
A Bit More Complex Example
We next consider an unnecessarily complicated C program that computes the greatest common denominator of two integer values.
See Example:
~jteresco/shared/cs220/examples/gcd-c
Lots of things to notice here:
When executing, functions from both gcdmain.c (main) and gcd.c (gcd) will be used. Both of these are included in our executable file gcdmain.
gcc -c gcd.c
gcd.o is a compiled version of gcd.c, but it cannot be executed.
C (and many other languages) require a two steps for source code to be converted into an executable. The first step compiles source code into object code, the second takes a collection of object code files and links together the references in those files into an executable file. (There's much more to discuss here, but this should suffice for now.)
#include "gcd.h"
.
'\0'
.
%d
's in the format string),
and put them into the place pointed at by the address
of a and the address of b, then return the number of
values that matched the input with the correct format."
Right. And you thought I/O was a pain in Java.
&
operator. Don't worry, it will make better sense
when you see more examples.
%s
, which means to expect an additional parameter
which is a character string (well, really a pointer to a
null-terminated array of char). Here, the string is
argv[0], the first command-line parameter, which is
always the name of the program. This labels the error
message with the program name.
%s
's, so we
have two additional parameters to fprintf, both pointers
to strings.
gcc -c gcdmain.c
This produces the object file gcdmain.o. We need to link together our two object files, which, together, have the function definitions we need:
gcc -o gcdmain gcdmain.o gcd.o
This gives us gcdmain, which we can run.
The bad news: that was a lot of trouble just to write a simple program.
The good news: you will have examples to look at and you can ask a lot of questions.
C Programming Assignments
The main part of the lab is to write two C programs. The first should be fairly straightforward, but the second will require some more careful thought. For both, you should try to come up with an elegant and simple solution. Avoid expensive operations like multiplication, division, and nested loops. Keep in mind that bitwise operations are the focus of this assignment.
If you're having trouble, ask questions! Make sure that your code is well written and documented. I encourage, but will not require, that you develop them using Unix. You are, however, required to make sure your programs run properly on the Linux systems in Roger Bacon 306 before you submit them.
Write a C program access.c that reads in a five digit access code as a decimal number and writes out each of the five digit access codes that are equivalent (there are 32). Make sure that you print any leading zeros. Again, your program may prompt for the input value or accept it on the command line. You may assume that the value on the input is an integer.
Submission and Evaluation
This lab is graded out of 25 points.
By 10:00 AM, Friday, September 24, 2010, submit documented source code for your two programs. All necessary files should be submitted by email to jteresco AT siena.edu.
Grading Breakdown | |
counttheones.c correctness | 5 points |
access.c correctness | 8 points |
Efficiency, style and elegance | 7 points |
Documentation | 5 points |