0

How do you convert a code like "a = Array[i]" into x86 assembly?

On ARM, you could do something like LDR R3, [R2, R1], but I couldn't manage to find a similar thing for x86.

There's "mov eax, [array + 0 * 4]", but you need to put integers instead of registers for it to work. So is there an equilivant in x86 as well?

Thank you

Ada
  • 21
  • 3
  • 1
    `mov eax, [array + rdi+4]` works, if `array` is in the low 32 bits of virtual address space. And `[rcx + rdx*4]` always works, exactly like scaled indexing on ARM. So whatever you actually tried, you haven't shown a [mcve] of it, so the question should be closed. I picked some relevant duplicates with examples. Also, if you want to see how C compiles, ask a compiler, e.g. for a function that takes an `int*` and a `size_t`, or that takes just an integer and return an element of a global array. [How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116) – Peter Cordes Jan 29 '22 at 06:10
  • ARM's addressing modes are pretty good, but fancy addressing has always been a CISC specialty. ARM gives you a base register, plus an offset from either a register (optionally scaled by operand size) or from an immediate of 12 bits or so. x86 lets you use base register, offset register **and** immediate displacement all together in any instruction. [...] – Nate Eldredge Jan 29 '22 at 06:38
  • 2
    The immediate is a full 32 bits sign-extended, so it can either be an offset, or an absolute base address if you are working in the low 2GB of memory, or both combined. And the offset register can be scaled by 1,2,4 or 8, independent of operand size. A separate PC-relative addressing mode gives you PC plus 32-bit signed immediate, for position-independent code (no register offset here, sadly). These are the things you gain by being able to let an instruction go on for arbitrarily many bytes. About the only thing it doesn't have is ARM's pre/post-increment. – Nate Eldredge Jan 29 '22 at 06:39

0 Answers0