Fall 2013
Lab 1
Due Wednesday, September 4


I have posted some programs that contain useful code on piazza, but as of my writing this, only a few students have registered. I would like all students to register on piazza so I can get responses to my poll question, which is about how much additional instruction I need to do for the c programming language. To encourage students to register, I have made this worth 5 points on this lab.

The Lab Program

Write a program that takes two command line arguments, the first is an output file name and the second is a number, and writes the number out in little endian form to the file. One complication is that, in order to make some aspects of your life easier, the input is in hex. The good news about having the input in hex is that it will look a lot closer to the output, which will make testing easier. The bad news about hex is that you will need to figure out how to parse a number that is in hex. Example:

hexdump is the name of a program that lets you view binary files viewed as a hex file. It is worth doing a quick experiment with hexdump to get better acquainted with the program: try running hexdump on one of your source files (preferably a small one). For me:
cmw@paris:~/cs520/p1/lab2$ hexdump -C write_int.c
00000000  23 69 6e 63 6c 75 64 65  20 3c 73 74 64 69 6f 2e  |#include< stdio.|
The -C option is important. Try running hexdump on one of your source files without the -C option and see what it does. In order to interpret the output of hexdump, it may help to have access to an ASCII table, which is very easy to find on the internet. Hint: just type "ASCII table" into google and select one of the many choices.

Detailed instructions

  1. Log on to one of the computers in the lab.
  2. Open up a terminal
  3. Make a directory entitled lab2
    mkdir lab2
  4. Change to the lab2 directory
    cd lab2
  5. In the lab2 folder, create a file called write_int.c and write your program in this file. You are free to use any editor you can find on agate that you are happy with. Many students have been using SciTE, so if you have no preference for editor, that is a good place to start.
  6. To compile your file, in the lab2 directory type:
    gcc -g -Wall -o write_int write_int.c
  7. When you are done, submit your source file. In order to submit your file, you will need to be SSH'd in to agate.
    ~cs520/bin/DoSubmission.py lab2 write_int.c


Please submit whatever you have at the end of lab, even if it is incomplete. You have the rest of today to finish this assignment.


Stuff you may find helpful

Reading an Integer in from the command line

As I am sure you remember, part of last lab was to read in a number from the command line. If you are happy with your code for doing this, you should copy it! If you didn't do very well with reading an integer in from the command line, you can feel free to copy some of my code from lab 1, which is currently posted on piazza. You will find that learning how to splice programs together is an extremely useful skill.

Writing to a file

Another useful resource is stackoverflow.com. This website, while it does have its problems, is often quite useful. For example, this question shows much of the basics of what you need to do. As usual, you need to make sure you divine the correct lesson out of this, the person's code is fairly close to what you will need to do, except it contains one error. Fortunately, the first answer says what the error is, and how to correct it.

Writing to a file in little endian

As was previously mentioned, this program is very closely related to the previous lab, where you wrote something out to the command line in little endian. If you are still unsure about precisely how you should accomplish this, you should look at your code for lab 1, or my code from lab 1 if your lab 1 had problems. Instead of writing to stdout, you can instead write to the file in binary. Although there are a number of ways to write to a file, I found the function putc(int character, FILE* stream) to be useful for doing this.