The difference between cmpl and cmp

前端 未结 3 1611
星月不相逢
星月不相逢 2021-02-07 17:50

I am trying to understand assembly to be able to solve a puzzle. However I encountered the following instructions:

0x0000000000401136 <+44>:    cmpl   $0x7,         


        
相关标签:
3条回答
  • 2021-02-07 18:27

    Just to add one extra detail for clarification to @glglgl's excellent answer is that the "l" in "cmpl" is an operation suffix indicating that the operation is being done on a long number (32 bit integer or 64-bit floating point).

    0 讨论(0)
  • 2021-02-07 18:33

    According to my understanding cmpl compares unsigned.

    It does both, in a way.

    The difference in signed vs. unsigned is here the usage of the jump instructions.

    For >, there is ja for unsigned and jg for signed (jump if above and jump if greater).

    For <, there is jb for unsigned and jl for signed (jump if below and jump if less).

    To be exact, here is the meaning of several jump commands:

    For unsigned comparisons:

    JB/JNAE (CF = 1)           : Jump if below/not above or equal
    JAE/JNB (CF = 0)           : Jump if above or equal/not below
    JBE/JNA (CF = 1 or ZF = 1) : Jump if below or equal/not above
    JA/JNBE (CF = 0 and ZF = 0): Jump if above/not below or equal
    

    For signed comparisons:

    JL/JNGE (SF <> OF)          : Jump if less/not greater or equal
    JGE/JNL (SF = OF)           : Jump if greater or equal/not less
    JLE/JNG (ZF = 1 or SF <> OF): Jump if less or equal/not greater
    JG/JNLE (ZF = 0 and SF = OF): Jump if greater/not less or equal
    
    0 讨论(0)
  • 2021-02-07 18:44

    I don't think x86 actually has an instruction called CMPL. It's probably part of your assembler syntax to give hints on operands or something else (like JZ and JE being the same).

    From the intel manual on what it is doing:

    Compares the first source operand with the second source operand and sets the status flags in the EFLAGS register according to the results. The comparison is performed by subtracting the second operand from the first operand and then setting the status flags in the same manner as the SUB instruction. When an immediate value is used as an operand, it is sign-extended to the length of the first operand.

    Sign-ness is given implicitly, because of the two's complement representation of numbers.

    How to manipulate the jump? If you are sure that the jump should do the exact opposite than what it is doing, you just have to change JA to JBE.

    0 讨论(0)
提交回复
热议问题