 For this example, we're going to be building a function that takes an array, and we'll just walk down the array and square every element in it. I've got a test suite here that will help check to make sure I've built my code correctly, and now I just need to add in the function. So in this case, the first element in my array is always going to be the length of the array. So I'm just going to go get that and stick that in a register. As my parameter will always come in in $A0, I'm just going to go get the first element out of the data structure. Now I want to go through and iterate over every element in this array. We have t0 number of elements, and I want to apply a squaring function to each of those elements individually. So I'm going to be building a for loop for this. So I will initialize a counter, and my loop is going to run until my counter is at least as large as the length of my array. So I've got some loop that says for i equals zero, i less than n. At the end of my array, I will increment my counter, and then go back to the beginning of the loop. Once the loop is done, I need to make sure that my results are in register v0, and then I can just go back to the calling function. So now all I need to do is go through and fill in all of the information about how I'm actually going to go find pieces of data in this array, pull them out of memory, manipulate them, and then put them back into memory. So I have a base address, which is in a0, and I have a counter in t1. Now since all of the elements in my array are word-sized, or integers, that means they're all four bytes large. So since MIPS is a byte-addressed architecture, I'm going to need to convert that number of integers into bytes instead. So if I multiply my index by four, I will get the number of bytes that this element is from the beginning of the array. Since the first element in my data structure is actually the length of the array, I'm going to want to skip over that and get to the actual data. So I'm just going to skip over one integer. So now t2 will have the offset from the actual beginning of my array data structure. So this will tell me exactly where my piece of data is relative to the beginning of my array. Now if I add that offset to the address of my array, that will tell me where this piece of data is in all of memory. So I'm adding my offset t2 to my base address a0, and that will give me the address of my piece of data. Now that I know where my data is, I can go fetch it out of memory. I can square it, and then I can store it back into memory at the same place. So this should be all I need to do to get my program to actually square every element in this array. So if I go over to QTSPim and I run this, it shows me that all four of my tests pass. They walked over all the elements in my array and squared each one. So I started with an array that had say four, one, three, five, seven. It uses the four as the length of the array, reads one, stores back square of one, which is one, reads three, stores back nine, reads five, stores back 27, reads seven, stores back 49. And the majority of the interesting code is happening inside this loop. I'm taking my counter, I'm multiplying it by four, I'm skipping over four bytes so that I don't grab the length field, and then I add my offset to my base address. Then I can go load that data, square it, and store it back. I can make this a little simpler by using some alternative instructions. So instead of doing the multiplication, I could have used a shift left logical instruction. I can skip the instruction to actually add four, skipping the length field, because I can stick that constant four into my load word instruction in a moment. So there's my four byte offset to skip over the length field, and I'll use it again in the store word. So I've replaced the seven lines that I had before with just five. If I run this, it still works. I've used the shift left logical instruction to replace all of the instructions involved with actual multiplication. I just used a constant four in my load word and store word instructions instead of having to actually add four to my offset. So that meant that I ended up with some code that was slightly shorter, but did the same thing.