如何浮点数转换为IEEE 754使用汇编 [英] how to convert floating-point number to IEEE 754 using assembly

查看:456
本文介绍了如何浮点数转换为IEEE 754使用汇编的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以请你帮我浮点数转换为IEEE 754使用汇编

can you please help me to convert floating-point number to IEEE 754 using assembly

我有这个数-1.75,我知道它equla到-1.11000000000000000000000 E + 0 IEEE754
但我不知道该怎么做了转换汇编

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

推荐答案

你的意思是这样的:

; 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

这篇关于如何浮点数转换为IEEE 754使用汇编的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