Need to understand Assembly Test and JE

This is a discussion on Need to understand Assembly Test and JE within the Tech Board forums, part of the Community Boards category; I'm trying to decipher some assembly code and have come across these two lines: test %ebx,%ebx je 8048e04 %ebx holds ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    35

    Need to understand Assembly Test and JE

    I'm trying to decipher some assembly code and have come across these two lines:

    test %ebx,%ebx
    je 8048e04

    %ebx holds the value 0x804c600, which in turn holds the value 0x24.

    What I think that I understand is that the test command performs an and on the bits contained in the register. An and will only yield 0 if the value in the register is also 0. Otherwise, it will return the value in the register.

    More relevant to this situation is that the test command also sets various condition flags, which is the point here (I think). From what I have read, JE looks at the ZF flag. If that flag is set to 1, it will jump. If it is set to 0, it will not.

    In my case, I would think that ZF would be set, but it is not. Other than when the register contains the value 0 (which it does not in my case), how would these two lines ever result in the JE command actually jumping?

    This is mind numbing to me. Thanks for any help.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    It seems to me more like a test to see if the pointer is NULL (branch if it is), before going on to do something else.

    More context = more information.
    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
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,624
    JZ would be more reasonable than JE, but who's reasonable?!
    Devoted my life to programming...

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Quote Originally Posted by GReaper View Post
    JZ would be more reasonable than JE, but who's reasonable?!
    They are the same - jump if the zero flag is set.

    The test instruction does a bitwise AND operation, sets the flags (including the zero flag) and discards the result. So if ebx was anything other than zero, the zero flag will not be set. If it was zero (or NULL in some cases) the jump will be enacted. As ebx seemed to hold a memory address in this case I'd guess it was checking for a NULL pointer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert assembly>machine code, machine code>assembly
    By wenxinleong in forum C Programming
    Replies: 12
    Last Post: 06-23-2011, 10:42 PM
  2. Replies: 7
    Last Post: 02-15-2011, 01:50 PM
  3. Assembly test eax,eax?
    By maxorator in forum Tech Board
    Replies: 13
    Last Post: 11-13-2006, 11:29 AM
  4. Test at http://www.artlogic.com/careers/test.html
    By zMan in forum C++ Programming
    Replies: 6
    Last Post: 07-15-2003, 06:11 AM

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