Floating point numbers

In our Intel processors there is a stack of eight floating point 64-bit registers ST(0) .. ST(7). Using the registers explicitly is not straightforward, because a register can change its identifier when it is used. Raymond Filiatreault gives a thorough explanation in his tutorial entitled Simply FPU. However, there is an easy way to use floating point registers without referring to them by name. For example, the following sequence adds two numbers together. The prefix F indicates a float operation, LD represents load (of the current register) and ST stands for store.

FLD Num1
FSUB Num2
FST Difference

The following code demonstrates how to input a real number as a string and then use the a2r8 function to convert it to the type REAL8. (The space reserved needs to be of size QWORD). The printf macro is very useful for displaying floating point numbers. We use .2 to display the results to two places of decimals.

include \masm32\include\masm32rt.inc
.686
.data
  strNum1 db 20 dup(?),0
  strNum2 db 20 dup(?),0
  ptrNum1 DWORD strNum1
  ptrNum2 DWORD strNum2
  Num1 QWORD 0
  Num2 QWORD 0
  Sum QWORD 0
  Difference QWORD 0 
  Product QWORD 0
  Quotient QWORD 0
.code
START:
MOV ptrNum1, input("Enter real number: ")
MOV ptrNum2, input("Enter second real number: ")
;Convert to float (REAL8) then store.
MOV EAX, a2r8(ptrNum1) 
FLD REAL8 PTR [EAX]
FST Num1
MOV EAX, a2r8(ptrNum2) 
FLD REAL8 PTR [EAX]
FST Num2

FADD Num1 ;Num2 loaded already
FST Sum

FLD  Num1
FSUB  Num2
FST Difference

FLD  Num1
FMUL  Num2
FST Product

FLD  Num1
FDIV Num2
FST Quotient

printf("Sum: %.2f\n", Sum)
printf("Difference: %.2f\n", Difference)
printf("Product: %.2f\n", Product)
printf("Quotient: %.2f\n", Quotient)
inkey "Press any key to exit"
exit
RET
CALL START
END START
Programming - a skill for life!

Getting started with the MASM assembler, MASM demonstrations (including console games and floating point numbers) and ARM assembly language