CS255 Syllabus

### Accessing arrays in a high level programming language explained

• One of the most important data structures in high level programming languages is the array

• A computer program needs information to do its work (solve some problem)

An array is one of the many data structures that Computer Science has developed to manage/organize information

The array is a static structure, i.e., the number of elements in an array is fixed at creation and cannot be changed (unless you destroy the array and create a new one)

(In contrast, the linked list data structure is dynamic: the number of elements can change)

• The Array data structure

• Array variable: (CS170 material)

• Array = a serie (collection) of variables

• Each array elements in an array is a variable

All array elements have the same data type

• Important fact:

 Each array element is an independent variable I.e.: each array element can be used separately from each other

• Recall:

• Array elements (in Java and other programming languages) are accessed using:

 The name of the array variable The index of the array element

Example:

• Locating array variables in memory

• Facts:

1. An array has many array elements

2. Each array element is an (independent) variable

3. Variables are stored in computer (RAM) memory

 In order to use a variable, we must know where the variable is stored in memory (I.e.: we we must know the address of the variable

• Conclusion:

 We must know the address of all the array elements !!!

Problem:

 It is very hard to remember the address of so many (array element) variables !!!

Solution:

 We will store an array in a very special way, so that.... We compute the address of an array element using a small amount of information !!!

• Storing array variables in the computer (RAM) memory

• Recall:

 All variables in a computer program --- including array variables --- are stored inside the computer memory

• Storing array variables in the computer memory:

• Array elements are stored consecutively in the memory

Graphically depicted:

Important note:

 By storing the array elements consecutively in the memory, we can compute the location of the array elements using a small amount of infomation !!! We will see how this is done next

• Using array variables

• Recall:

• Array elements (in Java and other programming languages) are accessed using:

 The name of the array variable The index of the array element

Example:

• Information needed to accessing the array elements:

• A computer program that uses array variables must have infomation to let the program remember where an array element is stored in memory !!!

• When the (Java/C) compiler processes an array definition, it records:

1. The starting address of the first array element

 The starting addess of an array is called: base of the array

2. The data type (= size) of each array element

 The data type will tell us the size of each array element !!!

• Base of an array

• Base:

• base of an array = the starting address of the first array element

Graphically depicted:

• Computing the address of array elements

1. The base address of an array is (also) the address of the first element of the array

Graphically:

2. Because the array elements are stored consecutively in memory:

• The address of the second array element is equal to:

 ``` address of the 2nd array element = base address + size of B[0] ```

Graphically explained:

3. And:

• The address of the 3rd array element is equal to:

 ``` address of the 2nd array element = base address + size of B[0] + size of B[1] ```

Graphically explained:

4. And so on

• One more important fact:

• Each array element has the same data type, therefore:

 Each array element has the same size (= # bytes in memory)

• Accessing acrray elements

• Access the array element B[k] stored in memory:

1. First, we have to compute the address of the array element B[k] using:

 Base address of the array The data type (= size) of an array element The index (= k) of the array element B[k]

2. Then:

 Access the memory at the computed address (this is where the array element B[k] is stored in (RAM) memory)

• The address of an array element B[k] can be computed as follows:

 ``` Address of B[k] = Base address of array B + k × size of an array element ```

• Base address = The starting location (address) in the memory where the array is stored

• Size = The number of bytes (size) of an array element

The formula is graphically explained as follows:

• Example 1:

• Suppose we define the following array:

 ``` int B[10]; // Each int variable occupies 4 bytes of memory ! ```

• Suppose the array is stored starting at memory address 7000

Then:

 ``` Base Address of array B = 7000 Size of array B element = 4 (because data type is int) ```

• We can find the address of array variables B[1], B[5] and B[k] (for any value k) as follows:

 ``` Address of B[1] = 7000 + 1*4 Address of B[5] = 7000 + 5*4 Address of B[k] = 7000 + k*4 ```

Grapgically:

• Example 2:

• Suppose we define the following array:

 ``` short B[10]; // Each short variable occupies 2 bytes of memory ! ```

• Suppose the array is stored starting at memory address 7000

Then:

 ``` Base Address of array B = 7000 Size of array B element = 2 (because data type is short) ```

• We can find the address of array variables B[1], B[5] and B[k] (for any value k) as follows:

 ``` Address of B[1] = 7000 + 1*2 Address of B[5] = 7000 + 5*2 Address of B[k] = 7000 + k*2 ```

Grapgically:

• Accessing an array element using a constant array index

• Constant as array index:

 ``` B[6] (6, 3, and 5 are constants B[3] B[5] ```

Accessing an array element with a constant as array index:

• The address of an array element is:

 ``` base address + index * size of array element ^ ^ | | | +---- the size of array element is constant | We use a constant as index ```

Therefore, the expression

 ``` index * size of array element = constant !!! ```

• We can compute the constant

 ``` index * size of array element ```

and use it as the offset in the indirect mode to access the array element !!!

• Example 1:

 ``` Variable definition: int ans; int MyArray[10]; // Array with 10 elements High level language statement: ans = MyArray[0]; The compiler will translate this statement into the following assembelr instructions: movea.l #MyArray, A0 * Now reg A0 has the base address of the array move.l 0(A0), D0 * The address mode 0(A0) will access memory address * array's base address + 0 * which is the memory address of array element MyArray[0] move.l D0, ans * Store MyArray[0] in the variable ans High level language statement: ans = MyArray[1]; The compiler will translate this statement into the following assembelr instructions: movea.l #MyArray, A0 * Now reg A0 has the base address of the array move.l 4(A0), D0 * The address mode 4(A0) will access memory address * array's base address + 4 * which is the memory address of array element MyArray[1] move.l D0, ans * Store MyArray[1] in the variable ans High level language statement: ans = MyArray[5]; The compiler will translate this statement into the following assembelr instructions: movea.l #MyArray, A0 move.l 20(A0), D0 * We must use 20(A0) to access MyArray[5] move.l D0, ans ```

• Accessing array elements with a constant index (A[5]) - DEMO: click here

• Accessing an array element using a variable array index

• Variable as array index:

 ``` B[k] - k is a variable (must be int !) ```

Accessing an array element with a variable as array index:

• The address of an array element is:

 ``` base address + k * size of array element ^ ^ | | | +---- the size of array element is constant | We use a variable as index ```

Therefore, the expression

 ``` k * size of array element ```

is different for different values of k !!!

• We have to compute the expression

 ``` k * size of array element ```

by accessing the variable k in memory !!!

• Example 1:

 ``` Variable definition: int ans; int k; // Assume k has been initialized to some value int MyArray[10]; // Array with 10 elements High level language statement: ans = MyArray[k]; The compiler will translate this statement into the following assembelr instructions: movea.l #MyArray, A0 * A0 = base address of array move.l k, D0 * D0 = index muls #4, D0 * This computes: D0 = 4*D0 * D0 now contains the offset !!! adda.l D0, A0 * Add offset to the base address * Now reg A0 contains the value * base address + k*4 * which is the address of the array elem MyArray[k] move.l 0(A0), D0 * Gets MyArray[k] into register D0 move.l D0, ans * Store the value in ans ```

• Example 2:

 ``` Variable definition: short ans; int k; // Assume k has been initialized to some value short MyArray[10]; // Array with 10 elements High level language statement: ans = MyArray[k]; The compiler will translate this statement into the following assembelr instructions: movea.l #MyArray, A0 * A0 = base address of array move.l k, D0 * D0 = index muls #2, D0 * This computes: D0 = 2*D0 * Use use 2 because: * size of short = 2 bytes !!! * D0 now contains the offset !!! adda.l D0, A0 * Add offset to the base address * Now reg A0 contains the value * base address + k*2 * which is the address of the array elem MyArray[k] move.w 0(A0), D0 * Gets MyArray[k] into register D0 * NOTE: we must use .w because MyArray[k] * is a short variable !!! move.w D0, ans * Store the value in ans * NOTE: we must use .w because ans * is a short variable !!! ```

• Accessing array elements with a variable as index (A[i]) - DEMO: click here

Note:

 Later we will learn about a more powerful addressing mode that will help us access MyArray[i] more easier !!!