Hi
I have a litte question to this code
In eax = 15
mov ecx, eax
movzx eax, cx
and eax, 3
test eax, eax
jnz out
This test is eax 3 why must i not test is eax not zero ?
and eax,3
test eax,eax
actually, there are a couple unnecessary instructions, there....
mov ecx, eax
and eax, 3
jnz out1
it tests if either bit 0 or bit 1 are nonzero
if either bit is set (or both), the branch is made
at the same time, EAX is modified the same way - only bits 0 and 1 are allowed
notice - "out" is the name of an instruction - illegal as a label :P
You dont need the and, test does an and without modifying the registers
we don't know that without seeing the rest of the code - it may rely on EAX being less than 4
I think the proper question to ask the O.P. at this point is "what are you trying to do?". Then we can come up with the appropriate solution.
Regarding
TEST EAX, EAX
JNZ somewhere
I prefer to use
OR EAX, EAX
JNZ somewhere
because it makes more sense to me, but it's really a matter of style.
Also, it seems you're zeroing out the top 16 bits of EAX by that MOVZX instruction, but again, that's probably not needed: just test what bits you need in EAX (or just text AX instead of EAX if your flags fit into 16 bits--or use AL if they fit into 8 bits).
=============================================
OK, making some assumptions here:
- Coming into this code, you have a value in EAX that you want to put into ECX
- You want to test that value to see if either of the low 2 bits are set
- If either of the low 2 bits are set, you want to jump to a label ("out1" per Dave's suggestion)
If this is so, then this is all you need:
MOV ECX, EAX ;Get value into ECX
TEST AL, 3 ;See if either of low 2 bits set
JNZ out1 ; if so, go to out1
Its good to note that except when testing a register against itself, TEST and OR are not interchangeable, TEST performs a logical AND operation without affecting the values of the operands, OR changes the operand. For example, given that TRUE = !FALSE
EAX=3
TEST EAX,1 > Result is TRUE (Z flag is not set, EAX = 3)
TEST EAX,2 > Result is TRUE (Z flag is not set, EAX = 3)
TEST EAX,4 > Result is FALSE (Z flag is set, EAX = 3)
OR EAX,1 > Result is TRUE (Z flag is not set, EAX = 3)
OR EAX,2 > Result is TRUE (Z flag is not set, EAX = 3)
OR EAX,4 > Result is TRUE (Z flag is not set, EAX = 7)
I should clarify: TEST and OR are functionally identical in the case where a register is being TESTed/ORed against itself. In this case, the contents of the register aren't changed. Otherwise, it's good to keep in mind what you just pointed out.
Whoops; I see you already covered that in your reply. Never mind ...
Now have i understand this
;eax =15
mov ecx, eax
movzx eax, cx
and eax, 3 Set the bitflag From (Z 1 DS 0023 32bit 0(FFFFFFFF)) to (Z 0 DS 0023 32bit 0(FFFFFFFF))
test eax, eax
jnz @out jmp to @Out is eax not 0
This test only is "eax =15" not 0
Is this correct?
If correct why this long way?
but i can use
Cmp eax,0
jnz @out
delete
delete
Tell us what you're trying to do, not how you think you should do it.
Yes, that is far too complicated. There's a much simpler way. But please just explain simply what you're trying to accomplish.
This is only a question
I playing a little with command register an try to understand it
I trying not code any project it only for learn for Tips and Tricks :bg
The "and eax,3" isolates the low two bits. This is useful if e.g. the original eax has some sort of packed bits, looks like
that code only needs the low two bits and ignores the others.
Try and use binary, this really helps when using "and".
eax=00001111b
and 00000011b
eax=00000011b
Only 1 and 1 equals 1, all other combinations equal 0
well - the tip is - there are some unnecessary instructions, there - lol - see my first post
if the input value is 15, then EAX = 3 after it is AND'ed
so, it branches
it does test for 0, in reverse
that is...
if the lower 2 bits of the input value are 0, then it does not branch
if either of the lower 2 bits are 1, then it branches
the logic instructions AND, OR, XOR, NOT, TEST are very basic
you should try to understand how they work, as well as the math instructions ADD, SUB, ADC, SBB, NEG
these instructions are at the very core of programming
you should try to understand how they work, as well as the math instructions ADD, SUB, ADC, SBB, NEG
these instructions are at the very core of programming
Dave?!?
Nobody is perfect but i have this not see in this combination for test is zero. :bg
sorry ragdog - i did not mean to offend you - just trying to help you understand
ok - let's use a truth table for the lower 2 bits
all the other bits are ignored
i.e. 15 will yield the same result as 3, 12 will yield the same result as 0
input result
----- ------
00 does not branch
01 does branch
10 does branch
11 does branch
Well, I'm still confused, not least of all by your poor English. (Please don't be offended: I'm just being honest, and I'm sure that my skills in whatever your native language is are even worse!).
It seems there are three possible things you're doing here:
- Checking for EAX being equal to 15
- Checking for EAX being zero (or not)
- Checking the lower 2 bits for not both being zero
These are all covered by the following code snippets:
; Test for EAX being 15:
CMP EAX, 15
JE somewhere
; Tests for EAX being zero:
OR EAX, EAX
JZ somewhere
CMP EAX, 0
JE somewhere
; Tests for 2 lower bits of EAX not both being zero:
TEST AL, 3 ;We only need to look at
JNZ somewhere ; the lowest 8 bits
TEST AL, 00000011B ;Maybe easier to understand
JNZ somewhere ; using binary
Which is why I keep asking you what you're trying to do. Not what your program in general is trying to do, but what specific test you're trying to do here. It's still not clear at all, which makes it difficult to make suggestions that will work for you. So maybe you can clue us in?