The difference between cmpl and cmp

时光毁灭记忆、已成空白 提交于 2019-12-21 03:53:21

问题


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

0x0000000000401136 <+44>:    cmpl   $0x7,0x14(%rsp)
0x000000000040113b <+49>:    ja     0x401230 <phase_3+294>

What I think its doing is: The value of 0x14(%rsp) is -7380. According to my understanding cmpl compares unsigned. Also the jump is performed. So can it be that (unsigned)-7380 > 7 (unsigned)7380 > 7--> jump

I actually don't want it to jump. But is this the correct explanation or not? Am I flipping arguments?

Also if you have any advice about how to manipulate this jump!


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/24118562/the-difference-between-cmpl-and-cmp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!