Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patternson and Hennessy Text.

Similar presentations


Presentation on theme: "Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patternson and Hennessy Text."— Presentation transcript:

1 Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patternson and Hennessy Text

2 Review MIPS Loops review with problems

3 Loops Assume we wish to determine if two strings are of the same length using MIPS, the solution requires comparison of the lengths of two strings. Before this comparison can be performed, the string lengths must be computed. Let's look at an example in a high level language, first though. Recall that we cannot compute the length of a string in MIPS simply by accessing a variable. We need to count through every character until we reach the null character. An example of counting through each character in a high level language follows below: for(int i = 0; str[i] != 0; i++) length++; To perform this loop in MIPS, the code follows:

4 MIPS version # Assume the memory address of the string is $t1 and the # counter is $t2. # Perform loop operations. lb $t3, 0($t1) #load the first byte into a register whileloop: beq $t0, $zero, endwhile lb $t3, 0($t1) #load the next byte addi $t1, $t1, 1 #increment the counter and address addi $t2, $t2, 1 b whileloop endwhile:

5 More Examples We will also look at how to print every other character in a string assuming we are given the length and the address of the string. To perform this operation in C, we may use a for loop as follows: for(int i = 0; i < length; i += 2) printf("%c", str[i]); Let's assume our counter uses $t0 and the address of the string is $t1. Assume these values are valid. Then our loop looks as follows:

6 MIPS version for: bge $t0, $t2, endfor #assume the size of the string is in $t2 lb $t3, 0($t1) move $a0, $t3 #print the character from the even index li $v0, 11 syscall addi $t0, $t0, 2 #add 2 to the address and counter addi $t1, $t1, 2 b for endfor: Notice that we add two to both our address and counter to step through the loop and step through the addresses.


Download ppt "Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patternson and Hennessy Text."

Similar presentations


Ads by Google