【问题标题】:A simple assembly code cause a segment fault?一个简单的汇编代码导致段错误?
【发布时间】:2015-05-26 10:41:47
【问题描述】:
.section .data

.section .text
.globl _start
_start:
 movl $1, %eax  # this is the linux kernel command
    # number (system call) for exiting
    # a program

movl $4, %ebx   # this is the status number we will
    # return to the operating system.
    # Change this around and it will
    # return different things to
    # echo $?

int $0x80   # this wakes up the kernel to run
    # the exit command

但如果我删除最后一行代码 int 0x80 ,那么它会导致段错误

我不知道为什么?谁能告诉我。

感谢您的宝贵时间。


谢谢大家。现在我得到了答案。

没有int $0x80这行代码,系统不知道这个应用程序是否已经结束或者这个应用程序什么时候结束。所以会导致崩溃。

【问题讨论】:

  • 如果你没有正确退出你的程序回到操作系统(你用eax = 1离开int 0x80)你认为程序结束时会去哪里?
  • 谢谢,我知道为什么了。
  • 嘿,simowce,你会发布“为什么”吗?该答案将对任何未来遇到相同问题的开发人员有所帮助。

标签: linux assembly interrupt


【解决方案1】:

如果您删除int 0x80,您将遇到分段错误,因为它会在您的程序之后立即开始执行 RAM 中的任何随机字节。你真的无法预测那里会发生什么,其他事情肯定会发生,但很可能会出现段错误,因为随机数据很可能会成为进程内存之外的内存访问。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-16
    • 2023-03-12
    • 1970-01-01
    相关资源
    最近更新 更多