浮点异常,但没有除零。它是什么?

时间:2018-01-27 22:06:58

标签: assembly

我正在学习低级的东西,我试图编写并执行一个简单的程序,它打印输入字符的ASCII码。

section .bss
    i   resb 1
    o   resb 8
    ; i for input, o for output

section .text
    global _start
_start:
    mov eax, 3
    mov ebx, 0
    mov ecx, i
    mov edx, 1
    int 0x80
    ; get one char

    mov ecx, o
    add ecx, 7
    mov edx, 0
    ; set ECX point at last of 8 bytes labeled o
    while:
        mov al, [i]
        mov bl, 0
        cmp al, bl
        jz  end
        mov bl, 10
        div bl
        add ah, '0'
        mov [i], al
        mov [ecx], ah
        dec ecx
        inc edx
        jmp while
    end:
    ; algo - to decimal

    mov eax, 4
    mov ebx, 1
    int 0x80
    ; ECX ready - was decremented until highest non-zero digit in loop
    ; so is EDX - incremented digit count times

    mov eax, 1
    mov ebx, 0
    int 0x80

成功编译(nasm)和链接(ld),但在运行时:

Floating point exception (core dumped)

一旦已经修复(实际上是0而不是10),它运作良好,但只是中间测试(按相反顺序打印数字)。然后我保留了8字节的字段来累积循环计算,直到左边,然后中断4,1,..,..结束后,又让我有了一些有趣的CPU内容。

Raping Google没有给出任何结果。怎么了?提前谢谢。

UPD:如果你因为核心转储异常的严重问题而探索关于这个主题的SOF问题,不要在这个话题上浪费你的时间,在我的情况下,我脑子里发生了一个史诗般已经解决的问题,做了一些学校 - 错误。

1 个答案:

答案 0 :(得分:1)

mov al, [i]替换为movzx ax, [i] 您在ah中至少留下0x30,这会导致ax> = 0x3000,从而阻止ax和10之间的分割结果适合al

ah不为零,因为div bl设置它(它是除法的余数)但是在循环跳回后它永远不会被清除。
这也意味着第一次迭代是偶然的:ah最初为零,因为SYS_READ为3,因此在输入循环之前所需的mov eax, 3ah设置为零。

另外,您没有正确设置要打印的字符串的起始地址(它总是少一个字节)。

相关问题