浮点减法不一致地奏效

发布于 2025-02-07 14:20:47 字数 1878 浏览 1 评论 0原文

我试图在不使用浮点指令的情况下减去两个浮点数,但现在它仅适用于某些数字,例如10.9-2.5在8.4中结果,这是正确的,但5.7-2.5结果在7.2中。

我将指数均匀地制成,然后减去mantissas,然后将它们组合在一起。 目前,符号是自动0的,因为我首先想使其与正数一起使用,

这是代码:

; expands a floating point number into sign, exponent and fraction
%macro expand 1
    push rax
    mov eax, %1
    rol eax, 1
    mov byte [sign], al
    and byte [sign], 1
    rol eax, 8
    mov byte [exponent], al
    shr eax, 9
    or eax, 800000h
    mov dword [fraction], eax
    pop rax
%endmacro

; combines sign, exponent and fraction to make a floating point number
%macro combine 1
    push rsi
    push rcx
    push rax
    push rbx
    push rdx
    mov esi, %1
    xor ecx, ecx
    mov eax, [fraction]
    mov ebx, [exponent]
    mov edx, [sign]
    shrd ecx, eax, 23
    shrd ecx, ebx, 8
    shrd ecx, edx, 1
    mov dword [esi], ecx
    pop rdx
    pop rbx
    pop rax
    pop rcx
    pop rsi
%endmacro

section .bss 
    result resd 1

section .data
    source dd 5.7
    source2 dd 2.5

section .text
    global _start 

    _start:
        xor ebx, ebx
        expand [source]
        ; store expanded parts in these registers
        mov bl, [exponent]
        mov eax, [fraction]
        mov cl, [sign]
        expand [source2]

        ; make exponents even
        while:
            cmp [exponent], bl
            jnl endwhile
            inc byte [exponent]
            shr dword [fraction], 1
            jmp while
        endwhile:

        while2:
            cmp bl, [exponent]
            jnl endwhile2
            inc bl
            shr eax, 1
            jmp while
        endwhile2:


        ; make operand 2 negative and then add them together
        neg dword [fraction]
        add eax, dword [fraction]
        mov [fraction], eax
        mov [exponent], bl
        mov byte [sign], 0
        combine result
        mov edx, [result]

        exit

Im trying to subtract two floating point numbers without using floating point instructions but right now it only works with some numbers, for example 10.9 - 2.5 results in 8.4 which is correct but 5.7 - 2.5 results in 7.2.

I make the exponents even and then subtract the mantissas and then combine them.
For now the sign is automatically 0 because I first want to make it work with positive numbers

Here is the code:

; expands a floating point number into sign, exponent and fraction
%macro expand 1
    push rax
    mov eax, %1
    rol eax, 1
    mov byte [sign], al
    and byte [sign], 1
    rol eax, 8
    mov byte [exponent], al
    shr eax, 9
    or eax, 800000h
    mov dword [fraction], eax
    pop rax
%endmacro

; combines sign, exponent and fraction to make a floating point number
%macro combine 1
    push rsi
    push rcx
    push rax
    push rbx
    push rdx
    mov esi, %1
    xor ecx, ecx
    mov eax, [fraction]
    mov ebx, [exponent]
    mov edx, [sign]
    shrd ecx, eax, 23
    shrd ecx, ebx, 8
    shrd ecx, edx, 1
    mov dword [esi], ecx
    pop rdx
    pop rbx
    pop rax
    pop rcx
    pop rsi
%endmacro

section .bss 
    result resd 1

section .data
    source dd 5.7
    source2 dd 2.5

section .text
    global _start 

    _start:
        xor ebx, ebx
        expand [source]
        ; store expanded parts in these registers
        mov bl, [exponent]
        mov eax, [fraction]
        mov cl, [sign]
        expand [source2]

        ; make exponents even
        while:
            cmp [exponent], bl
            jnl endwhile
            inc byte [exponent]
            shr dword [fraction], 1
            jmp while
        endwhile:

        while2:
            cmp bl, [exponent]
            jnl endwhile2
            inc bl
            shr eax, 1
            jmp while
        endwhile2:


        ; make operand 2 negative and then add them together
        neg dword [fraction]
        add eax, dword [fraction]
        mov [fraction], eax
        mov [exponent], bl
        mov byte [sign], 0
        combine result
        mov edx, [result]

        exit

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文