Search code examples
assemblyx86attaddressing-mode

Loop over function args on the stack, using a register as an index for ESP?


Just a short question. Anyone knows if there is any way that I can do this in assembly?

movl $4, %ebx
movl (%ebx)(%esp), %eax

what I'm trying to do is basically create a loop that extras the next argument(fixed size) from the stack.

example:

int foo( int x, int y, int z, int a){
    if(x == y){
         x = z;
       if(y == z){
            printf("%d", a);
      }
    }
}

instead of immediately loading x, y, z and a into the register, can I load a into the register only when I'm sure that the first 2 conditions are true.

p/s: wrote the code on the fly, doesn't really do anything useful.


Solution

  • You want this

    movl $1, %ebx
    movl (%esp,%ebx,$4), $eax
    

    incrementing %ebx by one each time to get to the next argument.