Whenever you have a recursive call, push all your local variables and parameters onto the stack. Reset all locals to their default values and set the arguments to the values for the new function call. Then, goto the top of the function.
Change any returns to check if the stack is empty. If it is not, then you must pop all the variables off the stack and then goto the position after the "recursive call".
Things get a little more complicated if you have a function with multiple recursive calls, because you then need to store a variable on the stack with the point of return so you know which point to goto when you return.
This type of thing would be easiest to do in a language like C# or Java, since you can make a stack an array of objects and then push whatever you need.
In C++, you would need an array of void pointers, and then you'd copy each variable into the list. If you make use of classes, things get even messier, unless you have copy constructors defined for each class, or you declare all your objects on the heap.
However, at least in C++, you could declare a struct that matched your stack frame and then allocate that as a whole, and push it as a single unit. This will simply reduce the number of heap allocations required; it won't actually help with the object copying problem. |