Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Assembly (http://www.programmingforums.org/forum20.html)
-   -   [x86]Instruction test setting the zero flag (http://www.programmingforums.org/showthread.php?t=13750)

Klarre Aug 11th, 2007 6:34 AM

[x86]Instruction test setting the zero flag
 
Was currently stepping through an application and found a piece of code that I could not understand.

My question is:
How can a TEST instruction set the ZERO flag when testing a byte with the same byte?

:

...
TEST, CL, CL
JE SHORT Application.004013B4 ; When will this row be executed?
...


Thanks for your help!

/Klarre

Klarre Aug 11th, 2007 6:44 AM

When CL = 0 of course...

DaWei Aug 11th, 2007 9:24 AM

True. The TEST instruction ANDs the two operands and sets the flags accordingly. If the two operands are the same thing, the result will be zero only if the operand(s) are zero. It is really a dumb thing to do, in this case. Comparing CL to zero and jumping accordingly would be much clearer. But coders often do dumb things. That's why they wear dirty teeshirts and smell bad.

lectricpharaoh Aug 11th, 2007 8:28 PM

Quote:

Originally Posted by DaWei
True. The TEST instruction ANDs the two operands and sets the flags accordingly. If the two operands are the same thing, the result will be zero only if the operand(s) are zero. It is really a dumb thing to do, in this case. Comparing CL to zero and jumping accordingly would be much clearer. But coders often do dumb things. That's why they wear dirty teeshirts and smell bad.

It might be like how, in the bad old days (which I'm sure you remember ;)), some assembly coders used XOR (E)AX, (E)AX rather than MOV (E)AX, 0 because it was one byte shorter. I don't have my asm resources handy, but I'd expect TEST reg, reg is a few bytes shorter than TEST reg, imm32, unless it stores the immediate value as an 8-bit quantity, and sign-extends it.

I agree though, the clarity leaves something to be desired. However, if this was compiler-generated assembly (as seems likely from the context), the programmer has no business expecting it to be clear if optimizations are enabled.

Irwin Mar 9th, 2008 7:29 AM

Re: [x86]Instruction test setting the zero flag
 
Arguing about the readability of things such as 'XOR REG,REG'/'TEST REG,REG' is stupid. Someone should not be complaining about how they can understand somebody elses source when it's such a simple matter.

Furthermore, such instructions are not only shorter, they are a few clocks faster.

The Dark Mar 9th, 2008 4:17 PM

Re: [x86]Instruction test setting the zero flag
 
Dredging up a six month old thread to call people stupid, and having your first two posts in a forum call people stupid, is a sure way to make a name for yourself. Unfortunately, that name isn't a very nice one.

Irwin Mar 10th, 2008 2:33 AM

Re: [x86]Instruction test setting the zero flag
 
Quote:

Originally Posted by The Dark (Post 142261)
Dredging up a six month old thread to call people stupid, and having your first two posts in a forum call people stupid, is a sure way to make a name for yourself. Unfortunately, that name isn't a very nice one.

Fortunately for me, the first page goes back further than 6 months :) Albeit the fact that the main benefactors of the thread probably won't see it, others who are looking into the subject probably will. So I'm not too concerned about making a name for myself, good or bad.

For you to even bring up the argument of repertoire in a forum which is essentially made for helping others is laughable, so please; consider this my first and only response to you.

The Dark Mar 10th, 2008 3:09 AM

Re: [x86]Instruction test setting the zero flag
 
I wasn't having a go at you because you haven't posted much, if that is what you mean by "argument of repertoire". Extra helpers here are always welcome. Calling people who are asking questions and people who are answering them "stupid" is not helping, in fact it is a great hindrance to a forum such as this as it is likely to turn people away.


All times are GMT -5. The time now is 12:39 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC