1 / 18

How does the stack work?

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

Download Presentation

How does the stack work?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. How does the stack work? On using the Pentium’s push, pop, call, and ret instructions

  2. 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

  3. 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

  4. Stack diagram (continued) top Datum #2 top Datum #1 Datum #1 Before any removals After a pop occurs

  5. CPU’s stack grows downward 0xFFFFFFFF stack contents top-of-stack %esp 0x00000000 main memory

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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.

  13. 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”

  14. Structure Chart example main obtain_input process_data print_output

  15. Code for the ‘main’ function .section .text main: call obtain_input call process_data call print_output ret .globl main

  16. 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)

  17. 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’

  18. 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?

More Related