ret 导致程序集中出现段错误 [英] ret is causing a segfault in assembly

查看:40
本文介绍了ret 导致程序集中出现段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下汇编代码:

.text
.global main
main:
    push %r13
    push %r14
    push %r15
    jmp rest
x:    
    .data
    y0:    .quad   0
    .text
    mov %r14, y0
    mov $format,%rdi
    push y0
    popq %rsi
    movq %rsi, %r15
    call printf
    mov $format,%rdi
    mov $10,%rsi
    call printf
    ret
rest:    
    movq $5, %r9
    pushq %r9
    popq %rsi
    movq %rsi, %r14
    jmp x
    mov $0,%rax
    pop %r15
    pop %r14
    pop %r13
    ret
.data
format:
.byte 37
.byte 108
.byte 117
.byte 0

我希望 ret 能让程序跳回到原来的位置:但是,我遇到了分段错误.这是为什么?

I was hoping that the ret would make the program jump back to where it was: however, I get a segmentation fault. Why is this?

推荐答案

这些问题几乎总是由未能清理堆栈引起.您的情况也是如此.

These issues are almost always caused by failing to clean up the stack. The same is true in your case.

你开始于:

main:
    push %r13
    push %r14
    push %r15
    jmp rest

rest你然后

jmp x

x 中,您可以这样做:

Within x you do this:

mov $format,%rdi
mov $10,%rsi
call printf
ret

这给你留下了三件你没有处理过的东西.当这个 ret 被执行时,它会将 %r15 的前一个值移动到指令指针和段错误中.

This leaves you with three things sitting on the stack that you have not dealt with. When this ret is executed, it moves the previous value of %r15 into the instruction pointer and segfaults.

ret 不会从跳转中返回.它返回到 call 之后的指令.

ret will not return from a jump. It returns to the instruction following a call.

这篇关于ret 导致程序集中出现段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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