tags:

views:

200

answers:

1

Hi,

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 ? Thanks !

+4  A: 

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.

Pascal Cuoq
why `0(%ebx,%ecx,4)` instead of `(%ebx,%ecx,4)` ?I thought that this parameter should be an adress rather than a value...
Charles-Pierre
I never really thought about it, but 1) I'm pretty sure that `0(%ebx,%ecx,4)` and `(%ebx,%ecx,4)` mean exactly the same thing in gas in all contexts where you can find them, and 2) on the other hand, the meaning is different when used in destination or source position, just like l-values in C. Think about the roles of `x` in these two C statements: `x = 2;` and `y = x + 1;`.
Pascal Cuoq