Вы находитесь на странице: 1из 6

Facultad de Ingeniería

Departamento de Electrónica y Ciencias de la Computación

Carrera de Ingeniería Electrónica


Arquitectura de Computadores

Working with Arrays


Agosto 2013

Tek
Working with Arrays

Arrays

• Useful for accessing large amounts of similar data


• Array element: accessed by index
• Array size: number of elements in the array

• Example: 5-element array


• Base address = 0x12348000 (address of the first array element,
array[0])
• First step in accessing an array: load base address into a
register

0x12340010 array[4]
0x1234800C array[3]
0x12348008 array[2]
0x12348004 array[1]
0x12348000 array[0]
Working with Arrays

Accessing Arrays

int array[5];
array[0] = array[0] * 2;
array[1] = array[1] * 2;
# array base address = $s0
lui $s0, 0x1234 # put 0x1234 in
# upper half of
# $S0
ori $s0, $s0, 0x8000 # put 0x8000 in
# lower half of
0x12340010 array[4] # $s0
0x1234800C array[3]
0x12348008 array[2] lw $t1, 0($s0) # $t1 = array[0]
0x12348004 array[1] sll $t1, $t1, 1 # $t1 = $t1 * 2
0x12348000 array[0] sw $t1, 0($s0) # array[0] = $t1
lw $t1, 4($s0) # $t1 = array[1]
sll $t1, $t1, 1 # $t1 = $t1 * 2
sw $t1, 4($s0) # array[1] = $t1
Working with Arrays

Allocating Space for Arrays


• First step is to reserve sufficient space for the array:
          .data
array:    .word   2, 3, 5, 7, 11, 13        # 6 integers
size:     .word   6
. . .
lw      $t3, size
la      $t1, array                # get array address
li      $t2, 0                    # set loop counter
print_loop:
beq     $t2, $t3, print_loop_end  # check for array end

lw      $a0, 0($t1)               # print value at the array
li      $v0, 1                    # pointer
syscall

addi    $t2, $t2, 1               # advance loop counter
addi    $t1, $t1, 4               # advance array pointer
j       print_loop                # repeat the loop
print_loop_end:

          .data
array:    .space  24    # allocate 24 consecutive bytes with storage un­initialised
                        # could be used as a 24­element character array, or a
                        #  6­element integer array; a comment should indicate
                        #  which!
Working with Arrays

Arrays Using for Loops

int array[1000]; # $s0 = array base address, $s1 = i


int i; # initialization code
int size; lui $s0, 0x23B8 # $s0 = 0x23B80000
ori $s0, $s0, 0xF000 # $s0 = 0x23B8F000
size = 1000; addi $s1, $0, 0 # i = 0
addi $t2, $0, 1000 # $t2 = 1000
for ( i = 0; i < 1000; i++ )
array[i] = array[i] * 8; loop:
slt $t0, $s1, $t2 # i < 1000?
beq $t0, $0, done # if not then done
sll $t0, $s1, 2 # $t0 = i * 4
# (byte offset)
add $t0, $t0, $s0 # address of
# array[i]
lw $t1, 0($t0) # $t1 = array[i]
sll $t1, $t1, 3 # $t1 = array[i] * 8
sw $t1, 0($t0) # array[i] *= 8
addi $s1, $s1, 1 # i++
j loop # repeat
done:
Further Reading

• PATTERSON, David A.; HENNESSY, John L. Computer


Organization and Design: The Hardware/Software Interface.
Fourth Edition, Revised Printing. Morgan Kaufmann, 2011.
Chapter 2.

Some of this material has been adapted from slides prepared by


Jason Bakos, University of South Carolina; William D McQuain,
Virginia Tech

Вам также может понравиться