Array Declaration and Storage Allocation MIPS Arrays 1

  • Slides: 18
Download presentation
Array Declaration and Storage Allocation MIPS Arrays 1 The first step is to reserve

Array Declaration and Storage Allocation MIPS Arrays 1 The first step is to reserve sufficient space for the array: list: . data. space 1000 # reserves a block of 1000 bytes The label is a symbolic name for the address of the beginning of the array. list == 1004000 Memory 1004000 1004001 1004002 1004003 1004004. . . allocation for list This yields a contiguous block of bytes of the specified size. 1004999 The size of the array is specified in bytes… could be used as: • array of 1000 char values (ASCII codes) • array of 250 int values • array of 125 double values There is no sense in which the size of the array is "known" by the array itself. CS@VT September 2010 Computer Organization I © 2006 -10 Mc. Quain,

Array Declaration with Initialization MIPS Arrays 2 An array can also be declared with

Array Declaration with Initialization MIPS Arrays 2 An array can also be declared with a list of initializers: . data vowels: . byte pow 2: . word 'a', 'e', 'i', 'o', 'u' 1, 2, 4, 8, 16, 32, 64, 128 Address of vowels[k] == vowels + k 1004000 97 1004001 1004002 105 1004003 111 1004004 117 alloc for vowels names a contiguous block of 5 bytes, set to store the given values; each value is stored in a single byte. Memory 1004005 1004006 1004007 1004008 Address of pow 2[k] == pow 2 + 4 * k 1004009 1004010 1 1004012 alloc for pow 2 names a contiguous block of 32 bytes, set to store the given values; each value is stored in a word (4 bytes) 2 CS@VT September 2010 Computer Organization I © 2006 -10 Mc. Quain,

Another View MIPS Arrays 3 69 10040001 10040002 6 F 75 01 00 00

Another View MIPS Arrays 3 69 10040001 10040002 6 F 75 01 00 00 00 02 00 00 00 10040012 65 10040008 61 10040000 Viewed as hex nybbles, the contents of memory would look like (in little-endian): 0110 0001 Note that endian-ness affects the ordering of bytes, not the ordering of the nybbles within a byte. CS@VT September 2010 Computer Organization I © 2006 -10 Mc. Quain,

Array Traversal and Initialization MIPS Arrays 4 Here's an array traversal to initialize a

Array Traversal and Initialization MIPS Arrays 4 Here's an array traversal to initialize a list of integer values: . data list: . space listsz: . word main: 1000 250 . text lw $s 0, listsz la $s 1, list li $t 0, 0 # using as array of integers # $s 0 = array dimension # $s 1 = array address # $t 0 = # elems init'd initlp: beq $t 0, $s 0, initdn sw $s 1, ($s 1) # list[i] = addr of list[i] addi $s 1, 4 # step to next array cell addi $t 0, 1 # count elem just init'd b initlp initdn: li $v 0, 10 syscall QTP: why 4? CS@VT September 2010 Computer Organization I © 2006 -10 Mc. Quain,

Array Traversal Details MIPS Arrays 5 1004008 . . . initlp: beq $t 0,

Array Traversal Details MIPS Arrays 5 1004008 . . . initlp: beq $t 0, $s 0, initdn sw $s 1, ($s 1) addi $s 1, 4 addi $t 0, 1 b initlp 1004008 1004012 1004016 initdn: 1004020 A variable that stores an address is called a pointer. Here, $s 1 is a pointer to a cell of the array list. We can re-target $s 1 to a different cell by adding an appropriate value to it. CS@VT September 2010 Computer Organization I 1004020 1004024 © 2006 -10 Mc. Quain,

Alternate Traversal Logic MIPS Arrays 6 This traversal uses pointer logic to terminate the

