Assembly Printf-nasm故障

Assembly Printf-nasm故障,assembly,nasm,Assembly,Nasm,我在一个汇编类中,我需要为我正在做的一个项目多次打印一个白色字符“”。我已经在这里坐了几个小时,试图使printW函数工作,所以它将被称为X次。以下代码将打印出2''s,将初始cx更改为任何数字,但不会更改代码打印的“*”s的数量。我束手无策。有人能找出代码的问题,并解释为什么它是一个问题吗 printBoard: mov ecx,0x00010 cmp ecx,0 loop printWhiteRow ret printWhiteRow: call p

我在一个汇编类中,我需要为我正在做的一个项目多次打印一个白色字符“”。我已经在这里坐了几个小时,试图使printW函数工作,所以它将被称为X次。以下代码将打印出2''s,将初始cx更改为任何数字,但不会更改代码打印的“*”s的数量。我束手无策。有人能找出代码的问题,并解释为什么它是一个问题吗

printBoard:
    mov ecx,0x00010
    cmp ecx,0
    loop printWhiteRow
    ret

printWhiteRow:
    call printW


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;print a string
printW:
    push ebp                ; set up stack frame
    mov ebp,esp
    mov eax, whiteChar              ; put a from store into register
    push eax                ; value of variable a
    call printf             ; Call C function
    add esp, 8              ; pop stack 3 push times 4 bytes
    mov esp, ebp            ; takedown stack frame
    pop ebp                 ; same as "leave" op
    ret                     ; return
一些问题:

  • 您似乎不知道
    循环
    指令是如何工作的
  • 不知道这意味着什么:
    addesp,8;弹出堆栈3次推送4个字节
    。3乘以4等于12,并且在任何情况下,只推1个参数
  • cdecl调用约定将
    ecx
    定义为调用方已保存,因此如果希望保留其值,则需要保存它
  • 如果要打印单个字符,为什么不使用
    putchar
  • 这样的方法应该更有效:

    printBoard:
        mov ecx,0x00010
    printWhiteRow:
        push ecx     ; save counter
        push ' '     ; char to print
        call putchar
        pop ecx      ; cleanup argument
        pop ecx      ; restore counter
        loop printWhiteRow
        ret
    
    与printf相同:

    printBoard:
        mov ecx,0x00010
    printWhiteRow:
        push ecx       ; save counter
        push whiteChar ; string to print
        call printf
        pop ecx        ; cleanup argument
        pop ecx        ; restore counter
        loop printWhiteRow
        ret
    

    在调用
    printf()
    push ecx之前,您不需要保存
    ecx
    寄存器吗;在堆栈调用printf上存储ecx;pop-ecx;恢复ecx
    为什么不直接执行
    循环打印W
    ?注意,当以这种方式使用
    loop
    时,您不需要检查
    ecx
    是否为0。另外,不应该
    add esp,8
    be
    add esp,4
    ,因为您只将一个参数推到堆栈上?我的教授希望我们特别使用printf函数,所有符号都应该在同一行上…
    putchar
    将它们放在同一行上;)无论如何,
    printf
    是类似的。