Question

I'm learning asm and here's one of my (many) problems: I'd like to change the value of some index of an array. Let's say that:

  • %eax contains my new value
  • the top of the stack (ie (0)%esp) contains the index of the array
  • -4(%ebp) contains the adress of the array.

I've tried movl %eax, (-4(%ebp),0(%esp),4) but it did not work. Worse, it throws a syntax error : bobi.s:15: Error: junk `(%ebp),0(%esp),4)' after expression

What is the correct syntax?

Was it helpful?

Solution

There is no single instruction to do this in x86 assembly. You have to find an available register, use it to store the address of the array that you get from -4(%ebp), find another register to hold the index 0(%esp), and only then does it become possible to access the cell you are interested in (and in more RISC-like assemblies, you'd still need to add these two registers together before you can do the memory access).

Assuming the registers are available, something like:

movl -4(%ebp), %ebx
movl 0(%esp), %ecx
movl %eax, 0(%ebx,%ecx,4)

should work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top