next up previous
Next: Example: Get String Up: Data Structures Previous: Memory Array

Byte Arrays

A SAL array of 5 bytes initialized to 0 is declared in the .data section as:

A:     .byte   0:5

This is an extension of the declaration of a character variable. A is the name of a character variable which is the first element of a 5 element character array.

The general form of a character variable declaration is:

{ VariableName: }  .byte   { Value:NumElements }

Although this resembles an array declaration, SAL has no array facilities other than the memory array, m[]. Instead, a block of NumElements memory cells are assigned from m[] and the first cell is located at address VariableName.

To access element A[I], the address of element I is calculated by adding I to the base address of the array, which is the address of the first element of the array, A[0]. A[I] can be accessed at memory location m[A+I]. In general,

displaymath243

where the A on the left side is used symbolically, while the A on the right side means the numerical value of the address of the first element of the array.

When using the m array in SAL, the array index must be a constant or a single integer variable. To access A[I] using the m array, the address of A[0] must first be added to the value of I . The SAL la (load address) instruction is used to assign the address of a label to a variable. The following SAL code prints element 5 of a character array, A:

        la      I, A    # assign address of A to I
        add     I, I, 5 # add 5 to base address
        put     m[I]    # print A[5]

Note the A[5] is the sixth element of the A array.

The memory array, m[I], may be used as an operand in SAL instructions anywhere that a variable can be used.

Note that the la and move instructions are fundamentally different. The instruction

        move    I, A

moves the contents of memory cell A into I. Since A is a .byte variable, one byte is moved into the memory cell labelled by I.

The instruction

        la      I, A

moves the address of memory cell A into I. Since addresses are unsigned 32 bit integers, the 4 byte address of A is moved into 4 memory cells starting at the location of I. For this reason, I must be a .word.

Suppose that A is declared in the .data section at location 0x1001 0100. The elements of A are assigned addresses as shown below:

tabular33

The instructions below assign the character 'X' to A[3]:

        la      I, A      # I <-- 0x1001 0100
        add     I, I, 3   # I <-- 0x1001 0103
        move    m[I], 'X' # move 'X' to cell 0x1001 0103


next up previous
Next: Example: Get String Up: Data Structures Previous: Memory Array

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