You are on page 1of 2

I have been a member of this site for over three months now and the number one r equest

from new members is overwhelmingly, "Can you help me understand how to us e the stack?" And so putting aside the many wonderful resource which may be foun d online, I will do my best to put the concepts in the simplest terms as they re late to RoboZZle. Stacks are not just part of this game. They are an integral part of computing an d can be found at the core of an innumerable variety of processes. Stacks are ev en hardwired into microprocessors themselves. If you are serious about programmi ng it behooves you to learn to manipulate stacks. Functions Before you can understand stacks you must have a clear understanding of function s. A function is a series of instruction that can be called by a label as a sing le instruction. For example: You are looking at a puzzle and notice that all or most of the distances you need to move are devisable by two. You look down and s ee that F3 has a length of two so you fill it with, "Forward, Forward" From this point on you may place the F3 instruction elsewhere in your code and even thoug h it only takes one instruction space it will execute two forward movements. Mor e importantly like any other instruction you can use it in more than one locatio n within your code. Now this raises an interesting question. How does the program flow remember whic h F3 instruction called it? It must remember because it knows where to return. I f you call F3 from position F1:0 it will return to F1:1. If you call you call it from F2:8 it will return to F2:9. The short answer is it remembers by storing i t on the stack. This means the stack is memory. Nesting A second concept you must understand before the power of the stack can be revele d is nesting. Nesting is when a function has been called and then proceeds to ca ll a function. For example: Program F2: F3, Right Turn, F3. And F1: F2, Paint Re d. When the code runs F1 call F2. Now this next part is very important. Every fu nction call is stored on the stack. So when the F2 instruction executes it not o nly passes control to the F2 function but also places the return address F1:1 on the stack. This is known as pushing the stack. And look what happens next. F2 calls F3. Thi s passes control to F3 and pushes F2:1 to the stack. So what happened to the F1: 1 that we pushed earlier? Let's fallow the program flow a bit farther. F3 just m oves forward twice. It has no more code so it returns to the place it was called . It goes to F2:1 and also removes that address from the stack. This is known as popping the stack. Some languages require a return instruction. RoboZZle has what is know as implied returns. I think of it as an invisible ins truction just to the right of the last real instruction of every function. Anywa y, on with the example. F2:1 executes the right turn. Then another push/pop in a nd out of F3. Now it is time for F2 to return which it does without fail to F1:1 and the red paint. This not only proves the stack is memory. It proves that it can hold more than o ne thing. The trick is that you can only get at one piece of information at a ti me. The addresses are nested one within the next. This is known as "first in las t out" or FILO and is a fundamental behavior of all stacks. The term nesting is normally used to refer to a function call within a function but because every fu nction call pushes and every function return pops, nesting and FILO are essentia lly synonymous. At this point it should be clear why program stacks are called s tacks.

Looping If you have understood everything up to now then you are ready to learn to count cells in RoboZZle. Remember that every function call pushes the stack. So then what happens when a function calls itself? This is called looping. Let's make a loop. F5: Forward, F5 if blue, Forward. When F5 is called it first moves. Then i f it is on blue it calls itself and pushes F5:2. It then moves again. If it once again lands on blue it pushes a second F5:2. It will continue pushing this same address to the stack until it lands on something other than blue. Let's say it pushes three times then lands on green. It will now skip the F5 ins truction and execute the second forward for the first time. This is just normal conditional fall through so we get two extra moves that don't involve the stack. Next the function returns and pops the last address pushed. It executes the sec ond forward a second time and returns again. It will continue in this manner unt il it has popped all three loops. I sometimes refer to this as recursive fall th rough. It will thus have moved a distance of eight. That's push 3, extra 2, pop 3. The distance covered by F5 will always be twice the push count plus two. The next ad dress popped will be a return to whatever point called the F5 function in the fi rst place. If you pop an empty stack execution terminates. If you create an unco nditional infinite loop then all alloted stack memory will eventually be filled. Have you ever seen the "Error! Stack Overflow." pop up? The 1000 step limit in RoboZZle ether prevents this error or is such an error depending on your perspec tive. Complex Stacking The excessive use of conditional function calls can lead to stacks filled with s eemingly chaotic return addresses. This in turn can lead to migraine headaches. You have been warned! Conditional Returns It has been suggested in the requested features forum that a "return instruction " be added to the game. The main purpose for this would be conditional returns. This would allow remaining instructions within a given function to be discarded based on the current cell color. While I am for adding this instruction I would not want it to be available to puzzles created prior to its existence as it woul d cause both solution lengths and difficulty ratings to fall. This is mostly con jecture but I don't think any major changes to RoboZZle are being planned anyway . Well, That's about it. I hope this helps one or two of you out there.