简体   繁体   English

装配中的循环不起作用

[英]Loop in assembly doesn't work

I had some problems when I tried to create a loop in asm. 当我尝试在asm中创建循环时遇到了一些问题。 So I created another code with just the loop. 所以我用循环创建了另一个代码。 The problem is, when I decrement or increment the ecx , the variable gets messed. 问题是,当我减少或增加ecx ,变量会变得混乱。 If I use the loop instruction without the dec it doesn't work either. 如果我使用没有dec的循环指令它也不起作用。 How do I use the ecx to loop? 我如何使用ecx循环?

Code

section .text
     global main

     extern printf
section .data
FORMAT: db "L", 10, 0 ; just to print the L 10 times
main:

     mov ecx, 10 ; start the counter in 10
     jmp runloop ; i imagine i dont need it
runloop:
     push FORMAT
     call printf
     add esp, 4
     dec ecx
     cmp ecx, 0
     jne runloop

ecx value is not guaranteed to be preserved across the printf call. 不保证在printf调用中保留ecx值。 Use one of the following registers instead: ebx , ebp , esi , edi . 请改用以下寄存器之一: ebxebpesiedi You should preserve them too by push ing the register of choise onto the stack and restoring it afterwards. 您应该通过push选择寄存器push堆栈并在之后恢复它来保留它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM