Today we have talked about “the Stack” in class. I have tried to explain to you how the program stack works when we call a sub-routine: The instruction “call” saves the return address on the top of the stack; later, the last instruction of the sub-routine — “ret” — pops the return address from the stack and in this way lets the execution of the program continue right after the instruction “call”.

In order to study the instructions “call” and “ret”, we had a look at the assembly-language file “main00.s”:

.text .globl main main: call sub after_call: ret sub: ret

This short program consists of 3 instructions only. The first instruction of “main” calls the sub-routine starting at label “sub”. This sub-routine consists of only 1 instruction, i.e. “ret”. Thus, it immediately returns to the calling program “main”. The second instruction of “main” is again a “ret”. With this instruction, “main” returns to whoever called “main” in the beginning.

You can assemble and link the file “main00.s” by using GCC:

$gcc -S main00.s

GCC creates the file “a.out”. You can run the executable “a.out”

in this way:

$./a.out

The program does not do much. It just executes the three instructions. That’s it. But that’s a lot. In case you want to see the details, you might want to use the GNU debugger “gdb” and check them out.

Below are a few puzzles for the students who are eager to fully understand the program stack. I ask you to try them out and see what happens. Do you understand, what happens? Any comments? Do you have any other variations on this topic?

Feel free to interpret these puzzles as exam questions.

In case you compile these assembly-language files with OS X, you need to use “_main” instead of “main”.

------------------------ file "m1.s": ------------------------ .text .globl main main: call sub after_call: ret ret sub: ret ------------------------ file "m2.s": ------------------------ .text .globl main main: call sub ret after_call: ret sub: ret ------------------------ file "m3.s": ------------------------ .text .globl main main: call sub after_call: ret sub: ret ret ------------------------ file "m4.s": ------------------------ .text .globl main main: call sub after_call: ret sub: pushl %eax ret ------------------------ file "m5.s": ------------------------ .text .globl main main: call sub after_call: ret sub: pushl %eax addl $4, %esp ret ------------------------ file "m6.s": ------------------------ .text .globl main main: call sub after_call: ret sub: pushl %eax addl $4, %esp ------------------------ file "m7.s": ------------------------ .text .globl main main: call sub after_call: ret sub: subl $5, (%esp) ret ------------------------ file "m8.s": ------------------------ .text .globl main main: call sub pushl %ebx after_call: ret sub: subl $5, (%esp) ret ------------------------ file "m9.s": ------------------------ .text .globl main main: call sub pushl %ebx after_call: ret sub: movl 4(%esp), %eax movl %eax, (%esp) ret ------------------------ file "m10.s": ------------------------ .text .globl main main: call sub pushl %ebx after_call: ret sub: movl 4(%esp), %eax pushl %eax movl %eax, (%esp) ret

Forget Mindcraft. The real adventure is RNO.

Other variations: https://www.youtube.com/watch?v=CTobovkjkvg&nohtml5=False