问题
can you please help me to convert floating-point number to IEEE 754 using assembly
i have this number -1.75 and i know it equla to -1.11000000000000000000000 E+0 on IEEE754 but i dont know how to do the convert in assembly
回答1:
Did you mean something like this:
; Conversion of an ASCII-coded decimal rational number (DecStr)
; to an ASCII-coded decimal binary number (BinStr) as 32-bit single (IEEE 754)
include \masm32\include\masm32rt.inc ; MASM32 headers, mainly for printf
.data
DecStr db "-1.75",0
BinStr db 40 dup (0)
result REAL4 ?
number dd 0
frac dd 0
divisor dd 1
.code
main PROC
mov edi, offset DecStr
xor edx, edx
@@: ; integer part
movzx eax, byte ptr [edi]
inc edi
test al, al ; end of string?
jz ToFPU ; yes: end of parsing
cmp al, '.' ; decimal point?
je @F ; yes: end of loop (jump forward to the next @@)
cmp al, '-' ; negative?
je @B ; yes: next character (jump backward to the last @@)
imul edx, 10 ; prior result * 10
and al, 0Fh ; eliminate ASCII-part
add edx, eax ; add eax store result
jmp @B ; loop: next character (jump backward to the last @@)
@@: ; end of loop
mov number, edx
; fractional part
mov ecx, 1 ; divisor for later division
xor edx, edx ; holds the result
@@: ; loop
movzx eax, byte ptr [edi]
inc edi
test al, al ; end of string?
jz ToFPU ; yes
and al, 0Fh ; eliminate ASCII-part
imul ecx, 10 ; "increment" divisor
imul edx, 10 ; prior number * 10
add edx, eax ; new number
jmp @B ; loop (jump backward to the last @@)
ToFPU:
mov frac, edx ; for 'fild'
fild frac
mov divisor, ecx ; for 'fidiv'
fidiv divisor ; correct fractional number
fiadd number ; add integer part
cmp byte ptr DecStr, '-' ; negative?
jne @F ; no: jump forward to the next @@
fchs ; change sign of st(0) to '-'
@@:
fstp result ; store result
ToBinStr: ; http://commons.wikimedia.org/wiki/File:IEEE-754-single.svg
mov edi, offset BinStr ; first position in string
mov eax, result ; REAL4
; Sign
bt eax, 31 ; carry if sign-bit is set
jnc @F ; jump if positive (forward to the next @@)
mov bl, '-' ; minus-character
mov [edi], bl ; store it into BinStr
inc edi ; increment pointer to BinStr
@@:
; integer part & point
mov bx, ".1" ; store '1.' into BinStr
mov [edi], bx
add edi, 2 ; increment pointer to BinStr
; fractional part
mov ecx, 22 ; first bit position for bt
and eax, 007FFFFFh ; isolate mantissa
@@:
bt eax, ecx ; carry if bit is set
setc bl ; 1 if carry else 0
or bl, 30h ; convert to AsCII
mov byte ptr [edi], bl ; store it into BinStr
inc edi ; next position in string
sub ecx, 1 ; next bit
jnc @B ; loop (jump backward to the last @@)
; exponent
mov word ptr [edi], 'E ' ; store 'E ' into BinStr
add edi, 2 ; increment pointer to BinStr
movzx eax, word ptr result + 2 ; we need only the high word
shr ax, 7 ; exponent -> al
xor ah, ah ; the sign bit was left
sub al, 127 ; minus BIAS
mov bl, '+'
jnc @F ; jump if positive exponent (forward to the next @@)
mov bl, '-'
neg al ; abs(al)
@@:
mov [edi], bl ; store sign into BinStr
inc edi ; increment pointer to BinStr
mov ecx, 7 ; first bit-position for 'bt'
@@:
bt eax, ecx ; carry if bit is set
setc bl ; 1 if carry else 0
or bl, 30h ; convert to ASCII
mov byte ptr [edi], bl ; store it into BinStr
inc edi ; next position in string
sub ecx, 1 ; next bit
jnc @B ; loop (jump backward to the last @@)
printf ("%s\n",offset BinStr) ; masm32rt.inc
invoke ExitProcess, 0
main ENDP
END main
来源:https://stackoverflow.com/questions/23540139/how-to-convert-floating-point-number-to-ieee-754-using-assembly