0

I stuck in my assembler code where I want to compare two values of the stack.

x86, Syntax AT&T

cmpl -4(%ebp), 4(%ebp)

Error: too many memory references for `cmp'

I think it is not possible to compare two values based on a multiplier and ebp. Any suggestions ?

old_timer
  • 69,149
  • 8
  • 89
  • 168
jam
  • 1,253
  • 1
  • 12
  • 26
  • 3
    In x86 it is impossible to use two memory operands with simple instructions like `CMP`. So copy/`MOV` one of the memory values to `EAX` and then compare the other to `EAX`. – zx485 Nov 05 '16 at 16:57
  • The first few google hits for `"too many memory references for"` all have answers. e.g. http://stackoverflow.com/questions/2531682/gas-too-many-memory-reference. Most of them are about mov, not cmp, but MOVS and CMPS both exist. Ira's answer makes a good point that CMP is different from wanting to add, for example, since you could do it another way. – Peter Cordes Nov 06 '16 at 17:34

1 Answers1

2

You can compare two values in memory using the CMPSD instruction.

Op wants to do something equivalent to:

  cmpl -4(%ebp), 4(%ebp)

He can do that by placing the addresses of the memory locations of interest in ESI and EDI respectively, and then using the CMPSD memory-to-memory string-compare instruction:

  lea   -4(%ebp), %esi
  lea    4(%ebp), %edi
  cmpsd

(forgive my non-expert abuse of AT&T syntax).

That's different than anybody would do this in practice. The other answers provided here (load a value into a register and compare) are much more practical. If nothing else, those solutions only burn one register, and this hack burns two.

Lesson: in assembler, there's almost always more than one way to skin a cat.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Ira Baxter
  • 93,541
  • 22
  • 172
  • 341