Question

I am trying divide two integers and get a floating point number, then display the floating in a results section. Problem is, that it now displays as a really large integer, and I have no idea if I am doing it right.

Code:

definitions:

floatVal         REAL4   0.0f
numCount         SDWORD   0 
nextNum          SDWORD   0
totalSum         SDWORD   0
averageNum       SDWORD   ?

code in question:

fild totalSum
fidiv numCount
fstp floatVal

then later to display:

mov     edx, OFFSET average_1
call    WriteString
mov   eax, floatVal
call      WriteDec
call      CrLf

I am using the irvine32 library, and this is my first attempt at trying to use float in assembly language.

Was it helpful?

Solution

While a 32 bit float accidentally fits into the eax register, the WriteDec function expects an integer and hence interprets the bit pattern as such producing unintended output. Floats do not directly map to integers that way.

You should either convert the result to integer or find a way to print floats.

If you are just interested whether you are doing it right use a debugger. The code you posted seems correct, by the way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top