This question already has an answer here:
- gas: too many memory reference 3 answers
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 ?
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.
来源:https://stackoverflow.com/questions/40440658/compare-two-values-from-the-stack