Overview

In this reading, you’ll learn about how arrays are organized in x86-64 and how they can be accessed. You will also see the deep relationship between arrays and pointers in C, by seeing how closely related they are at the assembly level.

In a few lectures we will see how the organization of arrays can play a huge role in program performance. For now it will suffice to understand the basic mechanisms involved x86-64 arrays and pointers.

Required Reading

Sections 3.8.0 - 3.8.3 (pp. 255-260) from the course textbook; and the intro to Section 5 of this webpage (i.e. everything before Section 5.1).

Learning Objectives

BASIC Learning Objectives

Each student will be responsible for learning and demonstrating proficiency in the following objectives PRIOR to the class meeting. The reading quiz will test these objectives.

  1. Given an array, write an equation that determines the memory address of the ith element of the array.
  2. Write an instruction that assigns a value to the ith element of an array, given the register that has the starting address of the array and another register that contains i.
  3. Given an expression involving a pointer or array, determine both the type and value of that expression.
  4. Given the starting address of a 2D array and the coordinates of the element of that array, calculate the address where that element is stored.

ADVANCED Learning Objectives

The following objectives should be mastered by each student DURING and FOLLOWING the class session through active work and practice.

  1. Compare the use of array subscripting with that of pointer arithmetic.
  2. Convert between a C assignment statement involving a pointer/array and the equivalent x86-64 instruction.
  3. Trace x86-64 assembly code that involves accessing a 2D array.

Pre-class Exercises

These exercises are geared towards mastering the BASIC learning objectives listed above. You are expected to submit them before class and it is highly recommended that you complete them before attempting the reading quiz.

  1. Assume that you have an array named my_array and an assignment to the ith element of that array as follows.

    my_array[i] = 5;

    Assuming that the starting address of my_array is in %r10 and i is stored in %r13, write the correct x86-64 instruction to perform this assignment, assuming my_array is of the following types.

  2. Consider the following C code.

    short int *my_short_arr = calloc(4, sizeof(short int));
    my_short_arr[0] = 10;
    my_short_arr[1] = 20;
    my_short_arr[2] = 30;
    my_short_arr[3] = 40;

    Assuming that my_short_arr starts at address 0x100, fill in the following table to give the type (e.g. short int) and value (e.g. 0x104 or 30) of the following expressions.

    Express Type Value
    &my_short_arr[1]
    my_short_arr[2]
    my_short_arr+3
  3. Consider the following 2D array/matrix.

    int my_matrix[5][20];

    Assuming that my_matrix starts at address 0x300, what is the address of the following elements of the matrix?