CS 47 - T. Howell - Spring, 2008
Homework Assignment #3
Due Thursday,
March 20, 2008 by 23:59 as an email attachment hw3.zip (details below).
Note: This file will look better in IE than in Firefox due to font problems beyond my control.
You are to write one short C file, big_mult.c
and two short assembly language files mull.s.
and subfac.s.
Details on each of these code files is as follows.
Problem #1: big_mult.c
You are to write a C program called big_mult.c that
multiplies two unsigned integers, x and y, read from the command line. The output is a pair of unsigned integers representing
the most significant and least significant 32 bits of the full 64-bit product x * y.
The inputs and outputs are to be given in hexadecimal format.
Your C program will take care of reading the inputs and printing the output, but it will call
a function mull.s to do the actual
multiplication. Your C program should use only int or unsigned int variables and should not do any
arithmetic.
The function defined by mull.s
should have the following declaration in C before the procedure
main.
void mull(unsigned int x, unsigned int y, unsigned int* low, unsigned int* high);
The least significant 32 bits of the product are to be assigned to
low, and the
most significant 32 bits of the product are to be assigned to
high.
Remember to put an appropriate header comment into your assembly file
(the ordinary C comment /* ... */
will work for assembly too). You will also need to read carefully the
description of the mull
instruction in the IA32
instruction set reference manual, or equivalent documentation.
One way to approach writing this assembly program is to write a similar program in C,
compile it to assembly code using the -S option, and modify the resulting assembly
code to do what you need. Your final assembly code should be very short and should
contain only one multiplication instruction
mull.
The compile command to test your programs will look like this:
gcc -Wall -std=c99 -o big_mult.exe big_mult.c mull.s
.
Here is a sample output to use in testing your programs.
C:>big_mult 2f432f43 629b03cb
2f432f43 x 629b03cb = 12345678 87654321
.
Problem #2: subfac.s
You are to write an assembly language program called subfac.s that
computes the subfactorial function of a nonnegative integer input n.
The subfactorial function is defined by the following recursion.
subfac(0) = 1
subfac(n) = n subfac(n-1) + (-1)n
The values of subfac(n) get large very rapidly as n increases, so your program
should return a 32-bit number
representing subfac(n) mod 232. In other words, just let your
32-bit register overflow.
Try to make your code as short and efficient as possible. You can
use recursion (have
subfac
call itself), but I recommend writing a loop that starts
from 0 and increments up to n. Try to get your assembly code for
subfac
to occupy at most 50 bytes (25 is possible). To see your code, use
gcc subfac.s -c
to produce object file
subfac.o , then use
objdump -d subfac.o .
The following C program will be used to test your
subfac
function.
int subfac(int n);
int main(int argc, char* argv[])
{
    int i = 1, n;
    if(argc < 2) {printf("Usage: subfac i1 i2 i3 ... (list of non-negative integers)");}
    while (i < argc)
    {
        /* printf("input %d = %s\n", i, argv[i]); */
        sscanf(argv[i], "%d", &n);
        if(n < 0) printf("%d out of range\n", n);
            else printf("!%d = %d\n", n, subfac(n));
        i++;
    };
    return(0);
}
Here is a sample output to use in testing your programs.
C:>subfac 0 1 2 3 4 5 13
!0 = 1
!1 = 0
!2 = 1
!3 = 2
!4 = 9
!5 = 44
!13 = -2004174364
.
Submission
requirements and grading system
As noted above, you are to email me
your programs as a zip file hw3.zip,
which should contain the three source code files
only, with the exact names as
given above, and with no paths
embedded in the zip file. Your
email must be sent to me by 23:59 on
Thursday, March 20.
Your email must
have the following subject line:
CS 47 Section X Homework #3 John Doe
but of course with your own name and section number instead of
"John Doe and X." 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, the
date, and a
brief description of the problem.
I will grade according to three criteria: execution tests (70%), source
code (20%),
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!