0

I have char *str1 and char *str2, passed thru %rdi and %rsi, respectively. I'm looping through each byte, but cmp will not let me perform memory to memory comparison. cmp (%rdi), (%rsi)

There is also cmpsb, which I am struggling to understand how to actually use it. Are there any other ways to compare equality of the contents of a pointer to another content of a pointer?

Mat
  • 202,337
  • 40
  • 393
  • 406
  • That's right, you can't compare two memory locations in the same instruction except with the `cmpsX` family of instructions, designed for the purpose. But you can load from one address, and compare with the other. – Weather Vane Mar 03 '20 at 20:14
  • When you say I can load from one address, and compare it with the other, are you talking about doing this with cmp or cmpsx? – Block o Butter Mar 03 '20 at 20:17
  • 2
    You load a register from memory with `mov`, and compare with another memory address using `cmp`. – Weather Vane Mar 03 '20 at 20:19
  • `repe cmpsb` implements memcmp. But note that unlike rep stos and rep movs, it's slow and not worth using on all existing CPUs. – Peter Cordes Mar 03 '20 at 20:20
  • for the https://stackoverflow.com/questions/2531682/gas-too-many-memory-reference, I understand why mov (%rsi), (%rdi) doesn't work for mov, since you cannot move memory to memory, thus why you have to move memory to reg, then reg to memory. But for cmp, how do you work around this, assuming you don't use repe. – Block o Butter Mar 03 '20 at 20:28
  • 2
    @BlockoButter As Weather Vane said: first load one of the operands into a register, then compare that register with the other operand. For example, do `mov (%rdi), %al; cmp %al, (%rsi)`. – fuz Mar 03 '20 at 20:35
  • 1
    Look at (optimized) compiler output for a C function that does `return *p == *q;` or something. movzbl to load one, then byte compare with the other memory operand. – Peter Cordes Mar 03 '20 at 20:41

0 Answers0