next up previous
Next: Example: Bubble Sort Up: Data Structures Previous: Example: Get String

Integer and Float Arrays

The elements in an integer array are 4 bytes long. The address of element A[I] in an integer array indexed from 0 is:

displaymath257

If the first index of the array is not 0, then the address of A[I] is given by:

displaymath258

An integer array requires 4 times the amount of space for the same number of elements as a byte array. The declaration

A:      .space  20

can be used to declare a 5 element integer array. The declaration

A:      .word   0:5

also declares a 5 element integer array, initialized to 0 in all elements. Although the declarations are equivalent, except for initialization, the .word declaration is preferable because it indicates to the programmer that the array will be used as an integer array.

If A begins at address 0x1001 0100 in the .data section of a SAL program, the addresses of the elements of A are assigned as shown below:

tabular95

The address of the last element, A[4], is given by:

A + 4 ( 4 - 0 ) = 0x1001 0100 + 0x10 = 0x1001 0110.

Since A is an integer array, A[4] extends from byte 0x1001 0110 to byte 0x1001 0113. The next available address after A[4] is 0x1001 0114.

Moving an integer value to or from an integer array would require 4 move operations using the memory array m, because SAL interprets m[I] to be a .byte variable. For integer operations, SAL provides a word-addressable memory array equivalent to the C declaration:

int M[WORDS_IN_MEMORY];

The use of M[I] is the same as m[I], except that M[I] is interpreted as a .word variable. The following instructions load the integer 1996 into A[3]:

        la      I, A
        add     I, I, 12
        move    M[I], 1996

M[I] refers to the contents of a .word in memory located at addresses I through I+3. When used to address a word, I must specify a word address, which is a multiple of 4 bytes. The two least significant bits in a word address are always 0.

SAL provides no way to specify elements of a floating point array. To perform floating point operations on array elements, the array elements must first be moved into .float variables. After the floating point operations are performed, the results must be moved back to the array.

The following SAL program shows what happens when a .float variable is accessed as an array element.



next up previous
Next: Example: Bubble Sort Up: Data Structures Previous: Example: Get String

CS 301 Class Account
Wed Nov 3 15:29:25 AST 1999