开发者

Can't break out of a simple Assembly loop

I've been asked to create a simple loop in assembly language but I am having trouble as the loop doesn't end when it should, it continues in an infinite loop.

I need to give the ECX a variable which is taken by input, but in my code below even when I specify the counter directly is still falls into an infinite loop.

My code is here (UPDATED):

PasteBin Code

The code may contain errors other than the actual loop.

//Edited for explanation of the programs requirements...

The program needs to take开发者_运维知识库 an input "n" which will be used as the counter for the loop. I then take input "n" more numbers, when a number is positive I need to add that to the variable postot, when negative I need to add that to the variable negatot. After "n" numbers have been entered I print the results for each variable and the program exits.

Edit 2:

Fixed the loop problem by clearing the Stack correctly so I added:

        add esp,8

and:

next:   push ecx
        ...
pop ecx
loop next

Which fixed the loop problem.


Ok, I got it. . . You read int the number from the user. Then on line 41 you either jump to line 47 or fall through to line 43.

If you go to line 43, you then reach line 45 which jumps to line 49(end). If instead, you went to line 47, you will fall through to line 49(end).

In both cases, you then loop back to line 29(next).

You expect your ecx register to decrement every time loop is reached, and for it to remain unchanged during the processing of the loop.

Have you tried pushing ecx at around line 29, and popping it right before you loop?


I see two errors:

  • There's a JG positive in there AFTER you increment a stack pointer. Are you sure you want to check the result of fiddling with your stack frame, rather than the value of the number you entered?

  • jmp end doesn't: Right after end comes a loop next. I don't see any condition for terminating the loop.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