CS 366 - Second SPIM Programming Assignment

Due: Monday, February 17, 2003 at 11:59 pm

For this assignment, we will decode the different parts of a floating point number. Each number input by the user will be written out in two ways:

  1. As a 32 bit hexidecimal value (in 8 hex digits) showing the IEEE Single Precision Floating Point Format.
  2. As a value showing the sign (either "+" or "-"), mantissa (in binary showing both the floating point and the leading one or zero) and exponent of the number (this can be output as the proper signed decimal value).

Since the SPIM simulator only outputs numeric values in decimal, you will need to output the hex and binary values as a character string.

Your program should prompt the user for a floating point number. This number should be read in as a SINGLE PRECISION floating point number. Your program should then output the number in the two specified ways. Then your program is to prompt the user if they wish to repeat the operation with another floating point number. The user is to answer with either a y for yes or an n for n. An answer other than y or n should be treated as an n for no. If the answer is no, quit the program. If the answer is yes, loop back to the top and re-run the program.

A single precision floating point number is stored in a sign and magnitude format in 32 bits. The magnitude has two parts: the fractional part of the mantissa and the exponent.

The floating point value of zero is stored when the exponent and fractional part of the mantissa contain only zero bits (all bit values for bits 30 through 0 are zero). This does allow for a representation for both positive zero and negative zero (which is common for any sign and magnitude style format).

Let us look at the single precision floating point representation for 0.625.

     0.625 = 0.5 + 0.125
           = 1/2 + 1/8
           = 0.101      (in binary)
           = 1.01 * 2-1 (normalized)

     sign:     positive
     mantissa: 1.01                                    (in binary)
     exponent: -1                                      (in decimal)

     encoded sign value: 0
     encoded mantissa value: 1.01
                               01                      (fractional part only)
                               01000000000000000000000 (extending to 23 bits)
     encoded exponent value: -1 + 127 = 126            (in excess-127)
                                      = 01111110       (in binary)

     Entire encoded value: 001111110010000000000000000000000
     Translation:          seeeeeeeeffffffffffffffffffffffff
       where: s indicates the sign bit
              e indicates the exponent
              f indicates the fractional part of the mantissa
The output given by your program for the value of 0.625 could be as follows. The non-bold information can be given in any readable format, while the bold information should be output as shown.
     For the floating point value of 0.625
     The encoded value in hex is: 3f200000
     Which is +1.01000000000000000000000 * 2^-1
The creation of the hex value has nothing to do with the value being in sigle precision floating point format. The same algorithm could be used for displaying any value from a register in hex.

The programs will be graded based on how they execute on xspim running in the CS Department computers. You are to submit your program electronically using the the turnin command with the project name of mp2. Your program must be well commented, use blank lines to separate logical sections of your code and align labels, opcodes, operand and comments in neat columns.

Alternative Register Names in SPIM

This is from Figure 9.9 in the Goodman&Miller text
Register NameAlternative NameDescription
$0the value 0
$1$atreserved by the assembler
$2 - $3$v0 - $v1expression evaluation and function results
$4 - $7$a0 - $a3the first four parameters -
  not preserved across procedure calls
$8 - $15$t0 - $t7temporaries -
  not preserved across procedure calls
$16 - $23$s0 - $s7saved values -
  preserved across procedure calls
$24 - $25$t8 - $t9temporaries -
  not preserved across procedure calls
$26 - $27$k0 - $kreserved for use by the operating system
$28$gpglobal pointer
$29$spstack pointer
$30$s8saved value -
  preserved across procedure calls
$31$rareturn address
$f0 - $f2floating point function results
$f4 - $f10temporaries -
  not preserved across procedure calls
$f12 - $f14the first two floating point parameters -
  not preserved across procedure calls
$f16 - $18temporaries -
  not preserved across procedure calls
$f20 - $f30saved values -
  preserved across procedure calls

Input and Output in SPIM

I/O is done in SPIM using the syscall opcode. For each I/O operation, a specific value is placed in the $2 register (also referred to as $v0). Depending on the I/O operation to be performed, other registers may be involved. The following table lists the possible syscall services.
ServiceSystem Call Code
placed in $2/$v0
Arguments Results
print_int1$a0 = integer
print_float2$f12 = float
print_double3$f12 = double
print_string4$a0 = address of string
read_int5integer (in $v0)
read_float6float (in $f0)
read_double7double (in $f0)
read_string8$a0 = address of string buffer
$a1 = length of string buffer
sbrk9$a0 = amountaddress (in $v0)
exit10
Spim Input/Output Example Program

The following special characters are used with character strings in SPIM. The special characters follow the C language convention:
CharacterEncoding Sequence
Newline\n
Tab\t
Double quote\"