Program 2
The purpose of this second SAL programming assignment is:
- to learn more about character I/O,
- to learn about the IEEE standard used to represent floating point numbers,
- to learn about bit manipulation using masks and logical operations.
Program Statement
Write a SAL program that reads a simplified form of a base ten floating point number and prints a simple version of its internal IEEE representation. Assume that the input number consists of from 1 to 6 decimal digits, followed by a decimal point, followed by from 1 to 6 more decimal digits, terminated by '\n'. All other input should be reported as "bad input." Use getc to read the characters in the number. Build the number and store its value in a .float type variable. Your program should print out the internal representation of the number in the form:
1.bbb bbbb bbbb bbbb bbbb bbbb * 2^e
where the leading 1 is the hidden bit in the IEEE representation and the b's are the 23 bits in the significand. The e is the biased-127 value of the exponent.
NOTE 1: We have simplified the "standard" definition of a floating point number to make this programming assignment somewhat easier. Using our definition, the following are not legal base 10 floating point numbers: +2.1, -54.06 (can't have a sign), 70., .003 (need a digit before and after the ".").
NOTE 2: The little e is a base 10 integer and its value can be positive or negative. If it is negative, -5 for example, write the 2^e part as 2^-5.
Sample Runs
Bold indicates user input, italics indicate annotation, everything else would be printed by your program.
input a special float: 2.25 answer is: 1.001 0000 0000 0000 0000 0000 * 2^1 all done or input a special float: 134.0625 answer is: 1.000 0110 0001 0000 0000 0000 * 2^7 all done or input a special float: 0.1234 answer is: 1.111 1100 1011 1001 0010 0100 * 2^-4 all done or input a special float: +134.0625 <- can't use a '+' sign bad input all done or input a special float: 1234567.0625 <- too many digits before '.' bad input all done or input a special float: .0625 <- need at least one digit before '.' bad input all done or input a special float: a#$,*123 <- bad characters bad input all done
Requirements
- You may only use getc, putc and puts (not get or put), that is, all I/O must be character I/O except for the printing of messages.
- You must use a SAL procedure, called getFloat for example, to read the floating point number entered by the user.
- You must use a SAL procedure, called prtFloat for example, to print the "1.bbb bbbb bbbb bbbb bbbb bbbb * 2^e" form of the number.
- You must use a SAL procedure, called prtInt for example, to print the integer value of e, including a '-' sign if one is needed (there is no need to print a '+' sign if e is positive).
- Your main program should call getFloat. If the form of the input is OK, it should next call prtFloat. The prtFloat procedure should call prtInt to print the value of e. You should return from prtInt to prtFloat to main and then quit. Your procedures should all use the generic return method: "b (addr)".
Hints
- You don't need to convert the float to its internal representation. Using the declaration .float for the variable in which you build the number does the job for you.
- To get at the parts of the internal representation of the floating point number, first use move to copy the bit pattern of the floating point number to a variable of type .word. Then use the SAL shift and logical instructions, with the appropriate masks, to extract the desired parts of the new variable.
- To implement the prtInt procedure, first determine the largest power of 10 in the integer, for example, if you were trying to print the integer 123, the largest power of 10 in the integer would be 10^2=100. Then compute 123/100=1 and print the character '1'. Next reduce the number by subtracting 1*100 to get 23. Then compute 23/10=2 and print the character '2'. Reduce the number by 2*10, compute 3/1=3, and print '3'. Reduction by 3*1 gives 0 so quit.
Handing in your program
You must "hand in" your program (the SAL source code) to the computer directly by
cp p2.s ~cs354-1/handin/username/P2/.
just once, where "p2.s" is the name of the file containing your SAL source code. No printouts will be turned in. I will run your program several times using different test data.