{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

recitation_week_4 - i in $s2 li $s3 0 max in $s3 li $s4-1...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
1. Ask the students to write code to find the location of the maximum element in an array with 10 positive elements. Try to explain the basic concepts of memory access, shift, loop and branch to them. One of the possible implementations is follows: # find the index of the maximum element in an array with 10 positive .data A: .word 34, 67, 10, 45, 90, 11, 3, 67, 19, 100 .text .globl main main: elements la $s0, A # starting addrerss of A in $s0 li $s1, 10 # number of elements in $s1 li $s2, 0
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: # i in $s2 li $s3, 0 # max in $s3 li $s4, -1 # index in $s4 Loop: sll $t1, $s2, 2 # $t1 = i * 4; add $t1, $t1, $s0 # $t1 = i * 4 + $s0 lw $t0, 0($t1) # $t0 = A[i] slt $t2, $t0, $s3 # $t2 = 1 if $t0 < $s3. $t2 = 0 if $t0 >= $s3. bne $t2, $zero, L1 # if ($t2 == 0), goto L1 ori $s3, $t0, 0 # update max value ori $s4, $s2, 0 # update max index L1: addi $s2, $s2, 1 # i = i + 1 bne $s2, $s1, Loop # if (i != $s1), go back to loop done: li $v0, 10 syscall...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online