我有一个关于如何从十进制浮点数转换为IEEE754格式的代码

时间:2019-05-28 23:51:15

标签: assembly dos x86-16 ieee-754

我应该将小数转换为-2.25,结果为1 |。 10000000 | 00100000000000000000000

这是我的尝试,但是没有用

.data
    String db "enter num $",0
    DecStr dd ?
    BinStr db 40 dup (0)
    result dd ?
    number dd 0
    frac dd 0
    divisor dd 1

.code
main PROC     
    mov ax,@data
    mov ds,ax 

    mov ah, 9
    lea dx, String
    int 21h       

    mov ah, 0ah
    mov dx, DecStr
    int 21h




    start:                         ; integer part
    mov si, offset DecStr
    xor dx, dx
    mov al, byte ptr [si]
    xor ah, ah
    inc si
    test al, al                 ; end of string?
    jz retRes                    ; yes: end of parsing
    cmp al, '.'                 ; decimal point?
    je fraction                       ; yes: end of loop (jump forward to the next @@)
    cmp al, '-'                 ; negative?
    je start                       ; yes: next character (jump backward to the last @@)
    xor bx,bx
    imul bx:dx, 10              ; prior result * 10
    and al, 0Fh                 ; eliminate ASCII-part
    add dx, ax                  ; add eax store result
    jmp start                   ; loop: next character (jump backward to the last @@)

    fraction:                   ; end of loop
    mov number, dx              ; fractional part
    mov cx, 1                   ; divisor for later division
    xor dx, dx                  ; holds the result

    binn:
    mov al, byte ptr DecStr[si]       ; loop
    xor ah, ah
    inc si
    test al, al                 ; end of string?
    jz retRes                   ; yes

    and al, 0Fh                 ; eliminate ASCII-part
    imul bx:cx, 10              ; "increment" divisor
    xor bx,bx
    imul bx:dx, 10              ; prior number * 10
    add dx, ax                  ; new number
    jmp binn                    ; loop (jump backward to the last @@)

    retRes:
    mov frac, dx                ; for 'fild'
    mov divisor, cx             ; for 'fidiv'
    div divisor                 ; correct fractional number
    add cx,number               ; add integer part
    cmp byte ptr DecStr, '-'    ; negative?
    jne ress                    ; no: jump forward to the next @@ 
    xor number, 0x8000          ; change sign of st(0) to '-'
    mov ax, number
    mov result, ax              


    ress:
    mov ax, number 
    mov result, ax

    ToBinStr:                  
    mov si, offset BinStr      ; first position in string
    mov ax, result             ; result

    ; Sign
    push cx
    mov ch, cl
    mov cl, 7
    sub cl, ch
    mov bx, ax
    shr bx, cl                   ; carry if sign-bit is set
    jnc binnStr                  ; jump if positive (forward to the next @@)
    mov bl, '-'                  ; minus-character
    mov BinStr[si], bl                 ; store it into BinStr
    inc si                       ; increment pointer to BinStr

    binnStr:
    ;integer part & point
    mov bl, "1"                ; store '1.' into BinStr
    mov BinStr[si], bl
    add si, 2                  ; increment pointer to BinStr

    ; fractional part
    mov cx, 7                 ; first bit position for bt
    and ax, 7Fh              ; isolate mantissa

    binn1:
    push cx
    mov ch, cl
    mov cl, 7
    sub cl, ch
    mov bx, ax
    shr bx, cl
    and bl, 1
    or bl, 0x30
    pop cx
    mov BinStr[si], bl            ; store it into BinStr
    inc si                      ; next position in string
    dec cx                      ; next bit
    jns binn1                     ; loop (jump backward to the last @@)
    pop bx

    ; exponent
    add si, 2                  ; increment pointer to BinStr
    mov al, byte ptr result + 2
    xor ah, ah                  ; 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 storeSign                   ; jump if positive exponent (forward to the next @@)
    mov bl, '-'
    neg al                      ; abs(al)

    storeSign:
    mov BinStr[si], bl               ; store sign into BinStr
    inc si                     ; increment pointer to BinStr

    mov cx, 7                  ; first bit-position for 'bt'
    binn2:
    push cx
    mov ch, cl
    mov cl, 7
    sub cl, ch
    mov bx, ax
    shr bx, cl
    and bl, 1
    or bl, 0x30
    pop cx
    mov BinStr[si], bl            ; store it into BinStr
    inc si                      ; next position in string
    dec cx                      ; next bit
    jns binn2                   ; loop (jump backward to the last @@)
    pop bx
    jmp printres

    printres:
    mov ah,9
    lea dx,BinStr ; print result
    int 21h

main ENDP

END main

0 个答案:

没有答案