简体   繁体   English

为什么在移动堆栈指针时出现分段错误?

[英]Why am I getting a segmentation fault when moving the stack pointer?

Working with assembly code and wondering why I get a seg fault with the instruction subl $8, %esp? 使用汇编代码,想知道为什么我的subl $ 8,%esp指令出现段错误?

        pushl %ebp
    movl %esp, %ebp

        movl 16(%ebp), %esi
        movl 12(%ebp), %edi
        movl 8(%ebp), %eax
        movl $0, %ebx
        subl $8, %esp
        jmp .LL1

.LL1:
        cmpl %ebx, %esi
        je .LL2
        movl %ebx, 4(%esp)
        movl %eax, (%esp)
        addl $1, %ebx
        jmp .LL1

.LL2:
    popl %ebp
    ret

segfault on subl $8, %esp 在subl $ 8上出现segfault,%esp

Are you sure that the code segfaults on subl $8, %esp ? 您确定subl $8, %esp上的代码存在段subl $8, %esp吗?

In your code, you subtract 8 from the stack pointer, but never restore the value. 在您的代码中,您从堆栈指针中减去了8,但从未恢复该值。 The address to return to is no longer the most recent thing on the stack when you execute ret . 执行ret时,返回的地址不再是堆栈中的最新内容。

The instruction 指令

movl %esp, %ebp

copies the original value of %esp to %ebp . %esp的原始值复制到%ebp When you return, you restore the original of %ebp with popl , but never restore %esp , so you return to some garbage address. 返回时,将使用popl还原%ebp的原始内容,但从不还原%esp ,因此将返回到一些垃圾地址。

Before the line 上线前

popl %ebp

add

movl %ebp, %esp

to correct the stack pointer before returning. 在返回之前更正堆栈指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM