previous | start | next

Memory Operands for Array Elements

Consider the following loop that accesses the array elements x[0], x[1], ... x[N-1]:

 
  for(int i = 0; i < N; i++) {
    x[i] = 10;
  }

The code generated might look like this:

  ****   for(int i = 0; i < N; i++) {
               movl    $0, -8(%ebp)

               jmp     .L2
       .L3:
  ****     x[i] = 10;
               movl    -8(%ebp), %edx     ;  -8(%ebp) is i
               movl    -16(%ebp), %eax    ;  -16(%ebp) is the beginning address of array x
               movl    $10, (%eax,%edx,4) ; address: %eax  + 4 * %edx

               addl    $1, -8(%ebp)       ; i++
       .L2:
               movl    -8(%ebp), %eax
               cmpl    -12(%ebp), %eax    
               jl      .L3                ; if (i < N) goto L3


previous | start | next