How does the stack work? - PowerPoint PPT Presentation

About This Presentation
Title:

How does the stack work?

Description:

... using the Pentium's push, pop, call, and ret instructions. Last ... But you could also get this same effect by using a sequence of push' and pop' instructions: ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 19
Provided by: ProfessorA
Learn more at: https://www.cs.usfca.edu
Category:
Tags: pop | stack | work

less

Transcript and Presenter's Notes

Title: How does the stack work?


1
How does the stack work?
  • On using the Pentiums 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
CPUs 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
  • Theres 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
  • (Youd better hope that its 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 ltENTERgt
  • Computer does as requested.

13
Structured programming
  • A discipline for faster program design
  • Idea break a big task into simple pieces
  • Its known as task decomposition
  • We can use a diagram to illustrate it
  • Diagram is called a Structure Chart

14
Structure Chart example
main
process_data
print_output
obtain_input
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
  • Its labeled TODO in our comments
  • Can you add a solution to this dilemma?
Write a Comment
User Comments (0)
About PowerShow.com