Title 1

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 youve given the .space directive a suitable label. list: size: . . . .data .word .word la li print_loop: beq 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 10 $t1, list $t2, 0 $t2, $t3, print_loop_end lw $a0, ($t1) li $v0, 1 syscall addi $t2, $t2, 1 addi $t1, $t1, 4 j print_loop print_loop_end: [email protected] 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 McQuain, Feng & Ribbens 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, $t1 is set to the address of the first # char in string_space, and $t2 is set to the last one test_loop: bge $t1, $t2, is_palin # if lower pointer >= upper # pointer, yes lb lb bne $t3, ($t1) $t4, ($t2) $t3, $t4, not_palin # grab the char at lower ptr # grab the char at upper ptr # if different, it's not addu subu j $t1, $t1, 1 $t2, $t2, 1 test_loop # advance lower ptr # advance upper ptr # repeat the loop

... [email protected] October 2009 Computer Organization I 2006-09 McQuain, Feng & Ribbens Example 1: Array Traversal in C MIPS Arrays 3 // PrintList.c #include 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("%3d: ++Pos; %d\n", Pos, Array[Pos]); } } [email protected] October 2009 Computer Organization I 2006-09 McQuain, Feng & Ribbens Example 1: Array Traversal in MIPS MIPS Arrays 4 # PrintList.asm .data Sz: .word 10 Array: .word 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 NL: .asciiz "\n" .text main:

lw move move $s7, Sz $s1, $zero $s2, $zero # get size of list # set counter for # of elems printed # set offset from Array print_loop: bge $s1, $s7, print_loop_end lw li syscall la li syscall $a0, Array($s2) $v0, 1 # print next value from the list $a0, NL $v0, 4 # print a newline addi $s1, $s1, 1 addi $s2, $s2, 4 j print_loop print_loop_end: [email protected] 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 McQuain, Feng & Ribbens 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; // // // // $s3: $s0: $s1: $s2: 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; } } } } [email protected] October 2009 Computer Organization I

2006-09 McQuain, Feng & Ribbens Example 2: Analysis int main() { . . . int Stop, Curr, MIPS Arrays 6 data declarations as before $s3: upper limit for pass $s0: counter for inner loop $s1: offset of current elem Next, Temp; no need for these for (Stop = Sz - 1; Stop > 0; Stop--) { for (Curr = 0; Curr < Stop; Curr++) { $t7: current value $t8: next value Next = Curr + 1; if ( L[Curr] Temp = L[Curr] = L[Next] = } > L[Next] ) { L[Curr]; L[Next]; Temp; } } } [email protected] October 2009 We need to map arguments and variables to registers, and identify any additional registers needed.

Computer Organization I 2006-09 McQuain, Feng & Ribbens Example 2: MIPS Bubblesort Sz: List: .data .word .word MIPS Arrays 7 10 17, 5, 92, 87, 41, 30, 23, 55, 72, 36 .text main: #################################################### bubble_sort lw $s3, Sz # set outer loop limit addi $s3, $s3, -1 outer: bge li li # outer bubble-sort loop $zero, $s3, outer_end $s0, 0 $s1, 0 # set inner loop counter # set current element offset ## inner loop goes here ## addi $s3, $s3, -1 j outer outer_end: [email protected] October 2009 # decrement outer loop limit

# restart outer loop Computer Organization I 2006-09 McQuain, Feng & Ribbens Example 2: MIPS Bubblesort MIPS Arrays 8 ## see preceding slide for surrounding code inner: bge lw lw # inner bubble-sort loop $s0, $s3, inner_end $t7, List($s1) $t8, List + 4($s1) ble $t7, $t8, no_swap sw $t8, List($s1) sw $t7, List + 4($s1) no_swap: addi $s1, $s1, 4 addi $s0, $s0, 1 j inner inner_end: [email protected] October 2009 # get current element # get next element # increment inner loop counter # restart inner loop Computer Organization I 2006-09 McQuain, Feng & Ribbens

Recently Viewed Presentations

  • Presentazione di PowerPoint - unito.it

    Presentazione di PowerPoint - unito.it

    D'Alembert believed that the function f (and hence f ) must be an "analytic expression"—that is, it must be given by a formula. In 1748, Euler wrote a paper on the same problem in which he agreed completely with d'Alembert...
  • Cell Theory - Welcome to Mrs. Garcia&#x27;s Science Class

    Cell Theory - Welcome to Mrs. Garcia's Science Class

    What evidence supports each component of the cell theory? In 1663, after Robert Hooke looked at the cork cells, was there enough evidence to support the cell theory? Why or why not? How did Robert Hooke's observation turn into a...
  • Adult Nursing  I Topic: Care of clients with

    Adult Nursing I Topic: Care of clients with

    Nursing Interventions for Pneumonia 2. Activity Intolerance related to impaired respiratory function Promoting rest and conserving energy: Take rest and avoid overexertion & exacerbation of symptoms Assume comfortable position (semi-Fowler's) Change position frequently to enhance secretion clearance, ventilation & perfusion...
  • The Garland Handbook Of Latin American Music

    The Garland Handbook Of Latin American Music

    The Garland Handbook Of Latin American Music Part Three: Nations and Musical Traditions, South America, Peru Overview Cultural heritage Several pre-Colombian Amerindian ethnolinguistic groups Either gone or subdued by Inca by time of Encounter Left extensive archeological evidence of musical...
  • Managing and developing effective teams

    Managing and developing effective teams

    Objectives. To be able to identify the qualities of an effective team. To be able to set objectives for your team. To understand the stages of team development and how to manage each one effectively
  • Title Goes Here - CEOS

    Title Goes Here - CEOS

    Group code: WX8. Rate: $89 USD plus tax. Room block expires on . 15 September 2017. ... Iron Mountain Road with unique architectural features called pigtail bridges and one lane tunnels with a view of Mount Rushmore. Visit Custer State...
  • Introduction - Houston Community College

    Introduction - Houston Community College

    Introduction. If no plea agreement is reached and the defendant pleads "not guilty", the case will go to trial. The trial is a symbol of the moral authority of the . state. Every defendant has the right to a hearing...
  • Chapter 1: Introduction

    Chapter 1: Introduction

    Suffix is located at the end of a word. Substitution of suffix changes the meaning of a word. In medical terminology usually indicates a procedure, condition, disease, or part of speech. Example: -er, meaning one who -it is meaning inflammation...