I'm trying to implement a floating point multiply without using FP hardware instructions.
I think my code works for the sign bit and exponents bits, but not the mantissa.
The general idea:
1. Add exponents of those two numbers.
2. Multiply their mantissas.
3. Normalize the mantissa.
4. Add to exponent the part got from normalizing mantissa.
I ignore the sign bit for now since I test it on values higher than 0.
And here is the problem: I tried to multiply those two mantissas and then - since the result would be in two registers edx:eax - shifting bits one by one from edx to eax meanwhile increasing exponent. But it doesn't seem to work, so I wonder if my idea is good, or maybe there is some better way to do it?
Here is what I have already written in MASM:
mov eax, [ebp+8] ;put into eax one of numbers to multiply
mov ecx, a ;in ecx is second number to multiply, constant = 1.8
and ecx, 7F800000H ;mask to get exponent
and eax, 7F800000H
shr ecx, 23
shr eax, 23
sub ecx, 127
sub eax, 127
add ecx, eax ;exponent of the final number - later should be added part got from mantissa
mov eax, [ebp+8]
mov edx, a
and eax, 007FFFFFH ;getting mantissa
and edx, 007FFFFFH
; editor's note: unsure if there were any unlisted instructions
; between the two code in the original
mul edx ; multiply the mantissas
mov ebx, 0
spr:
cmp edx, 0 ;check if edx is cleared out
jne przesun
je dalej
przesun:
inc ecx
shr eax, 1 ;making space for new bit
shr edx, 1 ;put bit to CF
bts eax, 31 ;putting bit from CF ; Bug #1, see Michael's answer
jmp spr
dalej:
shr eax, 7
shl ecx, 23
add eax, ecx ;result of multiplying
The result is 0 for every number I tried multiplying with 1.8.
(atm I test it on number 15, so the result should be 27)
^
BTSdoesn't do what you seem to think it does.Quoting from Intel's manual (emphasis added):
So you're always setting the bit to 1, regardless of the value of the bit you just shifted out.
There are other instructions that you can use to accomplish what you're trying to do:
or: