# Programming - Assembly Floating Point Register Example (Trading Profit Calculator)

Hello its a me again Drifter Programming. Today we get back to MIPS Assembly to talk about Floating Point Registers and Instructions. Because the topic is simple we will actually just talk about the Registers and Instructions and then get into an Example. The example will be a simple Trading Profit Calculator where we get a Buy and Sell price as floats and print out the profit %. So, without further do, let's get straight into it!

## FP Registers:

Beside the standard 32 Registers we talked about in my first post here some months ago, we also have 32 Floating Point (FP) Registers named in the same way as \$f0 - \$f31. These are in a chip outside of the main processor called FPA (Floating Point Accelerator) and let us do calculations on Floating Point numbers.

The first FP register \$f0 doesn't have a value of 0 like we had in the other registers and we can use them as 32-bit single-precision registers or combine them in pairs \$f0, \$f2, ...,\$f28, \$f30 (even numbered registers) to get 64-bit double-precision registers.

Other then that we use them in the same way as the other Registers, but with different instructions that are specialized for the use on those.

## FP Instructions:

The FP Instructions are split into single and double-precision ones. We mostly just have to add a .s or .d on the already known instructions

Load, Store and Immediate Load can only be done with one register (single-precision) and the instructions look like that:

li.s   fd, val  # load immediate/constant float value into fd

The rest of the instructions can be found here.

Here a small version with single-precision instructions:

• add.s fd ,fs, ft     # \$fd = \$fs + \$ft
• sub.s fd, fs, ft     #  \$fd = \$fs - \$ft
• mul.s fd, fs, ft    #  \$fd = \$fs * \$ft
• div.s fd, fs, ft     #  \$fd = \$fs / \$ft
• neg.s fd, fs         #  \$fd = -\$fs
• mov.s fd, fs       # copy from \$fs to \$fd

By changing the .s with .d you get the double-precision instructions...

What about input and output of floats and doubles...

I don't know if you remember from my post about Input and Output Calls...

But, we use a syscall with code:

• 2 for printing float and 3 for printing double
• 6 for reading a float and 7 for reading a double

### FP Variables:

The same way as we used .word, .byte and .asciiz at the .data field, we can use .float to specify and allocate an address enough to store a floating point number. That way we can have floating point variables in our Assembly code. I will not use this today, but knowing the instructions and the way we do this using the other Registers and bytes or words, you can do this easily without any problem. We talked about Memory Instructions and so on in this post that I uploaded some months ago.

The code that follows simply gets a buy and a sell price as single-precision float input from the user and then calculates the profit using the following equation:

Assembly Code:

``.data``
``# Constant strings to be output to the terminal``
``buyPrice: .asciiz "Enter the Buy Price: "``
``sellPrice: .asciiz "Enter the Sell Price: "``
``profit: .asciiz "You have a "``
``profit_2: .asciiz "% profit\n"``

``.text``
``main:``

``# print message promt for getting buy price``
``li \$v0,4``
``la \$a0, buyPrice``
``syscall``

``# get the value from the user as float``
``li \$v0, 6``
``syscall``
``mov.s \$f1, \$f0``

``# print message promt for getting sell price``
``li \$v0,4``
``la \$a0, sellPrice``
``syscall``

``# get the value from the user as float``
``li \$v0, 6 ``
``syscall``
``mov.s \$f2, \$f0``

``# calculate profit``
``sub.s \$f3, \$f2, \$f1  # sell - buy``
``div.s \$f4, \$f3, \$f1  # sell - buy / buy``
``li.s  \$f5, 100.0``
``mul.s \$f6, \$f4, \$f5  # (sell - buy / buy)*100.0 ``

``# print out the profit``
``li \$v0,4``
``la \$a0, profit``
``syscall``

``li \$v0, 2``
``mov.s \$f12, \$f6``
``syscall``

``li \$v0,4``
``la \$a0, profit_2``
``syscall``

``# end program``
``li \$v0, 10``
``syscall``

If you run this on an MIPS Emulator like QtSpim or Mars you will get an output like that:

You will also get a "Negative Profit" value if you had a loss...

And this is actually it and I hope that you enjoyed it!

I think that after this we are now finished with most off the stuff that has to do with MIPS Assembly. Off course, there is even more yet to come and you should get ready for a Compiler Project soon!

Bye!

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now