linux - 不执行任何操作的简单 _start 末尾的段错误

标签 linux assembly segmentation-fault x86-64 nasm

当我汇编以下汇编代码时,出现错误 Segmentation fault (core dumped)

section .text
global _start

_start:
    mov eax, 8

我的Makefile如下

all:
    nasm -f elf64 -o asm.o asm.s
    ld asm.o -o asm
    rm asm.o

我不知道是什么问题。

我正在运行 64 位 Ubuntu。

最佳答案

CPU 执行程序,找到 mov eax, 8 指令,执行它……然后呢?目标文件中没有更多指令,但没有人告诉 CPU!它会执行接下来的任何操作,可能没有有效指令,这会导致段错误,就像@MichaelPetch 所说的那样。

IMO 最简单的解决方案是使用包装器,它负责初始化和清理您的程序,例如 GCC。只需将 mov eax, 8 放入 main 函数中,您可能熟悉 C 语言。

修改源文件如下:

section .text
global main

main:
    mov eax, 8
    ret

(main 是一个函数,所以你需要 ret 指令从它返回。)

并使用以下脚本:

nasm -f elf64 -o asm.o asm.s
gcc asm.o -o asm
rm asm.o

关于linux - 不执行任何操作的简单 _start 末尾的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39559371/

相关文章:

c - 为什么子进程 "read"函数不返回(我觉得会返回-1),子进程 'printf'函数不起作用

assembly - 如何在 nuget 错误中找到 "lib"文件夹?

assembly - x86 "ret"指令相当于什么?

c++ - 使用 new 分配派生类数组时出现问题

linux - nslookup 报告 "can' t 解析 '(null)' : Name does not resolve"though it successfully resolved the DNS names

linux - Grep 运行超过 x 分钟的进程计数

c++ - 无法进入while循环:"Segmentation fault"

c++ - 段错误是未定义行为的结果吗?

linux - 使用 bash 脚本获取文件名

c - Aarch64 程序集中的系统调用调用