C语言 cmpl 和 cmp 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24118562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The difference between cmpl and cmp
提问by Sarah cartenz
I am trying to understand assembly to be able to solve a puzzle. However I encountered the following instructions:
我正在尝试了解汇编以解决难题。但是我遇到了以下说明:
0x0000000000401136 <+44>: cmpl 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
x7,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
我认为它的作用是:0x14(%rsp) 的值是-7380。根据我的理解,cmpl 比较无符号。也执行跳转。那么是不是 (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!
另外,如果您对如何操纵此跳跃有任何建议!
回答by glglgl
According to my understanding
cmplcompares unsigned.
根据我的理解
cmpl比较无符号。
It does both, in a way.
从某种意义上说,它两者兼而有之。
The difference in signed vs. unsigned is here the usage of the jump instructions.
有符号和无符号的区别在于跳转指令的使用。
For >, there is jafor unsigned and jgfor signed (jump if above and jump if greater).
对于>,有无ja符号和jg有符号(如果高于则跳转,如果更大则跳转)。
For <, there is jbfor unsigned and jlfor signed (jump if below and jump if less).
对于<,有无jb符号和jl有符号(低于则跳转,低于则跳转)。
To be exact, here is the meaning of several jump commands:
准确的说,这里是几个跳转命令的含义:
For unsigned 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 equalFor signed 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
对于无符号比较:
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对于签名比较:
##代码##
回答by typ1232
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 JZand JEbeing the same).
我不认为 x86 实际上有一个名为CMPL. 它可能是您的汇编语法的一部分,用于提供有关操作数或其他内容的提示(例如JZ和JE相同)。
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.
将第一个源操作数与第二个源操作数进行比较,并根据结果设置 EFLAGS 寄存器中的状态标志。比较是通过从第一个操作数中减去第二个操作数,然后以与 SUB 指令相同的方式设置状态标志来执行的。当立即数用作操作数时,它被符号扩展到第一个操作数的长度。
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 JAto JBE.
如何操纵跳跃?如果您确定跳转应该执行与它正在执行的完全相反的操作,则只需更改JA为JBE。

