 This time we're going to be looking at creating recursive functions. For a quick review, a recursive function is really just any function that calls itself for some part of its processing. It does some limited amount of processing and then says, oh, I know this function over here that can finish the job for me. So it calls that. That function happens to be itself though. So it just keeps calling itself, doing a little bit less processing each time. Eventually getting to some base case and returning. But since you're going to be calling the same function again and again and again, it's going to be using the same registers over and over and over again. So it usually ends up involving some amount of stack management. The one case is if you've got some function that has tail recursion instead. Tail recursion is a special type of recursion in which our function doesn't actually do anything meaningful after it makes that function call. There isn't anything left for it to do. So it just returns to the calling function. These can often be so easy to reduce to loops that we just do that automatically though. So they don't tend to show up very often. This means that really if we have a recursive function, we're going to end up needing to manage the stack. There are a few things that you should definitely put on the stack. If you're going to use any save registers S0 through S7, you should copy them on the stack. The calling function will still expect that that data is available to it after your function is completed. So if you're going to use the save registers, just be sure to store the existing data onto the stack and then restore it when you're done. You may also want to store the value of some of your temporary registers. If you've got some data that you're going to need to be able to use after that recursive function call, then you'll want to store that into memory as well. Because if you make this function call, your code is going along. The recursive function goes off. It does something. Chances are it's going to wipe out any registers that you've used. This is potentially true of any function call you make, but it's especially true for a recursive function. Because a recursive function has been written in such a way that it uses, say registers x, y, and z, and then you make a recursive function to this function, it goes and it uses registers x, y, and z again. So it's very likely that any register you use in your recursive function is going to be overwritten by your function itself. So this means you need to be sure to put that in places where you'll be able to retrieve it after the function call returns. You'll also need to store the return address on the stack. This is because every time you use that jump in link instruction, it updates the return address. So you can think of the return address as pretty much just another temporary register, but one that you would normally not fiddle with much. You need to stick it on the stack before you make the function call, and then when you're ready to return from your function, you can pull it back off the stack. So there's a few things to keep in mind. Remember to decrement the stack pointer to allocate memory, increment the stack pointer to deallocate memory. Our stack starts from the top of memory and builds downward, so decrementing our stack pointer actually allocates memory for us. But also be sure to deallocate exactly as much memory as you allocated in the first place. If you deallocate more or less memory than you allocated in the first place, then the best thing that will happen is that you'll get back to the calling function. It will try to pull data out of memory and end up with the wrong data because the stack pointer has been offset up or down, and it's no longer pointing where the calling function thinks it is. Other scenarios would be that your stack pointer starts pointing at some place bad, things are no longer word aligned, or somebody starts trying to grab things from some region outside of memory and the machine just crashes completely. Again, because your recursive function call is likely to overwrite any registers that you've used, you'll have to be sure to restore their data after you've made that function call. So after you've made that function call, be sure you get your data back before you try working with that data again.