Alternate Traversal Logic MIPS Arrays 6 This traversal uses pointer logic to terminate the loop: . data list: . space listsz: . word main: . text la lw addi sll add 1004008 1000 250 $s 1, $s 0, 1004012 listsz $s 0, -1 $s 0, 2 $s 0, $s 1 1004012 # index of last cell # offset of last cell # ptr to last cell initlp: bgt $s 1, $s 0, initdn sw $s 1, ($s 1) addi $s 1, 4 b initlp initdn: li $v 0, 10 syscall 1004016 . . . 1004996 1004024 QTP: rewrite this using the do-while pattern shown in the previous lecture CS@VT September 2010 Computer Organization I 1005000 © 2006 -10 Mc. Quain,

Array Bounds Issues MIPS Arrays 7 An array can also be declared with a

Array Bounds Issues MIPS Arrays 7 An array can also be declared with a list of initializers: 1004004 'a', 'e', 'i', 'o', 'u' 1, 2, 4, 8, 16, 32, 64, 128 What happens if you access an array with a logically-invalid array index? ? ? contents of address 1004005 1004006 1004007 1004008 1 1004012 2 While vowels[5] does not exist logically as part of the array, it does specify a physical location in memory. . What is actually stored there is, in general, unpredictable. In any case, the value is not one that we want… CS@VT September 2010 Computer Organization I alloc for pow 2 vowels[5] 117 alloc for vowels . data vowels: . byte pow 2: . word Memory 1004036 © 2006 -10 Mc. Quain,

Special Case: Array of Characters MIPS Arrays 8 As we've seen, the declaration: .

Special Case: Array of Characters MIPS Arrays 8 As we've seen, the declaration: . data vowels: . byte 'a', 'e', 'i', 'o', 'u' Leads to the allocation: 61 65 69 6 F 75 However, the declaration: . data vowels: . asciiz Leads to the allocation: "aeiou" 61 00 An extra byte is allocated and initialized to store 0 x 00, which acts as a marker for the end of the character sequence (i. e. , string). This allows us to write loops to process character strings without knowing the length of the string in advance. CS@VT September 2010 Computer Organization I © 2006 -10 Mc. Quain,

Example: Searching a Character String char: vowels: . data. byte. asciiz MIPS Arrays 9

Example: Searching a Character String char: vowels: . data. byte. asciiz MIPS Arrays 9 'u' "aeiou" . text main: srchlp: lb li la lb $t 0, $t 1, $s 0, $s 1, char 0 vowels ($s 0) # # load character to look for it's not found yet set pointer to vowels[0] get vowels[0] beq seq bgt addi lb b $s 1, $zero, srchdn $t 1, $s 1, $t 0 $t 1, $zero, srchdn $s 0, 1 $s 1, ($s 0) srchlp # # # check for terminator compare characters check if found no, step to next vowel load next vowel srchdn: li $v 0, 10 syscall CS@VT September 2010 Computer Organization I © 2006 -10 Mc. Quain,

Example: Setup Details. . . lb li la lb. . . $t 0, $t

Example: Setup Details. . . lb li la lb. . . $t 0, $t 1, $s 0, $s 1, char 0 vowels ($s 0) # # MIPS Arrays 10 load character to look for it's not found yet set pointer to vowels[0] get vowels[0] char vowels $t 0 00 00 00 75 $t 1 00 00 61 75 61 65 69 6 F 75 00 $s 1 CS@VT September 2010 Computer Organization I © 2006 -10 Mc. Quain,

Example: Loop Details srchlp: MIPS Arrays 11 . . . beq $s 1, $zero,

Example: Loop Details srchlp: MIPS Arrays 11 . . . beq $s 1, $zero, srchdn # string terminator is 0 x 00 seq $t 1, $s 1, $t 0 # $t 1 = 1 iff $s 1 == $t 0 bgt $t 1, $zero, srchdn # if match found, exit loop addi $s 0, 1 # step to next elem of vowels lb $s 1, ($s 0) # load next elem of vowels b srchlp srchdn: . . . CS@VT September 2010 Computer Organization I © 2006 -10 Mc. Quain,

Example: Print Array Contents list: size: MIPS Arrays 12 . data. word. . .

