2014-05-08 53 views
-1

可以請你幫我把浮點數轉換爲IEEE 754使用匯編如何浮點數轉換爲IEEE 754使用匯編

我有這個數-1.75,我知道它equla到-1.11000000000000000000000è + 0在IEEE754 ,但我不知道該怎麼做了轉換匯編

+0

沒有指定的指令集。 「彙編」不是一種語言,我不認爲你想聽到這是如何在PowerPC中完成的(或者也許你想聽到的是......) –

+0

你想要轉換的格式* from * ?十進制字符串? – harold

+0

@PascalCuoq對不起,我忘了它我使用MASM 32位 –

回答

2

你的意思是這樣:

; 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 
相關問題