简体   繁体   中英

For loop in assembly goes on forever

The code below, the for loop will not stop looping the string. it goes on forever. But should only go on for how long the user inputs a number.

    segment .data 
  integer1: times 4 db 0 
heeder: db "%d", 0 
strin: db "Enter the number: ", 0
 segment .bss 



SECTION .text 
 global _main 

 extern _scanf 
 extern _printf 

 _main: 

    push integer1 ; address of integer1 (second parameter)
   push heeder ; arguments are right to left (first parameter)
   call _scanf
add esp, 8
mov eax, 0
mov eax, [integer1]

mov  ecx, eax




 loop:
 cmp eax, ecx 
 jbe for
 jmp end
 for:
 push strin
 call _printf
add esp, 4
 inc eax
      jmp loop
 end:

Can you get what is wrong with the code?

After you call printf() the value in eax is the return value of the printf function - eax is not preserved by the call.

Note that even after you fix that problem, there's at least one other bug - eax and ecx start the loop with the same value - it looks like you want `eax1 to be set to 0, but you're doing that too soon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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