Calling sprintf in x64 assembly

老子叫甜甜 提交于 2021-01-29 19:18:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!