为什么cmp 2,2不等于0

use*_*691 0 linux assembly nasm

extern putchar
extern exit
section .data

section .text
global main
main:

push 's'

mov eax, 2

cmp eax, 2

point:
call putchar
jz point

push 0
call exit
Run Code Online (Sandbox Code Playgroud)

在控制台上,我只看到了一个人的角色.

编译并运行:

nasm -f elf ./prog.asm
gcc -m32 -o prog ./prog.o
./prog
Run Code Online (Sandbox Code Playgroud)

Igo*_*sky 5

cmp "等于0"(即,它设置ZF标志).但是,call putchar下一行中的标记会丢弃设置的标志cmp,因此您的jz工作无效(或多或少意外).如果你想保存标志以便以后比较,你可以使用pushfpopf,但是这在你的情况下不会真正起作用,因为它putchar会期望堆栈上的字符,而不是标志.

现在,回答你没有陈述的实际问题.我假设你要打印's'两次.这是如何正确地做到这一点:

  mov eax, 2 ; init counter

print_loop:
  push eax; save the counter since it will be trashed by putchar
  push 's'
  call putchar
  add esp, 4 ; restore the stack pointer since putchar is cdecl
  pop eax ; restore the saved counter
  dec eax ; decrement it
  jnz print_loop ; if it's not yet zero, do another loop
Run Code Online (Sandbox Code Playgroud)

add esp, 4可以用另一个替换pop eax为稍短的代码.