Regarding cmp / jg, jle, etc in AT&T syntax assembly

↘锁芯ラ 提交于 2020-05-09 06:36:05

问题


So every single resource online tells me that something like this:

cmp %eax, %ebx
jg < something >

would jump to < something > if eax was greater than ebx. But I have another piece of code that seems to contradict this:

cmp $0x2, %eax
jg  < something>

as it jumps to < something > when eax has the value 3.

Am I missing something, or does cmp a, b - jg execute if b > a and not a>b? And does this apply to other jump statements as well?


回答1:


When we read something like

cmp $0x2, %eax
jg  < something >

we know the assembler used is one that reverses the position of the operands of an instruction. That's because Intel's syntax dictates that the destination operand comes before the source operand and clearly an immediate value like $0x2 can't ever be a destination!

Knowing the order of things we now interpret your first code snippet as

cmp ebx, eax
jg < something >  ;jump if EBX > EAX

and the second code snippet as

cmp eax, 2
jg < something >  ;jump if EAX > 2

And does this apply to other jump statements as well?

It does.




回答2:


See if THIS ANSWER helps you. If not, ask for clarification.

Scroll down past the other guy's source code and stuff in that answer to search for this string...

This biz, the cmpl %eax,%edx instruction, is one of the forms of the "compare" instructions

I wrote an explanation of the "sense" of CMP and various conditional jumps. See if it answers your questions here.

If you are still confused, post a note here and someone else may give you a better answer.

Hope it helps.



来源:https://stackoverflow.com/questions/29574147/regarding-cmp-jg-jle-etc-in-att-syntax-assembly

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