编译以下代码时:
global main
extern printf, scanf
section .data
msg: db "Enter a number: ",10,0
format:db "%d",0
section .bss
number resb 4
section .text
main:
mov rdi, msg
mov al, 0
call printf
mov rsi, number
mov rdi, format
mov al, 0
call scanf
mov rdi,format
mov rsi,[number]
inc rsi
mov rax,0
call printf
ret
Run Code Online (Sandbox Code Playgroud)
使用:
nasm -f elf64 example.asm -o example.o
gcc -no-pie -m64 example.o -o example
Run Code Online (Sandbox Code Playgroud)
然后运行
./example
Run Code Online (Sandbox Code Playgroud)
它运行,打印:输入数字: 但随后崩溃并打印: 分段错误(核心已转储)
因此,printf可以正常工作,而scanf则不能。我对scanf有什么错呢?