Assembly test eax,eax?

This is a discussion on Assembly test eax,eax? within the Tech Board forums, part of the Community Boards category; What should this do: Code: test eax,eax I've seen it in several places. Am I right test does bitwise AND ...

  1. #1
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

    Assembly test eax,eax?

    What should this do:
    Code:
    test eax,eax
    I've seen it in several places.

    Am I right test does bitwise AND between the arguments?
    But when you use test eax,eax, it's useless, because they're the same numbers?
    It's like (var&var) in C++.

    What else does test eax,eax do? Why is it used?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    IIRC, because mov eax,value doesn't set any of the condition flags, so you need a test of some sort to set the flags if you plan on doing some kind of branch.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Now, when test eax,eax is followed by a jz address, then jz checks the ZF flag?
    So it just checks if eax is 0?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    Seems like it
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >So it just checks if eax is 0?
    Yes.
    My best code is written with the delete key.

  6. #6
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I'm totally confused, here's some code (it's inside of a function):
    Code:
    test eax,eax
    jz label2
    retn
    :label
        db 00h
    :label2
        mov eax,label
        retn
    :label3
        mov edx,[eax]
    If eax is zero, it is set to zero? And :label3 is never reached, because if eax is not zero, then it returns immediately, and otherwise it sets eax to 0 and then returns.

    And even if it would reach :label3, eax would be 0 and then it gives edx the value that is on the memory address 0x0? It doesn't make sense.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >If eax is zero, it is set to zero?
    No, test doesn't change the value. It's basically an AND instruction that doesn't assign the result to a destination.

    >And :label3 is never reached
    Not in that snippet, but I doubt that's the whole of the program.
    My best code is written with the delete key.

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    No, test doesn't change the value. It's basically an AND instruction that doesn't assign the result to a destination.
    I don't mean that.
    Code:
    test eax,eax
    jz label2
    If eax is zero, it jumps to label2.
    Label2 sets it to zero:
    Code:
    :label
        db 00h
    :label2
        mov eax,label
        retn
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Label2 sets it to zero:
    It doesn't seem that way to me, but then again, none of your snippet makes sense out of context.
    My best code is written with the delete key.

  10. #10
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    It's from a disassembled program...

    What does it seem to you?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It sets eax to the address of label, not the value there. Then it returns, and eax is the return value.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Then eax points to 0.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Only if it's interpreted as a pointer to a single char.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Otherwise it points to 00B8D94Bh (the hex of the next instructions , not very meaningful)
    It can also be used to jump to that place (which seems useless here).

    So I think it is meant to be used as a single char (ah).
    "The Internet treats censorship as damage and routes around it." - John Gilmore

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Integer Emulation
    By Elysia in forum C++ Programming
    Replies: 31
    Last Post: 03-18-2008, 02:03 PM
  2. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  3. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 01:55 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. C,C++,Perl,Java
    By brusli in forum C Programming
    Replies: 9
    Last Post: 12-31-2001, 03:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21