Example: Print Array Contents list: size: MIPS Arrays 12 . data. word. . . lw la li $t 3, size $t 1, list $t 2, 0 # get array address # set loop counter beq $t 2, $t 3, prndn # check for array end 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 10 prnlp: lw $a 0, ($t 1) li $v 0, 1 syscall # print list element la $a 0, NL li $v 0, 4 syscall # print a newline addi b # advance loop counter # advance array pointer # repeat the loop $t 2, 1 $t 1, 4 prnlp prndn: CS@VT September 2010 Computer Organization I © 2006 -10 Mc. Quain,

Example: syscall Details. . . lw $a 0, ($t 1) li $v 0, 1

Example: syscall Details. . . lw $a 0, ($t 1) li $v 0, 1 syscall. . . la $a 0, NL li $v 0, 4 syscall. . . CS@VT September 2010 MIPS Arrays 13 # syscall #1 prints and integer to stdout # takes value via register $a 0 # takes syscall # via register $v 0 # syscall #4 prints asciiz to stdout # takes address of string via $a 0 # takes syscall # via register $v 0 Computer Organization I © 2006 -10 Mc. Quain,

Example: Palindromes MIPS Arrays 14 A palindrome is a sequence of characters that reads

Example: Palindromes MIPS Arrays 14 A palindrome is a sequence of characters that reads the same from left to right as from right to left: able was i ere i saw elba anna madam It is generally permitted to adjust capitalization, spaces and punctuation: A man, a plan, a canal, Panama! Madam, I'm Adam. For the purpose of an example, we will not allow such manipulations. CS@VT September 2010 Computer Organization I © 2006 -10 Mc. Quain,

Example: Reading a String MIPS Arrays 15 We must reserve space to store the

Example: Reading a String MIPS Arrays 15 We must reserve space to store the characters: buffer: . space 1025 # 1024 maximum, plus a terminator We'll want to issue a prompt to the user to enter the string to be tested: user_prompt: . asciiz "Enter. . . of no more than 1024 characters. n" We can use a couple of system calls to get the input: main: ## Prompt the user to enter a string: la $a 0, user_prompt li $v 0, 4 syscall ## Read the string, plus a terminator, into the buffer la $a 0, buffer li $a 1, 1024 li $v 0, 8 syscall CS@VT September 2010 Computer Organization I © 2006 -10 Mc. Quain,

Example: Finding the End of the String MIPS Arrays 16 We must locate the

Example: Finding the End of the String MIPS Arrays 16 We must locate the end of the string that the user entered: la la Length. Lp: lb beqz addi b Length. Dn: addi $t 1, buffer $t 2, buffer # lower array pointer = array base # start upper pointer at beginning $t 3, ($t 2) $t 3, Length. Dn $t 2, 1 Length. Lp # # $t 2, -2 # move upper pointer back to last char grab the character at upper ptr if $t 3 == 0, we're at the terminator count the character repeat the loop QTP: why -2? CS@VT September 2010 Computer Organization I © 2006 -10 Mc. Quain,

Example: Testing the String MIPS Arrays 17 Now we'll walk the pointers toward the

Example: Testing the String MIPS Arrays 17 Now we'll walk the pointers toward the middle of the string, comparing characters as we go: Test. Lp: bge $t 1, $t 2, Yes # if lower pointer >= upper pointer, yes lb lb $t 3, ($t 1) $t 4, ($t 2) # grab the character at lower ptr # grab the character at upper pointer bne $t 3, $t 4, No # if different, it's not a palindrome addi subi $t 1, 1 $t 2, 1 # increment lower ptr # decrement upper ptr b Test. Lp # restart the loop CS@VT September 2010 Computer Organization I © 2006 -10 Mc. Quain,

Example: Reporting Results MIPS Arrays 18 Yes: la $a 0, is_palindrome_msg li $v 0,

Example: Reporting Results MIPS Arrays 18 Yes: la $a 0, is_palindrome_msg li $v 0, 4 syscall b exit # print confirmation No: la $a 0, is_not_palindrome_msg li $v 0, 4 syscall CS@VT September 2010 Computer Organization I # print denial © 2006 -10 Mc. Quain,