 |
Cheat Engine The Official Site of 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 !
|
Posted: Mon Aug 24, 2015 6:50 am Post subject: About CMP and TEST |
|
|
Hi folks, I have a question about CMP and TEST (hence the title)
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 Thanks, and by the way, I love this forum <3
_________________
Rhaa Stregum Vitae  |
|
Back to top |
|
 |
justa_dude Grandmaster Cheater
Reputation: 23
Joined: 29 Jun 2010 Posts: 891
|
Posted: Mon Aug 24, 2015 8:51 am Post subject: |
|
|
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 |
|
 |
STN I post too much
Reputation: 43
Joined: 09 Nov 2005 Posts: 2676
|
Posted: Mon Aug 24, 2015 8:53 am Post subject: |
|
|
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.
_________________
|
|
Back to top |
|
 |
Stregum Advanced Cheater
Reputation: 0
Joined: 17 Jun 2014 Posts: 56 Location: We make baguettes there !
|
Posted: Mon Aug 24, 2015 2:49 pm Post subject: |
|
|
I see, thanks to both of you
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
_________________
Rhaa Stregum Vitae  |
|
Back to top |
|
 |
BanCheese Cheater
Reputation: 0
Joined: 22 Oct 2014 Posts: 49
|
Posted: Mon Aug 24, 2015 10:48 pm Post subject: |
|
|
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 |
|
 |
Stregum Advanced Cheater
Reputation: 0
Joined: 17 Jun 2014 Posts: 56 Location: We make baguettes there !
|
Posted: Tue Aug 25, 2015 7:22 am Post subject: |
|
|
Thanks for this one, seems easier, it's perfect for me to begin with
_________________
Rhaa Stregum Vitae  |
|
Back to top |
|
 |
|
|
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
|
|