0

If I have to translate a simple C function with some kind of addressing like this:

void f(int *a, int *b, long i){
    a[i] = b[i];
}

in System V AMD X86-64 (AT&T standard) assembly, using indirect addressing with index, base register plus a scale value.

So, because there's not any kind of arithmetic on pointer in assembly, the scale value should be 4 for a integer pointer?

Is this code correct?

f:
    pushq %rpb           # editor's note: typo for %rbp
    movq %rsp, %rbp
    movl (%rsi, %rdx, 4), %eax
    movl %eax, (%rdi, %rdx, 4)
    popq %rbp
    ret
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Fabio Carello
  • 1,062
  • 3
  • 12
  • 30
  • 1
    Did you try running it? Is it not working? By the way, you shouldn't assume `int` to be any particular size. Use the types defined in `stdint.h` (e.g. `int32_t`) if you need integers of a particular size. – Michael Jul 05 '13 at 14:26

1 Answers1

1
  1. I don't understand why you need to push and pop rbp. It seems you are not changing it...
  2. Since a and b are int pointers, I would use eax instead of rax
  3. Yes, you need to multiply by 4 when doing calculation of pointer in asm(I assume you are using a machine that thinks sizeof(int)=4. If you are not sure, test it. Or you can use stdint.h with int32_t on your C version for a reliable result.
  4. A possible code could be:

    mov eax, dword ptr [rsi+rdx*4]
    mov dword ptr [rdi+rdx*4], eax
    ret

I think this code should run with no error. Let me know if something goes wrong here. Thanks.

Good luck!

xiangpisaiMM

xiangpisaiMM
  • 160
  • 1
  • 4
  • By the way, I am using intel syntax...You may need to modify it to at&t syntax accordingly... – xiangpisaiMM Jul 05 '13 at 22:42
  • 1. I know there are no effects on rpb, it's just prologue and epilogue convention; 2. Ok edited with movl and 32 bit register 3. Yes, sizeof(int) = 4. Thank you! 4. Edited. Should be the same now, but I still used AT&T. :D – Fabio Carello Jul 06 '13 at 09:51