Statement Purpose:
The purpose of this Lab. is to familiarize the studentswith the concept of arrays, related operations performed on arrays, how to deal with Strings and also with procedures.This lab will explain how to create a procedure and how it will be called.The students will also know the mechanism of calling and retrieving the results.
Activity Outcomes:
Students will learn how to write MIPS programs that contain arrays, strings andprocedures. They will practice on how to create and access an array element, or a part of the array.They will perform some operations on array like search record in array, sort array, find max and minimum element in array etc. They will also practice on how to create and deal with Strings, perform some operations on it like length of strings, concatenation, converting from upper case to lower case and vice versa etc. Students will solve different problems that will help them to know how to divide the programinto different proceduresfor a given task. There are some exercises, through which they will understand the concepts learnt in this chapter.
Instructor Note:
Read the exercises below and submit your answer in the answer sheet available in the end. English will be the official language throughout the discussion
Names I.D
- .……………..……………………………….………………………………
Strings/Arrays
-In order to work with strings and arrays in MIPS you need to do the following:
- Allocate space in memory and give first address a label (string name).
- Use loops and index counters to go through arrays and strings.
- The counters increment or decrement step size depends on the data type, the array holds.
- Use lw, lh,lb and sw,sh,sb to access strings and arrays.
Example1: program to convert lowercase letters to uppercase
Algorithm:
- Read a string.
- Read current character.
- If the current character = null go to step 10
- If the character is upper go to step 6
- convert it to Upper
- store the character
- Increment counter
- Repeat steps 2 -8
- Display counter
- Exit
Example 2:Write a program that finds the length of a given string.
Algorithm:
1.Read String.
2.Read first character String[0]
3.If String[0] = null terminator t then break
4.Increment counter
5.Repeat steps 2 -4
6.Display counter
Procedures/Subroutines
To write procedures in MIPS,you should do the following:
- Label the procedure
- Use the instruction jal to call procedure.
- Preserve all saved registers before executing procedure code.
- Retrieve them after finishing
- Use jr instruction to return to first instruction after procedure call.
Procedures/Subroutines Instructions
- subroutine call: "jump and link" instruction
jal sub_label # "jump and link"
- copy program counter (return address) to register $ra (return address register)
- jump to program statement at sub_label
- subroutine return: "jump register" instruction
Jr $ra # "jump register"
- jump to return address in $ra (stored by jal instruction)
- Note:
- return address is stored in register $ra;
- if subroutine will call other subroutines, or is recursive, return address should be copied from $ra onto stack to preserve it, since jal always places return address in this register and hence will overwrite previous value
Example 2:Max Procedure
Exercise:Write a complete program to use the previous Max procedure?
Practice
1-Write a program that readsNinteger numbersof array from the user and then displays the sum and the numbers of the array.
- Use three procedures in your code( Read_Array(),Calculate_Sum() and Display_Array())
2-Write a program to sort N numbers (Using any sorting Algorithms like selection sort, Insertion sort, ….etc).