问题
It seems that I can't call sprintf()
correctly in assembly.
When I try to dprintf()
my buffer that should now be formatted, all I get is:
(null)
and a segmentation fault. When running lldb
with my program, strlen()
is the reason of the fail as it can't find a \0
in my buffer.
Here's my code:
mov rdi, buff
mov rsi, 0
mov rdx, 17
call memset
lea rsi, [rel n_head]
mov rdx, rax
call sprintf
mov rdx, rdi
lea rsi, [rel fmt]
mov rdi, 1
call dprintf
...
section .data
n_head: db "Low battery: %d%%", 0
fmt: db "%s", 10, 0
section .bss
buff: resb 17
What did I do wrong?
I assemble with nasm -f elf64
and clang
with nasm 2.14.02 and llvm 10.0.0 on FreeBSD 12.1-RELEASE amd64.
来源:https://stackoverflow.com/questions/61293711/calling-sprintf-in-x64-assembly