Download presentation
Presentation is loading. Please wait.
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 Datum #1 top empty stack Before any insertions top After first push Datum #1 After second push Datum #2 top Items are added at the top (and removed from the top)
4
Stack diagram (continued) Datum #1 Before any removals Datum #2 top Datum #1 After a pop occurs top
5
CPU’s stack grows downward main memory stack contents %esp top-of-stack 0x00000000 0xFFFFFFFF
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 –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 process_dataprint_outputobtain_input
15
Code for the ‘main’ function.section.text main:callobtain_input callprocess_data callprint_output ret.globlmain
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?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.