Arrays MIPS Arrays 1 First step is to

  • Slides: 8
Download presentation
Arrays MIPS Arrays 1 First step is to reserve sufficient space for the array.

Arrays MIPS Arrays 1 First step is to reserve sufficient space for the array. Array elements are accessed via their addresses in memory, which is convenient if you’ve given the. space directive a suitable label. list: size: . . data. word lw la li print_loop: beq 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 10 $t 3, size $t 1, list $t 2, 0 $t 2, $t 3, print_loop_end lw $a 0, ($t 1) li $v 0, 1 syscall addi $t 2, 1 addi $t 1, 4 j print_loop_end: CS@VT October 2009 # get array address # set loop counter # check for array end # print value at the array pointer # advance loop counter # advance array pointer # repeat the loop Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

Array Example MIPS Arrays 2 This is part of the palindrome example from the

Array Example MIPS Arrays 2 This is part of the palindrome example from the course website: . data string_space: . space 1024. . . # prior to the loop, $t 1 is set to the address of the first # char in string_space, and $t 2 is set to the last one test_loop: bge $t 1, $t 2, is_palin # if lower pointer >= upper # pointer, yes lb lb bne $t 3, ($t 1) $t 4, ($t 2) $t 3, $t 4, not_palin # grab the char at lower ptr # grab the char at upper ptr # if different, it's not addi j $t 1, 1 $t 2, -1 test_loop # advance lower ptr # advance upper ptr # repeat the loop . . . CS@VT October 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

Example 1: Array Traversal in C MIPS Arrays 3 // Print. List. c #include

Example 1: Array Traversal in C MIPS Arrays 3 // Print. List. c #include <stdio. h> int main() { int Sz = 10; int Array[10] = {1, 1, 2, 3, 5, 8, 13, 21, 34, 55}; int Pos = 0; while ( Pos < Sz ) { printf("%3 d: ++Pos; %dn", Pos, Array[Pos]); } } CS@VT October 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

Example 1: Array Traversal in MIPS Arrays 4 # Print. List. asm. data Sz:

Example 1: Array Traversal in MIPS Arrays 4 # Print. List. asm. data Sz: . word 10 Array: . word 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 NL: . asciiz "n". text main: lw move $s 7, Sz $s 1, $zero $s 2, $zero # get size of list # set counter for # of elems printed # set offset from Array print_loop: bge $s 1, $s 7, print_loop_end lw li syscall la li syscall $a 0, Array($s 2) $v 0, 1 # print next value from the list $a 0, NL $v 0, 4 # print a newline addi $s 1, 1 addi $s 2, 4 j print_loop_end: CS@VT October 2009 # stop after last elem is printed # increment the loop counter # step to the next array elem # repeat the loop Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

Example 2: C Bubblesort MIPS Arrays 5 int main() { int Sz = 10;

Example 2: C Bubblesort MIPS Arrays 5 int main() { int Sz = 10; int List[10] = {17, 5, 92, 87, 41, 10, 23, 55, 72, 36}; int Stop, Curr, Next, Temp; // // $s 3: $s 0: $s 1: $s 2: upper limit for pass index of current value in comparison index of successor to current value temp storage for swap for (Stop = Sz - 1; Stop > 0; Stop--) { for (Curr = 0; Curr < Stop; Curr++) { Next = Curr + 1; if ( List[Curr] > List[Next] ) { Temp = List[Curr]; List[Curr] = List[Next]; List[Next] = Temp; } } CS@VT October 2009 Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

Example 2: Analysis int main() {. . . int Stop, Curr, MIPS Arrays 6

Example 2: Analysis int main() {. . . int Stop, Curr, MIPS Arrays 6 data declarations as before $s 3: upper limit for pass $s 0: counter for inner loop $s 1: offset of current elem Next, Temp; no need for these for (Stop = Sz - 1; Stop > 0; Stop--) { for (Curr = 0; Curr < Stop; Curr++) { $t 7: current value $t 8: next value Next = Curr + 1; if ( L[Curr] Temp = L[Curr] = L[Next] = } > L[Next] ) { L[Curr]; L[Next]; Temp; } } } CS@VT October 2009 We need to map arguments and variables to registers, and identify any additional registers needed. Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

Example 2: MIPS Bubblesort Sz: List: . data. word MIPS Arrays 7 10 17,

Example 2: MIPS Bubblesort Sz: List: . data. word MIPS Arrays 7 10 17, 5, 92, 87, 41, 30, 23, 55, 72, 36 . text main: ########################## bubble_sort lw $s 3, Sz # set outer loop limit addi $s 3, -1 outer: bge li li # outer bubble-sort loop $zero, $s 3, outer_end $s 0, 0 $s 1, 0 # set inner loop counter # set current element offset ## inner loop goes here ## addi $s 3, -1 j outer_end: CS@VT October 2009 # decrement outer loop limit # restart outer loop Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens

Example 2: MIPS Bubblesort MIPS Arrays 8 ## see preceding slide for surrounding code

Example 2: MIPS Bubblesort MIPS Arrays 8 ## see preceding slide for surrounding code inner: bge lw lw # inner bubble-sort loop $s 0, $s 3, inner_end $t 7, List($s 1) $t 8, List + 4($s 1) ble $t 7, $t 8, no_swap sw $t 8, List($s 1) sw $t 7, List + 4($s 1) no_swap: addi $s 1, 4 addi $s 0, 1 j inner_end: CS@VT October 2009 # get current element # get next element # increment inner loop counter # restart inner loop Computer Organization I © 2006 -09 Mc. Quain, Feng & Ribbens