flat assembler
Message board for the users of flat assembler.

Index > Windows > Loop - problem :D

Author
Thread Post new topic Reply to topic
dieboy



Joined: 30 Dec 2004
Posts: 41
Location: Poland, Elblag
dieboy 15 Jul 2005, 17:02
Hi!
I'm gonna create a loop.
Code:
mov cx, 10
abc:
            invoke MessageBox, 0, 0, 0, 0
            loop abc    

This code works if I compiled it under dos, but when under windows - i have a loop, that never end! What's going on?

_________________
...
Post 15 Jul 2005, 17:02
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
r22



Joined: 27 Dec 2004
Posts: 805
r22 15 Jul 2005, 17:17
TWO POSSIBLE ISSUES
1)The API function your calling changes the value of ECX.
2)You are only setting CX to 10 the upper half of ECX could still have a value in it (in which case the loop would seem to never end).

FIX
Code:
mov ecx,10
abc:
push ecx
invoke MessageBox, 0, 0, 0, 0
pop ecx
loop abc
    

OR
replace ecx with a register that isn't changed by API function like ebp, ebx, esi, edi.
Post 15 Jul 2005, 17:17
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
T&K(r)



Joined: 10 Jul 2005
Posts: 18
Location: Poland - > Krakow
T&K(r) 15 Jul 2005, 17:24
I Tested this and i have the same problem even if i changeing registers (toECX) or pushing and poping.

The best will be if you write something like that :

Code:
mov cx, 10
abc:
            invoke MessageBox, 0, 0, 0, 0
            dec cx
            jcxz abc_end
            jmp abc
abc_end:


    


This is working - i checked it.

_________________
my GG ( Polish Instant Messager ) number is 8734187

Gdy widzisz kolejke - wstąp do niej - co ci szkodzi Smile
Post 15 Jul 2005, 17:24
View user's profile Send private message Reply with quote
dieboy



Joined: 30 Dec 2004
Posts: 41
Location: Poland, Elblag
dieboy 15 Jul 2005, 17:52
r22 wrote:

Code:
mov ecx,10
abc:
push ecx
invoke MessageBox, 0, 0, 0, 0
pop ecx
loop abc
    
I tested it, too.. And, unfortunetly, i have to remove loop from my code. Thanks for help.

_________________
...
Post 15 Jul 2005, 17:52
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
r22



Joined: 27 Dec 2004
Posts: 805
r22 15 Jul 2005, 22:16
--------------
mov ecx,10
.abc:
push ecx
push 0
push 0
push 0
push 0
call [MessageBox]
pop ecx
loop .abc ;dec ECX; JNZ .abc
-------------------
Works on my box (winxp sp2).
Maybe because your label isn't a local one (shouldnt affect it but who knows).

All the LOOP opcode does is
DEC ecx
JNZ labal
In most cases the above runs faster.

When you call an API like MessageBox, it doesn't save the states of EAX, ECX or EDX so you have to do it manually or use a register that IS preserved over API calls like EBX,EBP,ESI,orEDI.
Post 15 Jul 2005, 22:16
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
Reverend



Joined: 24 Aug 2004
Posts: 408
Location: Poland
Reverend 15 Jul 2005, 23:18
T&K(r) wrote:
The best will be if you write something like that :
Code:
mov cx, 10
abc:
            invoke MessageBox, 0, 0, 0, 0
            dec cx
            jcxz abc_end
            jmp abc
abc_end:
    

This is working - i checked it.

Why do you use 'jcxz' opcode? After 'dec ecx' if ecx was 1 a ZF flag is signed (as ecx will have value 0), so you can use just 'jz'. And in this case even, you can omit defining another label 'abc_end', because you can use 'jnz' and jump back. Even though the loop is bad, because MessageBox changes ecx, and you do not preserve it anyway. I'll suggest this:
Code:
mov ecx, 10
abc:
            push ecx
            invoke MessageBox, 0, 0, 0, 0
            pop ecx
            dec ecx
            jnz abc
    


P.S: 'dec ecx\ jz XXXXXX' does the same as 'loop XXXXXX'. Loop is smaller in bytes but also slower
Post 15 Jul 2005, 23:18
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.