News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

asm And

Started by ragdog, December 21, 2011, 10:03:29 PM

Previous topic - Next topic

ragdog

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

dedndave

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

Gunner

You dont need the and, test does an and without modifying the registers
~Rob (Gunner)
- IE Zone Editor
- Gunners File Type Editor
http://www.gunnerinc.com

dedndave

we don't know that without seeing the rest of the code - it may rely on EAX being less than 4

NoCforMe

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


donkey

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)
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

NoCforMe

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 ...

ragdog

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




bomz


bomz


NoCforMe

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.

ragdog

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

sinsi

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

Light travels faster than sound, that's why some people seem bright until you hear them.

dedndave

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

ragdog

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