CS 47 - T. Howell - Spring, 2008

Homework Assignment #1

Due Tuesday, February 12, 2008 by 23:59 as an email attachment hw1.zip (details below).

You are to write three short C programs and solve two exam-style problems. The programs are, h2d.c, d2h.c, and run_test_show_bytes.c. Details are as follows.

Programs #1 and #2: d2h.c and h2d.c

These programs should have similar functionality (with one exception, noted below) to the two Perl programs of the same names in the shaded "Aside" on pages 31-32 of the text. Note that the C versions will be a bit longer, since C, unlike Perl, does not make automatic conversions from strings (char*) to integers. Invalid strings provided by the user should result in an error message, but should not crash the program: it should merely continue on to the next input. You may use the library functions sscanf and printf from stdio.h if you wish. If a string begins with a valid number but has format errors, you may convert the valid portion and ignore the rest of that string. (This is the behavior of the functions in stdio.h.) Note that this behavior may be different from the Perl programs. If you want to play around with the Perl programs of the text, you can install Perl. Then all you need to do is find the appropriate code files on the textbook's student site, save them to files with the .pl extension (on Windows machines), and run them as scripts (but using the full extension, if any, with the file name).

Program #3: run_test_show_bytes.c

This program should run test_show_bytes (text p. 39) which calls show_bytes (text p. 37). You will need to add a program "main". It should read one integer parameter from the command line in either decimal or hexadecimal format and run test_show_bytes with that value as its argument. You may use the library function sscanf from stdio.h if you wish. An optional second command line parameter "squared" should allow you to run test_show_bytes on the square of the first parameter. (This will allow you to view the effects of overflow in integer arithmetic.) Any other command line parameters should be ignored. Note also that the program should not crash if an invalid parameter is given, but display an error message and exit.

Problem 1

Problem 1

Consider a 6-bit two’s complement representation. Fill in the empty boxes in the following table. Addition and subtraction should be performed based on the rules for 6-bit, two’s complement arithmetic.

 

Number

Decimal Representation

Binary Representation

Zero

0

 

n/a

-2

 

n/a

18

 

n/a

-30

 

n/a

 

00 1100

n/a

 

11 0100

TMax

 

 

TMin

 

 

Tmax + Tmin

 

 

Tmin +  1

 

 

Tmax + 1

 

 

-Tmax

 

 

-Tmin

 

 

 

 

Problem 2  (similar to Practice Problem 2.27)

Fill in the following table showing the results of multiplying different three-bit numbers in the style of Figure 2.21.

 

Mode

x

y

x · y (6-bits)

Truncated x · y

Unsigned Two’s comp.

[010]

[010]

[011]

[011]

 

 

Unsigned Two’s comp.

[110]

[110]

[101]

[101]

 

 

Unsigned Two’s comp.

[111]

[111]

[011]

[011]

 

 

 

Submission requirements and grading system

You are to email me your programs as a zip file hw1.zip, which should contain the text file and three source code files only, with the exact names as given above, and with no paths embedded in the zip file. (If you don't know how to create a zip file with these requirements, you should see me; if you don't have a zip utility on your Windows machine I recommend ZipGenius or WinZip.) You should print out the problems and submit your solutions in written form at the end of your class period. Your email must be sent to me by 23:59 on Tuesday, February 12. Your email must have the following subject line:
CS 47 Homework #1 John Doe
but of course with your own name instead of "John Doe." Please also preserve all spacing and capitalization in this subject line.

As with any code you submit, each code file must be appropriately documented with a header comment containing (as a minimum) your name, the class and section number, the homework problem number, and a brief description of the problem.

I will grade the programs according to three criteria: execution tests (60%), source code (30%), and documentation (10%). I believe I have specified these three programs very precisely above, but if you have any questions about the behavior of the programs it is your responsibility to ask me before the submission date.

Failure to observe any of the submission requirements stated above may result in a grade of 0 on this homework!

Happy programming!