-1

For example, what is the difference between

cmpl $0x7, 0x8(%rsp)

and

cmpl $0x7, (%rsp)

Also what is the difference between cmp and cmpl?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • `cmpl $0x7, (%rsp)` is the same as `cmpl $0x7, 0x0(%rsp)` – Chris Dodd Oct 04 '19 at 23:22
  • What about the 0x8 before (%rsp)? – Kenneth Straw Oct 04 '19 at 23:23
  • That's an offset added to the value of `rsp`. Consult documentation about effective address syntax. As for the `l` suffix, you don't need to type that out if the operation size can be inferred from the operands. In this case you do need it because neither operand has an intrinsic size. – Jester Oct 04 '19 at 23:27
  • 1
    Please read the GNU assembler manual. It explains all of this nicely. – fuz Oct 04 '19 at 23:59

3 Answers3

2

A memory operand on x86 in AT&T syntax is generally "offset(base, index, scale)" where offset is a (constant) offset, base is a (base) register, index is an (index) register, and scale is the constant 1, 2, 4 or 8. However, most of these fields can be omitted to get a default value. No offset means an offset of 0. No base means no base regiser. No index and scale means no index register.

In your specific examples, (%rsp) means %rsp as the base register, with no offset and no index. 0x8(%rsp) means %rsp as the base register and 0x8 (8) as the offset.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
2

Suffixes for Widths

Intel describes the instruction mnemonic as CMP (in Intel 64 and IA-32 Architectures software Developer’s Manual), but there are many forms of the instruction, such as comparing 8-, 16, 32-, or 64-bit numbers, comparing an immediate value with a value in memory, and so on.

Some assemblers use suffixes to distinguish the operand width, using:

  • b for byte,
  • w for word (two bytes in Intel’s use in these architectures),
  • l for long word (four bytes), and
  • q for quad word (four words, eight bytes).

If one of the operands is a register, the assembler can figure out (at least theoretically; some assemblers might not feature this) the width from it.

For example, cmp $0x7, %rsp would be a 64-bit compare since %rsp names a 64-bit register. (%esp is a 32-bit register, %sp is a 16-bit register.)

Operand Forms

cmpl $0x7, (%rsp) means a 32-bit compare of the immediate value 0x7 with data in memory at the address contained in the %rsp register.

Without the l suffix, the width in this instruction is not known. 0x7 is an immediate value. Unlike C, there is no default type for it.

Similarly, (%rsp) is a location in memory without an associated type. So the l is needed. (Note the difference here: cmp $0x7, %rbp would compare 7 to the value in %rbp, while cmp $0x7, (%rbp) would compare 7 to the value in the memory at the address in %rbp.)

8(%rsp) means the address resulting from adding 8 to (%rsp).

A fuller form is offset(base, index, scale), which represents the address in base (which is a register) plus index (another register) times scale (a constant 1, 2, 4, or 8) plus offset.

This form is used for indexing arrays: Suppose an array starts at address %rax and has elements 4 bytes wide, and you want the element with the index in %rbx. Then you an address this element with (%rax, %rbx, 4). The additional offset can be added to refer to members within structures within the array or to adjust the base address of the array relative to %rax.

Community
  • 1
  • 1
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • 1
    @ecm: fixed your edit. `cmpL` specifies 32-bit operand size, and thus is an assemble-time error on a 64-bit register. `cmp $7, %rsp` is a 64-bit compare, letting the register name imply the operand-size like a normal person. – Peter Cordes Oct 07 '19 at 07:46
-1

I believe the answer to your question can be found in this other SO question: The difference between cmpl and cmp

Rafe
  • 7,036
  • 5
  • 24
  • 27
  • 1
    Then the sensible thing to do would have been to close as a duplicate, surely? I'm unclear however how that other post answers the offset question. – paxdiablo Oct 04 '19 at 23:50
  • @paxdiablo: The problem with *this* question is that it's 2 unrelated questions about AT&T syntax. So it's too broad, or should be closed as a duplicate of 2 different questions, that being one of them. But agreed that this should have been a comment or just a close vote, definitely not an answer in this state. (Sometimes it can make sense to post an answer with links to other SO answers for questions that are duplicates of two or more *interwoven* things. But this is just "also I'm wondering about this other thing, too".) – Peter Cordes Oct 05 '19 at 03:31