Example 1 Array Traversal in C Algorithms in

  • Slides: 6
Download presentation
Example 1: Array Traversal in C Algorithms in MIPS 1 // Print. List. c

Example 1: Array Traversal in C Algorithms in MIPS 1 // 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]); } } Computer Science Dept Va Tech January 2008 Intro Computer Organization © 2006 -08 Mc. Quain & Ribbens

Example 1: Array Traversal in MIPS Algorithms in MIPS 2 # Print. List. asm.

Example 1: Array Traversal in MIPS Algorithms in MIPS 2 # 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 # stop after last elem is printed $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: Computer Science Dept Va Tech January 2008 # increment the loop counter # step to the next array elem # repeat the loop Intro Computer Organization © 2006 -08 Mc. Quain & Ribbens

Example 2: C Bubblesort Algorithms in MIPS 3 int main() { int Sz =

Example 2: C Bubblesort Algorithms in MIPS 3 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; } } Computer Science Dept Va Tech January 2008 Intro Computer Organization © 2006 -08 Mc. Quain & Ribbens

Example 2: Analysis int main() {. . . int Stop, Curr, Algorithms in MIPS

Example 2: Analysis int main() {. . . int Stop, Curr, Algorithms in MIPS 4 data declarations as before $s 3: upper limit for pass $s 0: counter for inner loop $s 1: offset of current elem Next, Temp; for (Stop = Sz - 1; Sz > 0; Sz--) { no need for these 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; } } } Computer Science Dept Va Tech January 2008 We need to map arguments and variables to registers, and identify any additional registers needed. Intro Computer Organization © 2006 -08 Mc. Quain & Ribbens

Example 2: MIPS Bubblesort Sz: List: . data. word Algorithms in MIPS 5 10

Example 2: MIPS Bubblesort Sz: List: . data. word Algorithms in MIPS 5 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: Computer Science Dept Va Tech January 2008 # decrement outer loop limit # restart outer loop Intro Computer Organization © 2006 -08 Mc. Quain & Ribbens

Example 2: MIPS Bubblesort Algorithms in MIPS 6 ## see preceding slide for surrounding

Example 2: MIPS Bubblesort Algorithms in MIPS 6 ## 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: Computer Science Dept Va Tech January 2008 # get current element # get next element # increment inner loop counter # restart inner loop Intro Computer Organization © 2006 -08 Mc. Quain & Ribbens