小编Sus*_*wal的帖子

弹出x86堆栈时出现分段错误

我正在尝试将x86程序集和C链接。

我的C程序:

extern int plus_10(int);

# include <stdio.h>

int main() {
    int x = plus_10(40);
    printf("%d\n", x);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我的汇编程序:

[bits 32]

section .text

global plus_10
plus_10:
    pop edx
    mov eax, 10
    add eax, edx
    ret
Run Code Online (Sandbox Code Playgroud)

我编译并链接两个如下:

gcc -c prog.c -o prog_c.o -m32
nasm -f elf32 prog.asm -o prog_asm.o
gcc prog_c.o prog_asm.o -m32
Run Code Online (Sandbox Code Playgroud)

但是,当我运行结果文件时,出现了分段错误。

但是当我更换

流行版

mov edx,[esp + 4]

该程序工作正常。有人可以解释为什么会这样吗?

c x86 assembly gcc nasm

36
推荐指数
1
解决办法
1258
查看次数

标签 统计

assembly ×1

c ×1

gcc ×1

nasm ×1

x86 ×1