Jurdun
Jurdun

Reputation: 145

Loop continuously looping indefinitely

I've been trying to create a loop to print a character an amount of times depending on the user's input, however, the loop does not stop and instead carries on indefinitely.

mov esi, [items]            //Esi points to the first item - Calling data from the C code and assigning it to the esi source indexer, in this case being the users inputted number. 



    loop1: mov eax, [esi]       // moves the first number which is in esi to the eax register
           push eax             // pushes it onto the stack so it can be used 
           call printInt        // Prints the integer in the eax register



           push ','             // Prints a comma after the number inputted
           call printChar

           push ' '             // Prints a space
           call printChar

           mov cx, 0            // assigning the value of 0 to counter
           loop2:
                   push '*'     // pushing the required character onto the stack
                   call printChar // printing the character
                   inc cx           // incrementing the counter by 1
                   cmp cx, [esi]    // comparing the program counter with the users inputted number
                   jne loop2        // jumping back to the beginning of the loop if cx is not equal to the users input thus printing the character the same number of times as the users inputted number

           call printNewLine

           mov eax, [esi]
           add esi, 4           // Now that's odd. Esi is pointing at an integer, and that's 4 bytes in size.
           cmp eax, 0

           jnz loop1



        jmp finish          // We need to jump past the subroutine(s) that follow
                                // else the CPU will just carry on going.

The program's input and output is controlled by C that is the reason for me tagging C with the post.

The part of the program that is not working as it should is beginning from loop2 and ending on jne loop2.

Thank you in advance for any help.

Upvotes: 1

Views: 91

Answers (1)

hidefromkgb
hidefromkgb

Reputation: 5903

The inner loop (the one that starts at loop2) is told to run 65k times if [esi] == 0, because at the first iteration cx is already greater than 0.

And yes, external functions may spoil it as well. One major need-to-know here is their calling convention. From the look of it (passing the first parameter through stack) your CX contents are pretty much doomed upon return: nearly all conventions that pass everything via stack assume that CX is caller-saved.

Upvotes: 2

Related Questions