Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...