Computer Science 220
Assembly Language & Computer Architecture
Fall 2011, 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. You may wish to solve them using your favorite programming language and convert them to C later to separate the problem solving from the learning curve of a new programming language.
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. The pow function, which can be used to compute powers, is not permitted. 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.
For example, suppose the menu features burgers for $2, chicken strips for $3, fries for $1, and soft drinks for $1, and your budget allows you $5 per meal. Of the 16 possible combinations of 0 or 1 of each item that form the possible meals with this menu, on your budget you can afford 13 (all but burger/chicken/fries, burger/chicken/drink, and all 4).
Write a C program valuemenu.c that takes as command-line parameters one int specifying your budget followed by anywhere from 1 to 26 additional ints specifying the price of each menu item. Your program should print its inputs, labelling the items A, B, C, etc. It should then print the number of possible combinations. Next, it should print each possible combination, listing the selected items and their costs, followed by the total cost of that meal and whether or not it is under budget. At the end, print the number of affordable combinations.
Submission and Evaluation
This lab is graded out of 25 points.
By the start of your next lab meeting, 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 |
valuemenu.c correctness | 8 points |
Efficiency, style and elegance | 7 points |
Documentation | 5 points |