When to use TEST vs CMP in Assembly Code?

This is a discussion on When to use TEST vs CMP in Assembly Code? within the Tech Board forums, part of the Community Boards category; Hi there, I'm sorry if this question doesn't belong here, this may be deleted if that is the case. Anyways, ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    12

    When to use TEST vs CMP in Assembly Code?

    Hi there,

    I'm sorry if this question doesn't belong here, this may be deleted if that is the case.

    Anyways, I am curious when TEST and CMP are used in assembly codes? According to examples in my textbook, it seems like they could be used interchangeably...but then again, why would they have them be separate instructions if so?

    Thank you for taking the time to read my question (and to answer)!


    --Genxi

  2. #2
    Registered User
    Join Date
    Oct 2011
    Posts
    890
    On x86, test does a binary AND between the operands, just does not save the result anywhere. cmp subtracts the second operand from the first without actually modifying the first operand.

    In other words, if you wanted to check if bit 6 (01000000b = 26 = 64) is set in register ch, then you'd use test ch, 64. If you wanted to see if ch is less than/equal to/greater than 64, then you'd do cmp ch, 64.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    12
    Quote Originally Posted by Nominal Animal View Post
    On x86, test does a binary AND between the operands, just does not save the result anywhere. cmp subtracts the second operand from the first without actually modifying the first operand.

    In other words, if you wanted to check if bit 6 (01000000b = 26 = 64) is set in register ch, then you'd use test ch, 64. If you wanted to see if ch is less than/equal to/greater than 64, then you'd do cmp ch, 64.

    Ah, that makes sense. Is it possible to use "cmp ch,64" instead of "test ch, 64" to test equality since you know that if ch and 64 are equal, their difference is simply 0.

    In another words, are the follow codes the same?:

    Code:
    cmp ch,64
    je .L7
    Code:
    test ch,64
    je .L7

    Thank-you again!

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    890
    Quote Originally Posted by Genxi View Post
    Ah, that makes sense. Is it possible to use "cmp ch,64" instead of "test ch, 64" to test equality
    No. The former compares for equality, and the latter just tests bit 6 (26=64). test only considers the bits that are set in the second operand, it ignores all the other bits.

    Remember, the difference is that cmp does a subtraction, and test a binary AND operation, with the result discarded and only the flags affected. They are two very different operations.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. is object code the same as assembly source code?
    By c_weed in forum Tech Board
    Replies: 3
    Last Post: 01-05-2012, 07:25 PM
  2. Need to understand Assembly Test and JE
    By joatmon in forum Tech Board
    Replies: 3
    Last Post: 09-29-2011, 03:18 AM
  3. Generate Assembly code and Binary code also
    By Hannibal2010 in forum C Programming
    Replies: 16
    Last Post: 07-07-2011, 06:43 AM
  4. Convert assembly>machine code, machine code>assembly
    By wenxinleong in forum C Programming
    Replies: 12
    Last Post: 06-23-2011, 11:42 PM
  5. Assembly test eax,eax?
    By maxorator in forum Tech Board
    Replies: 13
    Last Post: 11-13-2006, 12:29 PM

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