c - 打印两次 NASM

标签 c assembly printf nasm

我的问题:为什么当我调用 printf 一次时它会打印两次? 请注意,是的,我知道我正在堆中为存储在堆栈上的变量分配空间。我这样做只是为了习惯 NASM 中的 malloc、指针和“数组”。

在 x64 位机器中编译:
nasm -f elf32 -o TEMP.o 文件.asm
和:
gcc -m32 -o exec TEMP.o

extern exit, printf, malloc, free
global main

section .data
format  db  "%s", 10
msg:    db "Hello!!",10
BUF equ $-msg + 1

section .text
main:
    push BUF    ; How many bytes do we want to allocate
    call malloc ; ptr stored in EAX
    add esp, 4  ; clear the last thing on the stack (BUF)
    mov esi, eax    ; new source index at malloc pointer
    xor ecx, ecx    ; clear ECX (counter for us)
loop:
    mov dl, [msg+ecx]   ; mov letter into dl
    mov BYTE [esi+ecx], dl  ; cat dl onto array
    inc ecx         ; add 1 to our ounter
    cmp ecx, BUF-1d
    jl loop

    xor edx, edx
    mov BYTE [esi+ecx], dl
    add esp, 4
    mov esi, eax
    push esi    ;
    push format
    call printf
    add esp, 4*2
    push esi
    call free

    push 0
    call exit
    add esp, 4

最佳答案

您只需以 0 结束字符串

format  db  "%s", 10, 0
msg:    db "Hello!!",10 ,0
<小时/>

好吧,我刚才读了评论,看到你说你有代码是为了插入 0 ,我会检查一下,因为我复制/粘贴了你的代码,并且只在字符串末尾添加 0 使其输出一个字符串,我什至没有注意到插入代码,更不用说触摸它了,但我只能假设这就是您的问题所在。

关于c - 打印两次 NASM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21343875/

相关文章:

比较 C 中的两个二进制文件

c - 如何找到我的系统调用已添加到内核中?

assembly - 为什么 INVLPG 至少被调用一次

assembly - CALL 指令是否总是将 EIP 指向的地址压入堆栈?

GCC 的 asm 扩展版

c - 在 char 中显示一个 int,并进行一些修改

php - 使用 OpenSSL/C++ 和 PHP/Mcrypt : the 1st block is decrypted only 的 AES-128-CBC 加密

arm - 尝试 printf 数字 >= 10 时出现 STM32 硬故障

在四边形中将十六进制转换为二进制

c - 搜索创建通用 makefile 以编译多个程序的解决方案