-1

I have to write this C -language program as assembly.

void change(int64_t *num1, int64_t *num2){
    int64_t number = *num1;
    *num1 = *num2;
    *num2 = number;
}

This is what I have tried:

movq %rdi,%rdx
movq %rsi,%rdi
movq %rdx,%rsi

What am I doing wrong?

moi
  • 1
  • You need two reads from memory and two writes to memory to swap the content. So far you are only swapping the pointers to the variables. Hint: `movq %rax, [%rdi]` to read from `rdi`. (I'm just commenting, since this Q has likely too many duplicates and is likely not worth for future readers.) – Aki Suihkonen May 08 '22 at 09:17
  • You *are* changing the values in registers, but that's all, not swapping the pointed-to memory locations. Look at compiler output for the C function on https://godbolt.org/ – Peter Cordes May 08 '22 at 09:27
  • 1
    @AkiSuihkonen: Your `movq %rax, [%rdi]` hint is a broken mix of AT&T and Intel syntax that won't assemble. `movq %rax, (%rdi)` would store RAX to memory, using the address in RDI. – Peter Cordes May 08 '22 at 09:28

0 Answers0