首页 > 解决方案 > 使用 scanf 的程序中的分段错误

问题描述

我的 VirtualBox 上的汇编程序有问题。
在 VB 上,我有 Linux Mint(64 位)。我试图编译这个汇编代码(在文件中my_file.asm):

extern printf  
extern scanf  

section .data  
format: db "%d",0  
napis: db "a/b=%d",10,0  

section .bss  
a: resd 1  
b: resd 1  

global main  
section .text  
main:  

mov rdi, format  
mov rsi, a  
xor rax, rax  
call scanf  

mov rdi, format  
mov rsi, b  
xor rax, rax  
call scanf  

mov eax, [a]  
xor edx, edx  
div dword[b]  

ret

首先我输入:nasm -f elf64 myfile.asm
然后我输入: 然后我gcc myfile.o -o myfile
有这个错误消息:
“/usr/bin/ld: plik.o: relocation R_X86_64_32S against .bss' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld:最终链接失败:输出
collect2 上的不可表示部分:错误:ld 返回 1 退出状态“

所以我添加-no-pie到 gcc 命令:gcc -o test test.o -no-pie

现在,当我使用./my_file我有这个错误:Segmentation fault (core dumped)而且我不知道该怎么做。

标签: linuxgccassemblyx86-64nasm

解决方案


推荐阅读