## LSU EE 4720 -- Spring 2015 -- Computer Architecture # ## Array Access Examples # Time-stamp: <23 January 2015, 10:03:50 CST, koppel@dmk-laptop> ################################################################################ ## Array Access Examples # Given the base address of an array (address of first element, say &a[0]), # need to compute the address of an element at index i, (say, &a[i]). # To do this compute: # # English: Address of i'th element of a = address of a + i * size of element. # :Example: # # Array accesses. In most examples i is the index (the number of the # element to load). Note that i must be multiplied by the size of the # element before adding it on to the address of the first element. # # Registers: a, s1; b, s5; s, s2; us, s3; c, s4; i, t0; x, t1 # char *c; ... # $s4 = c; $t0 = i # x = c[i]; # add $t5, $s4, $t0 # $t5 -> &c[i] (Address of c[i].) lb $t1, 0($t5) # x = c[i]; $t1 -> c[i] # char *c; ... # $s4 = c; $t0 = i # x = c[i+1] + c[i+2]; # add $t5, $s4, $t0 # $t5 -> &c[i] (Address of c[i].) lb $t6, 1($t5) # $t6 -> c[i+1] lb $t7, 2($t5) # $t7 -> c[i+2] add $t1, $t6, $t7 # x = c[i+1] + c[i+2] # int *a; ... # $s1 = a; $t0 = i # x = a[i]; sll $t5, $t0, 2 # $t5 -> i * 4; Each element is four characters. add $t5, $s1, $t5 # $t5 -> &a[i] (Address of a[i].) lw $t1, 0($t5) # x = a[i]; $t1 -> a[i] # int *a; ... # $s1 = a; $t0 = i # x = a[i+1] + a[i+2]; # sll $t5, $t0, 2 # $t5 -> i * 4; Each element is four characters. add $t5, $s1, $t5 # $t5 -> &a[i] (Address of a[i].) lw $t6, 4($t5) # $t6 -> a[i+1] lw $t7, 8($t5) # $t7 -> a[i+2] add $t1, $t6, $t7 # x = a[i+1] + a[i+2] # int x, j, *a, *b; # $t1 = x; $t2 = j; $s1 = a; $s2 = b # j = 3; # b = a + j; # x = *b; // x = a[3]; addi $t2, $0, 3 # j = 3; sll $t5, $t2, 2 # $t5 -> j * 4 add $s5, $s1, $t5 # b = a + j; lw $t1, 0($s5) # x = *b = a[j] # short *s; ... # $s2 = s; $t0 = i # x = s[i]; # sll $t5, $t0, 1 # $t5 -> i * 2; Each element is two characters. add $t5, $s2, $t5 # $t5 -> &s[i] (Address of s[i].) lh $t1, 0($t5) # x = s[i]; $t1 -> s[i] # $s3 = us; $t0 = i # unsigned short *us; # x = us[i]; # sll $t5, $t0, 1 # $t5 -> i * 2; Each element is two characters. add $t5, $s3, $t5 # $t5 -> &us[i] (Address of us[i].) lhu $t1, 0($t5) # x = us[i]; $t1 -> us[i] # struct str { int i; char c; int j; char p[3]} ; # str a; # .. # x = a.i + a.c; // $t1 -> x; $a1 -> &a (address of a) lw $t1, 0($a1) # $t1-> a.i; lb $t2, 4($a1) # $t2-> a.c; add $t1, $t1, $t2 ####