Computer Science 220
Assembly Language & Computer Architecture

Fall 2011, Siena College

Lab 1: C and Bitwise Operations
Due: the start of your next lab meeting

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:

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:

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.

  1. Write a C program counttheones.c that takes a signed int value and prints out the number of bits in the number's internal representation that have the value 1. Your program may prompt for the input value of accept it on the command line. You may assume the value on the input is an integer.
  2. As a college student, you probably find yourself dining at restaurants that feature some sort of "value menu". Also as a college student, you want to get as much food from those value menus as your budget will allow. But even a poor college student needs some variety, so your goal might be to try as many combinations of items from the value menu as your budget will allow, but without repeating any one item on a given visit.

    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