Stack Abstract idea for storing data.. Stack Abstract idea for storing data.. Stack Abstract idea for storing data.. Example – Saving and restoring Example: We want to keep the e
Trang 2Objectives
We learn about the stack data structure
We study the x86 stack implementation and instructions
We see simple examples of using the stack
Trang 3Stack
Abstract idea for storing data
Two operations are allowed: PUSH and POP
The last element pushed is the first element to be popped
LIFO - Last In First Out
Trang 4Stack
Abstract idea for storing data
Two operations are allowed: PUSH and POP
The last element pushed is the first element to be popped
LIFO - Last In First Out
Trang 5Stack
Abstract idea for storing data
Two operations are allowed: PUSH and POP
The last element pushed is the first element to be popped
LIFO - Last In First Out
PUSH
Trang 6Stack
Abstract idea for storing data
Two operations are allowed: PUSH and POP
The last element pushed is the first element to be popped
LIFO - Last In First Out
PUSH
Trang 7Stack
Abstract idea for storing data
Two operations are allowed: PUSH and POP
The last element pushed is the first element to be popped
LIFO - Last In First Out
PUSH
Trang 8Stack
Abstract idea for storing data
Two operations are allowed: PUSH and POP
The last element pushed is the first element to be popped
LIFO - Last In First Out
POP
Trang 9Stack
Abstract idea for storing data
Two operations are allowed: PUSH and POP
The last element pushed is the first element to be popped
LIFO - Last In First Out
POP
Trang 10Stack
Abstract idea for storing data
Two operations are allowed: PUSH and POP
The last element pushed is the first element to be popped
LIFO - Last In First Out
POP
Trang 11Stew Dean
Trang 12ESP
ESP is a 32 bits register (Extended Stack Pointer)
At the moment your code begins to run, esp already contains an
address of a location in memory called “the stack”
ESP and the stack are set up automatically by the operation system
There are some special instructions that deal with ESP and the stack
esp
sp
… 00 13 2a de 4f 11 00 00 ff ff …
esp
Trang 21???????? ????????
Trang 2200000003 ????????
Trang 2300000003 12345678
Trang 24Example – Exchanging values
Exchanging two values:
push eax
push ecx
pop eax
pop ecx
Trang 25Example – Exchanging values
Exchanging two values:
Trang 26Example – Exchanging values
Exchanging two values:
Trang 27Example – Exchanging values
Exchanging two values:
Trang 28Example – Exchanging values
Exchanging two values:
Trang 29Example – Exchanging values
Exchanging two values:
Trang 30Example – Exchanging values
Exchanging two values:
Trang 31Example – Saving and restoring
Example: We want to keep the ecx register unchanged.
push ecx
inc ecx
pop ecx
Trang 32Example – Saving and restoring
Example: We want to keep the ecx register unchanged.
Trang 33Example – Saving and restoring
Example: We want to keep the ecx register unchanged.
Trang 34Example – Saving and restoring
Example: We want to keep the ecx register unchanged.
Trang 35Example – Saving and restoring
Example: We want to keep the ecx register unchanged.
Trang 36Example – Saving and restoring
Example: We want to keep the ecx register unchanged.
Trang 37Example – Saving and restoring (cont.)
Keeping a few registers and then restoring them:
Note the push and pop order
push ecx push eax push ebx
; some code
pop ebx pop eax pop ecx
Trang 38Summary
A stack is an abstract idea for storing data
Only PUSH and POP
Last In First Out
x86 stack:
ESP points to the “top” of the stack
PUSH decreases esp and writes to the stack
POP reads from the stack and increases esp
Examples:
Exchanging values
Saving and restoring
esp PUSH POP