Re: test eax, eax - why?
- From: Jack Klein <jackklein@xxxxxxxxxxx>
- Date: Thu, 24 Aug 2006 13:24:19 -0500
On 24 Aug 2006 07:41:06 -0700, spamtrap@xxxxxxxxxx wrote in
comp.lang.asm.x86:
Hi everyone,
I'm trying to learn x86 assembly language, and in addition to reading
the necessary books, trying the programmes etc., I'm also compiling my
own example C code and comparing it with the resultant (Visual C++)
code.
One thing I tried was compiling the "infinite" while(1){ ... } loop. I
got something like this:
mov eax, 1
test eax, eax
je label2
this seems somewhat odd. I know that "test" does a bitwise and of eax
and eax, but in this case, the rest of the
instruction (je) is not carried out. Why bother with this at all? Or
have I got the wrong end of the stick?
The C (or C++) code that you wrote contained the expression
"while(1)". Like all conditional expressions in C (and C++), this is
considered to be false if it evaluates to 0, and true if it evaluates
to any non-zero value.
You instructed the compiler to evaluate whether or not 1 is zero or
non-zero, and in the non-optimizing compiler it generated code to do
so. So you got exactly what you programmed.
Presumably the optimizing compiler would eliminate the test, but of
course VC++ makes it extremely difficult to see the assembly output of
optimized code.
Most likely you will find that if you replace this the defined C
idiom:
for ( ; ; )
....even the non-optimizing compiler will probably not include extra
code.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
.
- References:
- test eax, eax - why?
- From: spamtrap
- test eax, eax - why?
- Prev by Date: enabling x87 interrupts
- Next by Date: Suggestion for some good ASM books?
- Previous by thread: test eax, eax - why?
- Next by thread: Re: test eax, eax - why?
- Index(es):
Relevant Pages
|