Last active
October 25, 2019 22:05
-
-
Save soachishti/b4bf2f55b99ef123a1a3 to your computer and use it in GitHub Desktop.
Print Float Binary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; Print exponent if exponent greater than 10 | |
; positive and negative exponent | |
Include Irvine32.inc | |
; Change value of number and see result | |
.data | |
divisor DWORD 10b | |
;number DWORD 1.5 | |
;number DWORD 2.5 | |
;number DWORD 3.5 | |
number DWORD -13.5123 | |
mantissa DWORD 0 | |
leftMantissa DWORD 0 | |
rightMantissa DWORD 0 | |
exponent DWORD 0 | |
res DWORD 0 | |
beforePoint DWORD 0 | |
tmp DWORD 0 | |
.code | |
main PROC | |
;mov eax, number | |
;call WriteBin | |
;call Crlf | |
;http://stackoverflow.com/questions/15685181/how-to-get-the-sign-mantissa-and-exponent-of-a-floating-point-number | |
; Identify sign | |
mov eax, number | |
shr eax, 31 | |
jz positive | |
mov al, '-' | |
jmp lb_exit1 | |
positive: | |
mov al, '+' | |
lb_exit1: | |
call WriteChar | |
; Getting exponent | |
mov eax, number | |
shr eax, 23 | |
and eax, 0FFh | |
add al, -127 | |
mov exponent, eax | |
;call WriteDec | |
;call Crlf | |
; getting mantissa | |
mov eax, number | |
and eax, 07FFFFFh ; get mantissa from num | |
shl eax, 8 | |
or eax, 80000000h | |
mov mantissa, eax | |
;call WriteBin | |
;call Crlf | |
mov ecx, exponent | |
lp2: | |
shl eax, 1 | |
cmp cl, 0 | |
pushfd | |
; if ecx > 0 | |
jna increase | |
dec ecx | |
increase: | |
; if ecx < 0 | |
jnb decrease | |
inc ecx | |
decrease: | |
popfd | |
jnz lp2 | |
shr eax, 9 | |
mov rightMantissa, eax | |
;call WriteBin | |
;call Crlf | |
;call WriteBin | |
;call Crlf | |
mov eax, mantissa | |
mov ecx, exponent | |
lp3: | |
shr eax, 1 | |
inc ecx | |
cmp ecx, 31 | |
jnz lp3 | |
mov leftMantissa, eax | |
;call WriteBin | |
;call Crlf | |
; Convert right mantissa to int | |
mov ecx, 22 | |
lb: | |
bt rightMantissa, ecx | |
jnc lb1 | |
mov eax, 1000000000 | |
xor edx, edx | |
div divisor | |
add res, eax | |
;call WriteDec | |
;call Crlf | |
lb1: | |
shl divisor, 1 | |
dec ecx | |
cmp ecx, -1 | |
jne lb | |
mov eax, res | |
mov rightMantissa, eax | |
mov eax, leftMantissa | |
call WriteDec | |
mov al, '.' | |
call WriteChar | |
mov eax, rightMantissa | |
call WriteDec | |
ret | |
main ENDP | |
END main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment