Question: Assignment: Recursion in MIPS Assembly Programming In this assignment, you will be designing an assembly program running on MIPS hardware using the QtSpim MIPS simulator
Assignment: Recursion in MIPS Assembly Programming In this assignment, you will be designing an assembly program running on MIPS hardware using the QtSpim MIPS simulator
For this assignment, you will learn Recursion, Call by Value, Call by Reference, Memory Systems along with activation record concept, parameter passing mechanisms, and recursive functions in terms of a program that computer a sum of the first n integers. A recursion is a function (or subroutine in assembly programing languages) which calls itself in its function body. The notion of recursion is important in Computer Science as a lot of definition is easier to be described by recursion. For example, the Fibonacci number is defined as the sum of the previous two Fibonacci numbers, i.e., Fibonacci(n) = Fibonacci(n-1)+Fibonacci(n-2). So as long as we define the initial values Fibonacci(0)=0 and Fibonacci(1)=1, we will compute Fibonacci(2)=1+0, Fibonacci(3)=1+1, etc. Similarly, we may define sum(n) = n+sum(n-1) and the initial value sum(0)=0. Note that we have to have initial values defined for a recursion relation. Otherwise, we may not compute all the values. These initial values in programing serve a return condition as well. Therefore, in order to compute sum(10), for example, we will need to computer sum(9). Again, in order to computer sum(9), we have to computer sum(8). This process continues until we reach sum(0), which is used to compute sum(1). From then on, the results will be popped back to answer the sum in the upper level. The sum function we are working on is a recursive function because it calls itself in its body with parameter n-1. Basically, a recursive function includes two parts: 1) return if initial condition is met, and 2) do computation with calls to the function itself. The pseudo code for the sum function is depicted below. function sum(n) { // determine when to return if (n<1) return 0; else // do computation; call itself return n*sum(n-1); } Each function call will yield a call instance, which is unique in the system, and requires a data structure, called activation record, that contains return address, parameters, return value, and others. The return address will be the address of the next instruction following the call statement. The parameters will be values passed to the function. The return value will be the result to be 2 returned to the caller. There are two basic types of parameter passing mechanisms in most systems: call by value and call by reference. Call by value will pass the actual values to the function for computation whereas call by reference will pass the address (reference) for the value stored. Typically, we use call by reference for passing an array. In this assignment, we will use the following activation record for the recursive sum function as follows: Table 1 Activation Record for the Sum(n) Function Top of the stack Parameter n This is a caller saved variable using call by value to be passed to the sum function. This slot will also be used to store the result returning to the caller. Return Address The system will save the return address after the instruction JAL is executed. The address is the next instruction following the JAL instruction. Bottom of the stack Local variable ($S0) This is callee saved variable used locally for the sum function. So the callee should pop it up before returning back to caller. 1. Make a new folder for all your assembly program files. Create a file using Notepad or any editor available in your computer and name it Assignment-sum-
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
