agienka
agienka

Reputation: 396

What is assembly cmp instruction comparing exactly?

I'm learning assembly language and encountered a problem that I don't know even how to ask Google. I'm using gdb to debug simple c program.

I have following code:

cmp    eax,DWORD PTR [rbp-0xc]
jle    0x400cc2

Breaking on the cmp instruction, I'm checking values for the comparison:

p/d $eax // 1000
x/d $rbp-0xc //-24

So I assume, that the jump won't happen. But after going to the next step - I can see that ZF flag is set. Could someone please explain in simple words?

Upvotes: 2

Views: 1482

Answers (1)

Jester
Jester

Reputation: 58762

You seem to have done everything right, except you didn't provide MCVE and didn't show gdb log. You are correct that ZF should NOT be set. Maybe you used gdb wrong.

6           mov dword [rbp-0xc], -24
(gdb) s
7           mov eax, 1000
(gdb)
8           cmp eax, dword [rbp-0xc]
(gdb)
9           jle foo
(gdb) p/d $eax
$1 = 1000
(gdb) x/wd $rbp-0xc
0x7fffffffe254: -24
(gdb) p $eflags
$2 = [ CF PF IF ]

Indeed, the jump will not be taken.

Since you insist you are seeing ZF set, that probably means your x/d is using a byte size instead of the proper dword. If you have 1000 in memory that looks like e8 03 00 00 since x86 is little endian. If you print just the lowest byte as signed it will come out as -24. Use x/wd to force dword size, otherwise gdb defaults to the most recently used size which may not be appropriate.

Upvotes: 4

Related Questions