Chapter 6 Data Structures Lecture notes to accompany the text book SPARC Architecture, Assembly Language Programming, and C, by Richard P. Paul, 2nd edition, 2000 Abinashi Dhungel 1
Arrays and Structures Cannot be stored in registers Necessary to perform address arithmetic Registers do not have addresses Array – indexable data structure whose elements are all of same type and given an index i, j , k etc. the corresponding array element can be accessed 2
One-Dimensional Array Address of ith array element = address_of_first_element + i*size_of_array_element_in_bytes (Chap 5) Accessing ith element (shift, add, load/store) sll %ir, 2, %o0 !o0 = i * 4 add %fp, %o0, %o0 !o0 = %fp + i*4 ld [%o0 + a_offset], %o0 !o0 = [%fp + a_offset + %ir * 4] 3
Multidimensional Array Mapping may take many different forms Row major and Column major ordering For 2-D array arranged in row major order address a[i][j] = address of a[0][0] + (number of rows above ith row) * (number of elements in a row * size of an element) + (number of elements left of the jth column) * size of an element Assuming m rows and n columns and size_el for each element = address of a[0][0] + (i*n + j)*size_el Column major order ? 4
Multidimensional Array Mapping ith, jth and kth element of 3-D array int arr[di][dj][dk] in row major order = address of arr[0][0] + i*dj*dk*size_el + j*dk*size_el + k*size_el = %fp + arr_offset + ((i*dj + j)*dk + k)*4 Many languages except Fortran – row major 5
An example of multi-dimensional array access short ary[16][3][4][15] accessing i, j, kth element : (%fp + ary_offset) + (((i*d2 + j)*d3 + k)*d4 + l)*2 mov %i_r, %o0 call .mul mov d2, %o1 !%o0 = i*d2 add %j_r, %o0, %o0 !%o0 = i*d2 + j mov d3, %o1 !%o0 = (i*d2 + j)*d3 add %k_r, %o0, %o0 !%o0 = (i*d2 + j)*d3 + k .mul mov d4, %o1 !%o0 = ((i*d2 + j)*d3 + k)*d4 add %l_r, %o0, %o0 !%o0 = ((i*d2 + j)*d3 + k)*d4 + l sll %o0, 1, %o0 !%o0 = (((i*d2 + j)*d3 + k)*d4 + l) * 2 add %fp, %o0, %o0 ldsh [%o0 + ary_offset], %o0 !%o0 = ary[i][j][k][l] Address computation of an element in an n-dimensional array in general requires n-1 multilication and n additions 6
Lower bound different from zero Example as in pascal, b: array[l1..u1, l2..u2, l3..u3] of integer d1 = u1 – l1 +1 d2 = u2 – l2 + 1 d3 = u3 – l3 + 1 Address of ith ,jth, and kth element is %fp + b_offset + (((i – l1) * dj + (j – l2)) * dk + (k – l3)) * 4 7
Array Bound Checking Can be checked with the lower or upper bound of an array to prevent invalid memory access. lower_bound = 3 ... .global main main: ... subcc %i_r, lower_bound, %g0 bl error ... error: Accessing a constant subscript may be done by an assembler, for example to access arr[-1][3][3] ld [%fp + b0 + (((-1*d2+3) * d3 + 3) << 2)], %o0 8
Address Arithmetic (multiplication by positive constant) Multiplication by a constant can be done by shifting and adding mov %o0, %o1 !times one sll %o0, 2, %o0 !times four add %o0, %o1, %o1 !times five Easier with binary numbers Multiply %o0 by (03514)8 = 011 101 001 100 binary sll %o0, 2, %o1 sll %o1, 1, %o0 add %o0, %o1, %o1 sll %o0, 3, %o0 sll %o0, 2, %o0 sll %o0, 1, %o0 9
Multiplying with string of 1’s 111 is same as multiplying with (1000 – 1) sub %g0, %o0, %o1 sll %l0, 3, %o0 add %o0, %o1, %o1 !times seven 1110 is same as multiplying with (10000 – 10) 10
Structures Block of contiguous memory to store variables (fields) Fields may be of different types Must be aligned to the highest sized data element struct example { int a, b; char d; short x, y; int u, v; } a_offset = 0 b_offset = 4 d_offset = 8 x_offset = 10 y_offset = 12 u_offset = 16 v_offset = 20 str_size = 24 str_offset = -str_size & -4 = -24 ld [%fp + str_offset + a_offset], %o0 !o0 = a ld [%fp + str_offset + b_offset], %o0 !o0 = b ldub [%fp + str_offset + d_offset], %o0 !o0 = d ldsh [%fp + str_offset + x_offset], %o0 !o0 = x ldsh [%fp + str_offset + y_offset], %o0 !o0 = y ld [%fp + str_offset + u_offset], %o0 !o0 = u ld [%fp + str_offset + v_offset], %o0 !o0 = v 11
Alignining fields struct ex { int a, char b; short c; int d; } a_offset = 0 b_offset = align(a_offset + sizeof(a)) c_offset = align(b_offset + sizeof(b)) d_offset = align(c_offset + sizeof(c )) aligning any size x with b bytes can be done by (x + (b-1)) & -b a_offset = 0 b_offset = ((0+4) + 3) & -4 = 4 c_offset = ((4+1) + 1) & -2 = 6 d_offset = ((6+2) + 3) & -4 = 8 str_size = ((8+4) + 3) & -4 = 12 str_offset = -12&-4 = -12 12