CS 61C (Fall 2007)

Lab Assignment 6

Goals

This lab should help you examine how computers store integer and floating-point values.

Reading

Info

Recall that the single precision floating point number is stored as:

    SEEE EEEE EIII IIII IIII IIII IIII IIII

where S is the sign bit, 0 for positive, 1 for negative; E is the exponent, bias 127; I is the significand, with an implicit 1.  For example, the floating point representation of 1.0 would be 0x3F800000. You should verify to yourself that this is correct.

MARS has a built in floating point representation tool, which can be accessed from the tools menu.  You will find this very helpful.

Setup

Work with a partner on these exercises.

Copy the contents of ~cs61c/files/lab/6/ to a suitable location in your home directory.

Exercise 1

Find the shortest sequence of MIPS instructions to determine if there is a carry out (not overflow) from the addition of two registers, say $t3 and $t4. Place a 0 or 1 in register $t2 if the carry out is 0 or 1, respectively. (This can be done in just two instructions). Verify that your code works for the following values:

Operand Operand Carry out?
0x7fffffff 0x80000000 no
0xffffffff 0 no
0xffffffff 1 yes

Also, explain why you do not need to compare both values (hint hint).

Exercise 2

Find a positive floating point value x for which x+1.0=x. Verify your result in a MIPS assembly language program, and determine the stored exponent and fraction for your x value (either on the computer or on paper).

Note: The provided MIPS program floatadd.s will allow you to experiment with adding floating point values. It leaves the sum in $f12 and also in $s0, so you can examine the hex representation of the floating point value by printing $s0.

Exercise 3

Next, find the smallest positive floating point value x for which x+1.0=x. Again, determine the stored exponent and fraction for x. Explain why this is the smallest value. If we were working with double precision floating point (1 sign bit, 11 exponent bits, and 52 mantissa bits), in contrast to the single-precision float, what would the value be?

Exercise 4

Finally, using what you have learned from the last two parts, determine a set of positive floating point numbers such that adding these numbers in a different order can yield a different value. You can do this using only three numbers. (Hint: Experiment with adding up different amounts of the x value you determined in exercise 3, and the value 1.0).

This shows that for three floating point numbers a, b, and c, a+b+c does not necessarily equal c+b+a.

If time permits, you should write a program to add these three values in different orders. It should be a straightforward modification of the program from exercises 2 and 3.