Introduction to Computer Systems Arrays

Arrays

Arrays in C

C:


void a(int A[10] int x)
{
    A[0] = x;
    A[1] = A[0] << 2;
}

Assmebly:


movl  %esi, (%rdi)
sall  $2, %esi
movl  %esi, 4(%rdi)   #each int is 4 bytes long, so to get A[1], do *A + 4
ret


2D Arrays

C:


const int size = 10;
int B[size][size];

int b(int x, int y)
{
    return B[x][y]; // location = B + (size * x) + y
}

Assembly:


b:
    movslq  %esi, %rsi
    movslq  %edi, %rdi
    leaq    (%rdi, %rdi, 4), %rax
    leaq    (%rdi, %rdi, 4), %rdx
    leaq    B(%rip), %rax
    movl    (%rax, %rdx, 4), %eax