flat assembler
Message board for the users of flat assembler.

Index > Main > test instruction problem..

Author
Thread Post new topic Reply to topic
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 28 Jun 2011, 13:02
Hi everyone, I'm bit stuck here.. I got code like this:
Code:
@@:
     mov ecx,-1
     xor eax,eax
     cld
     repne scasb
     test ebx,ebx
     dec ebx
     jnz @b    

Here, EBX = 2 but.. This routine loops only 1 times.. why ? Sad after first one, it changes Z flag.. but I need to do it twice!
Here's result..
Code:
test 2,2 ;Z flag becomes 0, OK JNZ works fine.
test 1,1 ;Z flag becomes 0, OK JNZ should work fine but after DEC EBX Z flag is set to 1.. WHY ?    
Post 28 Jun 2011, 13:02
View user's profile Send private message Reply with quote
r22



Joined: 27 Dec 2004
Posts: 805
r22 28 Jun 2011, 13:15
Code:
DEC ebx ; changes Z flag
    

Try using JNS instead of JNZ
*edit* also you really don't need the TEST instruction at all...


Last edited by r22 on 28 Jun 2011, 13:15; edited 1 time in total
Post 28 Jun 2011, 13:15
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20309
Location: In your JS exploiting you and your system
revolution 28 Jun 2011, 13:15
DEC also alters the Z flag. Indeed in your code TEST is superfluous.
Post 28 Jun 2011, 13:15
View user's profile Send private message Visit poster's website Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 28 Jun 2011, 13:16
But why ? First time it does not, second time it does.
I'll try JNS..
Post 28 Jun 2011, 13:16
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20309
Location: In your JS exploiting you and your system
revolution 28 Jun 2011, 13:17
JNS will give you two loops, and thus the code will execute three times. Is that what you want?
Post 28 Jun 2011, 13:17
View user's profile Send private message Visit poster's website Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 28 Jun 2011, 13:21
Oh yes, I need 3 times with value of 2.. But I don't understand why DEC EBX modifies Z flag.. First time it does not and second time it does.. Can someone explain me please ? Smile Thanks.
Post 28 Jun 2011, 13:21
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20309
Location: In your JS exploiting you and your system
revolution 28 Jun 2011, 13:24
DEC always changes the Z flag according to the result. The first time through the DEC Z flag result is the same as the TEST result so you don't see the change.

BTW: It is explained in the manuals.
Post 28 Jun 2011, 13:24
View user's profile Send private message Visit poster's website Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 28 Jun 2011, 13:27
ahh.. I got it, thank you! Smile
Post 28 Jun 2011, 13:27
View user's profile Send private message 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.