 In this example, I'm going to be writing a function to compute factorials. I'm going to be using my test suite that I generated earlier to test this function so that I know that it actually works. The first thing I need to do is come up with the name for my function. Since I already have a test suite where I've been calling this function, I know I've named the function fact, so I'm going to start with a label for fact. I type in fact, add a colon, and that will tell QTSPim where to start this function at. Every time it sees a jump in link to fact, it will know to go to this line of code and start running. So my factorial function expects a couple of things. It expects to get its parameter an integer n in A0 because all of my arguments come in in the A registers. It will then do its computation, it will put its results in the V register, in V0 specifically, and then it'll return at the end. So for factorial, one thing I know is that factorial of 0 is 1. So I can test for that one really quickly and just return if the parameter is 0. So if my parameter is 0, then I will go to this return 0 label, I will put 0, I will put 1 in for my result, then I will go back to the calling function. I use the jump register return address instruction to tell the machine that I want to go back to the calling function. When it runs one of these jump in link instructions, it does two things. Primarily, it goes to whatever label I've mentioned, so fact matches up with fact, but it also sets the return address at the same time. This gives me a way to say, well, when I'm done with my fact function, I want to go back and keep running. So the return address will know to go back and run this move instruction next. Since this is the only thing I need to do if I'm just returning 1 as my result, then I don't have to do anything more for that case. Now I can look at an iterative solution for calculating any other factorial number. So I'm going to start by putting my result in v0, because I know my results should end up in v0 anyway. I might as well just start by putting them there now. Since I'm going to be doing a bunch of multiplication, I'm putting a 1 there, and I'll just calculate the product iteratively. Next I need a counter to keep track of what number I'm up to so that I don't go above n. So I will put a 1 in t0, and next I'm going to set up my loop. So the test for my loop should be that once my counter has gotten to be bigger than n, then I want to quit. So once my counter is greater than my parameter, then I will go to an end fact loop label. What I'm going to do inside that loop is multiply my result by my current parameter, and then I need to increment my counter. This will allow me to count from 1 up to however many I need, and keep track of a running product. Once I've computed that product, then I exit my fact loop, and I just need to return the results. My results are already in v0 because I've been putting them there as we go, so I won't need to copy them there so that they'll be available for the calling function. Instead, I just need to go back to that calling function. So I will put in another return statement. That should be all I need for my factorial function, so I'm going to try running it now. So it did well for tests 0 and 1, but it didn't do so well after that. It looks like I missed the instruction to repeat my loop at the end. So it went through the loop once, it multiplied 1 by 1, and quit. So I will add that line of code back in, now when I run it, all of my tests pass. So here, what I really needed for my factorial function was the name of the function as a label. I know I need to get my parameters in a0 through a3. In this case, I only have one parameter, so it's an a0. I know I will put my results in either v0 or v1, since I only have one result, I'm putting it in v0. And then the only other thing I need is to be able to go back to the calling function when I'm done, which I can do using the jump register return address instruction. Everything else in this function is really about calculating the results of my function. All the other code just serves to calculate the factorial. The name, the return, and the expectations about parameters and results are the only things that really make this a function.