Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


About CMP and TEST

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine
View previous topic :: View next topic  
Author Message
Stregum
Advanced Cheater
Reputation: 0

Joined: 17 Jun 2014
Posts: 56
Location: We make baguettes there !

PostPosted: Mon Aug 24, 2015 6:50 am    Post subject: About CMP and TEST Reply with quote

Hi folks, I have a question about CMP and TEST (hence the title) Smile
This is asked all over the interwebz, but I don't get it well.
(I know you're pissed off, you're about to send me into the great world of google and stuff, I did my best, I promise :p)

Let's take this example

Code:

[ EAX = 0000002B ]

test al,0x10
jne RUNAWAY
jmp CONTINUE


If i'm correct: al = 0x2B
Where does the CPU jumps ? What is going on ?
Does it do something like

Code:

     0x2B
AND  0x10

     0010 1011
AND  0001 0000
-------------------
     0000 0000


And then turns ZF to 1 and goes to CONTINUE ?
I asked this question in stackoverflow, and I had this reply:

Quote:

An AND (test) will see if any of the one-bits match between the operands. A single bit in common is enough.

A SUB (cmp) will check if all bits match.


1. Fair enough, I understand that, but why in this case, there are no bits in common and the cpu still goes to CONTINUE ?

2. Is my ZF Hypothesis ok ?

[By the way, this is a real example that I've taken from a game, and I'm trying to understand it]

Am I hopeless ? Tell me :c

Anyways Smile Thanks, and by the way, I love this forum <3

_________________
Rhaa Stregum Vitae Smile
Back to top
View user's profile Send private message
justa_dude
Grandmaster Cheater
Reputation: 23

Joined: 29 Jun 2010
Posts: 891

PostPosted: Mon Aug 24, 2015 8:51 am    Post subject: Reply with quote

Test does a bitwise and. If the result of the and was zero, the ZF is set to one. Otherwise, it is set to zero. In your example the result of the and is zero, so the zf is set to one.

JNE jumps if the zf = 0 and doesn't jump if the zf = 1. In your example, the zf is set to one so there is no jump.

That's why the code jumps to continue.

The cases you're describing are pretty uncommon... about the only time you see test generated is for checking whether or not a register is zero... do not confuse test with compare.

_________________
A nagy kapu mellett, mindig van egy kis kapu.
----------------------
Come on...
Back to top
View user's profile Send private message
STN
I post too much
Reputation: 43

Joined: 09 Nov 2005
Posts: 2676

PostPosted: Mon Aug 24, 2015 8:53 am    Post subject: Reply with quote

Quote:
If i'm correct: al = 0x2B

Yes.

From the wikipedia page
https://en.wikipedia.org/wiki/TEST_(x86_instruction)

If the result of the AND is 0, the ZF is set to 1, otherwise set to 0.

Quote:
And then turns ZF to 1 and goes to CONTINUE ?


Correct

Quote:
1. Fair enough, I understand that, but why in this case, there are no bits in common and the cpu still goes to CONTINUE ?


Because of jne. JNE or JNZ is only executed if the ZF flag is not set (0), if it is 1 the jne(z) is not executed and skipped. If you want the jump to be taken, use jz instead.

Look up the truth table of AND logic gate (http://onegoodmove.org/fallacy/images/and.gif) or operation, its the same principle but applies to bits. The result is only true(1) if both bits are common (match) otherwise they are zero.

_________________
Cheat Requests/Tables- Fearless Cheat Engine
https://fearlessrevolution.com
Back to top
View user's profile Send private message
Stregum
Advanced Cheater
Reputation: 0

Joined: 17 Jun 2014
Posts: 56
Location: We make baguettes there !

PostPosted: Mon Aug 24, 2015 2:49 pm    Post subject: Reply with quote

I see, thanks to both of you Smile

I was confused by the state of the flag and the comparison, plus the 'jne' :

jump if the result is 0, so that means ZF = 1 (and not 0). That's where i got confused, and oddly, I knew that.. It comes with practise I guess ^^

That's clearer now, thanks a bunch.

(This wikipedia page is absolutely great, I didn't know there would be a page for each instruction, I've got a pdf of references, without examples and stuff, the one linked in the wiki seems better)

Neat stuff, keep it up guys Smile

_________________
Rhaa Stregum Vitae Smile
Back to top
View user's profile Send private message
BanCheese
Cheater
Reputation: 0

Joined: 22 Oct 2014
Posts: 49

PostPosted: Mon Aug 24, 2015 10:48 pm    Post subject: Reply with quote

I've never used the wikipedia one, but I very much like this one: http://x86.renejeschke.de/

It even provides C-like implementations, which makes things easy to understand.

_________________
A guy who likes memory hacking.
Back to top
View user's profile Send private message
Stregum
Advanced Cheater
Reputation: 0

Joined: 17 Jun 2014
Posts: 56
Location: We make baguettes there !

PostPosted: Tue Aug 25, 2015 7:22 am    Post subject: Reply with quote

Thanks for this one, seems easier, it's perfect for me to begin with Smile
_________________
Rhaa Stregum Vitae Smile
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites