"Segmentation fault" 尝试打印整数时

"Segmentation fault" when trying to print an integer

我一直在尝试编写一个打印数字的汇编程序。 我编写了打印单个字符的过程 printc

printc: push    ebp
        mov     ebp, esp

        mov     eax, 4
        mov     ebx, 1

        add     [ebp + 8], byte '0' 
        lea     ecx, [ebp + 8]

        mov     edx, 1

        int     0x80

        mov     esp, ebp
        pop     ebp

        ret

然后我试着写printi 所以:

printi: push    ebp
        mov     ebp, esp

        mov     eax, [ebp + 8]

        cmp     eax, 0
        je      end

        mov     ebx, 10
        div     ebx

        push    eax
        call    printi

        push    edx
        call    printc

end:    mov     esp, ebp
        pop     ebp

        ret

最后,我打电话给printi:

_start: push    32 
        call    printi

得到Segmentation fault (core dumped)。 知道为什么吗?

正如 Peter Cordes 所说的那样,如果您的程序没有因 printi 中的无限递归而更早崩溃,您的程序将 _start 和 return 变成未定义的代码。 =20=]

存在无限递归的原因:您可能认为 div ebxEAX 除以 10。它不是:它将 EDX:EAX 64 位值除以 EBX,并且您无法控制 EDX.

DIV 之前将 EDX 置零允许程序打印一些东西(但不是所需的输出),然后在它掉落时崩溃 printi