# Computer Organization Lab #2: Floating Point

1. Add to your library.asm a function that takes a single precision floating point value (in eax) and negates it by directly manipulating the bits of the number's representation. Modify your main program (in C) to check if your function is working.

2. Add to your library.asm another function that takes a single precision floating point value (in eax) and doubles it by directly incrementing the exponents bits of the number's representation (Don't try this at home!) Modify your main program (in C) to check if your function is working.

3. Floating point operations tend to be slow. Bit manipulation tends to be fast. You might expect that the "tricky" functions above would be faster than their corresponding floating point operations (for example 2.0*number). Let's investigate that. Modify your main program so that it uses your function to double the floating point value 1.0F 10 times in a row. Do not use a loop! Then enclose that code in a loop that runs, say, 10,000,000 times (you might have to adjust the number of times).

To measure how long it takes this code to run you can use my Timer library. Download the files Timer.h, Timer.c, and environ.h, and add them to the folder containing your code for this lab. Modify your Makefile so that Timer.c is included in the build process.

Include the header "Timer.h" into your C main program and write something like this:

```        Timer stopwatch;
Timer_initialize( &stopwatch );
Timer_start( &stopwatch );
// Do stuff
Timer_stop( &stopwatch );
```

Now you can call Timer_time( &stopwatch ) to get the number of milliseconds that elapsed between the start and stop operations. The Timer_time function returns a long integer. Note that this library has low resolution. It must time intervals that are at least a few seconds (ideally tens of seconds) to be accurate. This is why you might have to adjust the number of times your loop runs.

Use wdis to disassemble the object file of your main program as produced by the C compiler. See if you can locate your loop (look for the ten calls to your function in a row). Can you follow how the loop works?

4. Repeat the measurement above for the more natural code number = 2.0*number and compare the result. Are the bit manipulations actually faster? You might be surprised. How long does each doubling operation require in both cases?

Again use wdis to disassemble the object file produced by the C compiler and compare it to that for your earlier program. What (important) differences do you see?

Pack both your main C program (either version) and your library.asm into a zip archive. Include, perhaps in commments (or a separate document), your timing results along with any comments you have about the disassembled code that you viewed or the results you obtained. Submit your zip archive to Moodle.

Last Revised: 2017-02-01