 In this example, I'm going to build a function to compute the Fibonacci sequence up to some arbitrary value that's provided as a parameter. So since this is a function, I will start off with a function name, and I will assume that my parameter comes in in register A0 as normal, and this value will tell me what the largest element in the Fibonacci sequence that I should generate is. So I've built a series of tests in the test suite, and for example, Fibonacci of 0 is 0. So I should just expect to get an array back with 0 in it for my first test. For some of the later ones, I'm getting several numbers. So Fibonacci of 3 is 2, so I generate all of the Fibonacci numbers in the sequence up to 2. The first thing I'll want to do is allocate some memory. I'm going to need to build an array, so I need to have some memory to put my data into. I need to know how much space I need for my array. So I know that if I get in, say, parameter of 0, I'm going to generate one element. If I put in a parameter of 4, then my array is going to have five elements in it. So I can see that I always have one more element in my array in the value of the parameter. So I can add one to my parameter to get the number of elements in my array. But I'm also going to want to add one more to that as well. In this case, I'd like to also keep around the size of this array so that I can compare it to other arrays and know that I'm not going to run outside the bounds of my array. So I'm going to add one more element at the very beginning of my array that I will use just to contain the length of the array. This way I don't have to pass it in as a parameter to other functions. It will just go along with the array data structure. So I need to allocate enough memory for the value of my parameter plus 2 in integers. So adding 2 to a 0 will tell me how many integers I need to allocate space for. And since MIPS is a byte address architecture, I'm going to multiply that number of integers by 4 so I can get the number of bytes that I actually want to allocate. In this case, I'm going to just directly allocate this memory on my heap. So I'm going to manipulate the global pointer. So I will start by storing my global pointer someplace safe because this is going to be the beginning of my array structure. Then I'm going to allocate t0 number of bytes on the heap. So now the space between $t9 and the new position for the global pointer is all allocated memory that hasn't been used. So that's what I'm going to be using to stick all the elements in my sequence into. Now I need to start putting elements into my array. First thing I should do before I start sticking data into my array is to put the length in. So my array starts at t9 and the size of my array is a0 plus 1. So I will add 1 to a0 and then I'm going to store that at the first element in my array. Now I'm going to start trying to put some data into my array and I will start with the couple of elements that I already know. So I know the values for 0 and 1. So if I can put those into my array I'm going to just put them in directly. Fibonacci of 0 is 0 so I will stick 0 in for the first piece of data in my array. This case I know that my array starts at t9 and t9 plus 0 is where I've put my length so the first element in my array is actually going to be at t9 plus 4 this time. And I can just stick 0 in directly. If my parameter is 0 then I should be done. I will also need to let my calling function know where I've put my array so I could use a move instruction to copy the address of the array from t9 to v0. Or since I'm not really going to be using it for anything else I can change all these references to t9 to be v0 instead. Now if I go through and I try to run my code I should see that it works for the first case where I'm just checking to see that Fibonacci of 0 is 0. We're not building up any of the more complex sequences but I've got one case that works now. For the second case I want to put 1 in as the second element in my array. So I can really just stick that in at v0 plus 8 because I know where that's at. And if I only need Fibonacci of 0 and 1 then I should be good there as well. And my test suite does like those two tests. So now I need to set up a loop that will iterate over the rest of the elements in my array and fill them in as needed. So first time I've declared a counter I'm starting with 2 because I've already gone through and solved Fibonacci of 0 and 1 directly. And I'm going to run through this loop until my counter is actually greater than my parameter. I want to be sure to get the a0th element filled in and I'm starting with 2. Example if my parameter is 2 I do want to run the loop at least once. And when I'm done I should be finished and I can just quit. So inside the loop I will certainly need to do a few things but the last thing I'll do is increment my counter by 1. So inside the loop I need to figure out what my previous two elements in the array were as well as what the next element in the array will be. And I'll have to know where all three of those elements are or where they'll go. So I have an index and I know that this will tell me where my next element should be. It's telling me about Fibonacci of n. So I can use that to find Fibonacci of n minus 1 and Fibonacci of n minus 2. So I'm first going to convert my counter to being an offset in terms of bytes. So I'm going to multiply it by 4 using the shift left logical instruction again. And this will tell me how far down my array I need to go to get to Fibonacci of n. But since I can only put in one, but since my load instructions, but since my load and store instructions aren't going to allow me to do a lot of complex computations, I'm going to have to do a lot of the arithmetic ahead of time. So I'm going to add my base address to that offset. And that will actually tell me where Fibonacci of n minus 1 is. This is because I've made room for the length of the array in the very first spot. But now I can go get the data that I need. So now I've collected Fibonacci of n minus 1, data for Fibonacci of n minus 2. So I can add those together and get the data for Fibonacci of n. Then I need to store that back into my array. That will go at my, so T1 was telling me where Fibonacci of n minus 1 is. So I will go one element, one integer farther down my array to get to Fibonacci of n. And this should be all I need to build my array. So this worked for, the last thing I need to do is to remember to tell my loop to go back to the beginning when it's done. That should be everything I need to build my array. So now when I run my test suite, it comes back and says all seven of my tests passed. So I need to remember that my integers are four bytes. So each integer is going to take up four slots in memory. So my first element was at where my array started plus zero. My second element is at the beginning of the array plus four. My third element is at the beginning of my array plus eight. And then after that I'm just moving down four bytes at a time. I'm keeping track of an index but I probably could have done this using just addresses instead. And each time I'm remembering that they're all four bytes apart. And I have to remember that I did make space for the length of my array up at the very beginning. Other than that I'm just going through the process of getting my two previous Fibonacci numbers out of memory, adding them together, and then putting them back into memory in the next slot.