190 likes | 211 Views
How does the stack work?. On using the Pentium’s push, pop, call, and ret instructions. Last-In, First-Out. In concept, the ‘stack’ is a dynamic data-storage structure intended for inserting and removing items in a LIFO discipline
E N D
How does the stack work? On using the Pentium’s push, pop, call, and ret instructions
Last-In, First-Out • In concept, the ‘stack’ is a dynamic data-storage structure intended for inserting and removing items in a LIFO discipline • The most recently inserted item will be the one that will be the soonest to be removed • To ‘push’ an item means to insert it, and to ‘pop’ an item means to remove it
A stack diagram Items are added at the top (and removed from the top) top Datum #2 top Datum #1 Datum #1 empty stack top Before any insertions After first push After second push
Stack diagram (continued) top Datum #2 top Datum #1 Datum #1 Before any removals After a pop occurs
CPU’s stack grows downward 0xFFFFFFFF stack contents top-of-stack %esp 0x00000000 main memory
Effect of ‘PUSH’ • Example: push %eax • This instruction will decrease the value in register %esp by 4, and then will copy the (4-byte) value from register %eax into the new location at the ‘top’ of the stack area
Effect of POP • Example: pop %edx • This instruction will copy the (4-byte) value at the ‘top’ of the stack into register %edx, and then will increase the value in %esp by 4, to effectively ‘discard’ that location from the ‘top’ of the stack area
Swapping register-values • There’s a special instruction (called ‘xchg’) that exchanges the values held in two registers • Example: xchg %ebx, %ecx • But you could also get this same effect by using a sequence of ‘push’ and ‘pop’ instructions: push %ebx push %ecx pop %ebx pop %ecx
CALL and RET • The ‘call’ instruction is used to perform an unconditional jump, while remembering the place where you jumped from • Example: call subrtn • This instruction will ‘push’ the value held in register %eip onto the stack, and then will copy the address of the label ‘subrtn’ into register %eip (this accomplishes a ‘jump’) • The ‘ret’ instruction returns from the call
Effect of RET • Example: ret • This instruction ‘pops’ the value currently at the top of the stack into register %eip • (You’d better hope that it’s the address of an executable instruction – else crash!!) • Assembly programmers need to keep the occurrences of push and pop balanced
An application ‘walk-through’ • We present a discussion of our design for an ‘interactive’ assembly language demo • Our design is based upon a very common program-structure pattern (known as the ‘Input-Process-Output’ paradigm) • We use ‘call’ and ‘ret’ instructions to follow this organizational pattern
A human-computer dialogue • Typical pattern: • Computer asks a question • Human types in a response • Simple example: • Computer says: How many dots? • Human replies: 125 <ENTER> • Computer does as requested.
Structured programming • A discipline for faster program design • Idea: break a big task into simple pieces • It’s known as “task decomposition” • We can use a diagram to illustrate it • Diagram is called a “Structure Chart”
Structure Chart example main obtain_input process_data print_output
Code for the ‘main’ function .section .text main: call obtain_input call process_data call print_output ret .globl main
Stubs • You can write empty ‘stubs’ (for testing) obtain_input: ret process_data: ret print_output: ret • Now you can ‘test’ your skeleton program (e.g. assemble, link, and execute)
Add details for each ‘stub’ • First write your final subroutine, so you can see “something” on the screen • You can use ‘dummy’ data temporarily • Get it working correctly (debugged) • Then you can focus on you next ‘stub’
In-class exercise • We left unresolved the question of how to handle the possibility that a user might try to type too many keystrokes • It’s labeled ‘TODO:’ in our comments • Can you add a ‘solution’ to this dilemma?