macos - 0xbffff8a8 : aam $-0x8 error when saving the base pointer

标签 macos assembly x86

我目前正在 Uni 学习微电子学和汇编编程的入门类(class)。在每个函数的开头,我通过将调用者的基指针压入堆栈来保存它。给出以下函数,我得到一个错误:

        .globl my_func
        .globl _my_func

my_func:
_my_func:
    pushl   %ebp
    movl    %esp,%ebp

    movl    4(%esp),%ebx

    subl    $1,%ebx
    movl    %ebx,%eax
    ret

0xbffff8a8: aam $-0x8 <-EXC_BAD_ACCESS (code=2, address=0xbffff8a8)

我发现这是一个内存异常,我只是不明白为什么会抛出它。当我跳过函数中的前两条指令(基指针保存)时,函数运行良好。在你指出它之前——是的,我知道这个函数毫无意义而且很慢,我只是想了解指令是如何工作的,以及如何使用堆栈和寄存器。

我正在使用 LLVM5.1 在装有 OSX10.9 的 Intel Mac 上为 IA32 组装它

最佳答案

您需要在函数末尾重置堆栈指针,显式地或通过弹出寄存器以匹配您在函数开始时压入的内容,否则当您返回时它将指向无效地址:

popl %ebp    ; restore stack pointer to its original value
ret

关于macos - 0xbffff8a8 : aam $-0x8 error when saving the base pointer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22856609/

相关文章:

macos - 当我命令 "brew cask install virtualbox"或 "brew cask search google-chrome"时发生 Homebrew 错误

php - 如何修复 PHP 警告 : Unable to load dynamic library: php_intl. ddl

linux - Git remote 尝试以我当前的本地用户身份连接到我的服务器

c - 汇编寄存器算术?

c - 如何使用 SSE2 添加数组中的所有元素?

c++ - 访问连续 L1 高速缓存的代码比访问 3x 非连续 L1 内存的代码慢

ios - 使用 CocoaHTTPServer 向 "send"发送消息

assembly - 哪个 x86 寄存器表示 movsb 指令中的源位置?

assembly - 如何理解这段嵌入的汇编代码?

c - VC++ 2010 中的内联汇编错误