相关疑难解决方法(0)

从无法对齐RSP的函数调用时,glibc scanf分段错误

编译以下代码时:

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有什么错呢?

linux assembly x86-64 nasm calling-convention

2
推荐指数
1
解决办法
515
查看次数

标签 统计

assembly ×1

calling-convention ×1

linux ×1

nasm ×1

x86-64 ×1