组装中的分割时出现浮点异常(核心转储) [英] Floating Point Exception (Core Dumped) while doing division in assembly

查看:95
本文介绍了组装中的分割时出现浮点异常(核心转储)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试添加2个两位数数字,这些数字必然会产生两位数或三位数的数字。

I'm trying to add 2 two-digit numbers which are bound to yield a two-digit or three-digit number.

这是我到目前为止的内容,当我尝试打印该进位时,它显示浮点异常(转储核心)

Here's what I have so far, and when I try to print the carry, it says Floating Point Exception (Core Dumped)

section .data
    msg db "Enter 2 numbers: "
    msgLen equ $-msg

section .bss
    numa1 resb 1
    numa2 resb 1
    numb1 resb 1
    numb2 resb 1
    carry resb 1

section .text
    global _start

_start: 
    ;print message
    mov eax, 4
    mov ebx, 1
    mov ecx, msg
    mov edx, msgLen
    int 80h

    ;accept first number (1st digit)
    mov eax, 3
    mov ebx, 0
    mov ecx, numa1
    mov edx, 1
    int 80h

    ;accept first number (2nd digit)
    mov eax, 3
    mov ebx, 0
    mov ecx, numa2
    mov edx, 2
    int 80h

    ;accept second number (1st digit)
    mov eax, 3
    mov ebx, 0
    mov ecx, numb1
    mov edx, 1
    int 80h

    ;accept second number (2nd digit)
    mov eax, 3
    mov ebx, 0
    mov ecx, numb2
    mov edx, 2
    int 80h

    ;character to number conversion
    sub byte[numa1], 30h
    sub byte[numa2], 30h
    sub byte[numb1], 30h
    sub byte[numb2], 30h
    sub byte[carry], 30h

    ;;;;;;;;;;;;;;;;;;;;;;;;;;

    ;add ones digit
    mov al, [numa2]
    add byte[numb2], al
    add byte[numb2], 30h

    ;get carry of sum of ones digit
    mov ax, [numb2]
    mov byte[carry], 10
    div byte[carry]

    mov eax, 4
    mov ebx, 1
    mov ecx, carry
    mov edx, 1
    int 80h


    mov eax, 1
    mov ebx, 0
    int 80h







  carry
  numa1   numa2
+ numb2   numb2
---------------
          numb2

where numb2 = numb2 % 10
      carry = numb2 / 10


推荐答案

首先, xor ebx,ebx mov ebx短和快,0

还加上两个1位数的结果最大进位为1(9 + 9 = 18),因此无需除以,只需将数字减10就足够了。此外,通常应避免使用16位寄存器,因为这既更长(由于使用了66h前缀),又比较慢(由于部分寄存器更新)。这意味着

Also, adding two 1-digit numbers results in a maximum carry of 1 (9 + 9 = 18), so no need to divide, just subtracting the number by 10 is enough. Moreover you should generally avoid 16-bit registers, which is both longer (due to the 66h prefix) and slower (because of the partial register update). That means

mov ax, [numb2]
mov byte[carry], 10
div byte[carry]

会比以下慢很多

movzx eax, word ptr [numb2]
sub   eax, 10

但是,毕竟,当x86已经具有 BCD时,为什么要使用这种复杂的方式说明。此外,这些指令具有用于BCD数学的AF和CF,因此无需自己进行管理。

But after all, why do you use such a complex way when x86 already has BCD instructions for that purpose. Besides, those instructions have AF and CF for BCD math carry so no need to manage it on your own

您还可以直接使用二进制数学,只需在输入/输出。在大多数情况下,转换成本可以忽略不计

You can also use binary math directly and just convert them at input/output. Most of the case the cost of converting is negligible

这篇关于组装中的分割时出现浮点异常(核心转储)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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