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

Slides:



Advertisements
Similar presentations
Procedures 2 Intructions Supporting Procedures Making Use of a Stack.
Advertisements

1 Procedural Programming Paradigm Stacks and Procedures.
Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Today’s Agenda  Stacks  Queues  Priority Queues CS2336: Computer Science II.
Procedures and Stacks. Outline Stack organization PUSH and POP instructions Defining and Calling procedures.
Lecture 6 Machine Code: How the CPU is programmed.
1 CHAPTER 8 BUFFER OVERFLOW. 2 Introduction One of the more advanced attack techniques is the buffer overflow attack Buffer Overflows occurs when software.
University of Washington Last Time For loops  for loop → while loop → do-while loop → goto version  for loop → while loop → goto “jump to middle” version.
Stacks and HeapsCS-3013 A-term A Short Digression on Stacks and Heaps CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.
Digression on Stack and Heaps CS-502 (EMC) Fall A Short Digression on Stacks and Heaps CS-502, Operating Systems Fall 2009 (EMC) (Slides include.
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
S. Barua – CPSC 240 CHAPTER 10 THE STACK Stack - A LIFO (last-in first-out) storage structure. The.
Accessing parameters from the stack and calling functions.
EECC250 - Shaaban #1 Lec # 5 Winter Stacks A stack is a First In Last Out (FILO) buffer containing a number of data items usually implemented.
An “interactive” application An introduction to “structured” assembly language programming.
Room: Timbalan Pengarah Pusat Komputer Phone: Dr Masri Ayob TK 2633 Microprocessor & Interfacing Lecture 4: Introduction.
CSCE 121, Sec 200, 507, 508 Fall 2010 Prof. Jennifer L. Welch.
TK 2633 Microprocessor & Interfacing
Digression: the “Stack” 1 CS502 Spring 2006 Digression: the “Stack” Imagine the following program:– int factorial(int n){ if (n
Stacks and HeapsCS-502 Fall A Short Digression Stacks and Heaps CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Defining and Using Procedures Creating Procedures.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Stack Operations Runtime Stack PUSH Operation POP.
CS-280 Dr. Mark L. Hornick 1 Calling subroutines in assembly And using the Stack.
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Assembly, Stacks, and Registers Kevin C. Su 9/26/2011.
Chapter 10 And, Finally... The Stack. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Stacks A LIFO.
Fall 2008CS 334: Computer SecuritySlide #1 Smashing The Stack A detailed look at buffer overflows as described in Smashing the Stack for Fun and Profit.
Chapter 10 The Stack Stack: An Abstract Data Type An important abstraction that you will encounter in many applications. We will describe two uses:
CS-2710 Dr. Mark L. Hornick 1 Defining and calling procedures (subroutines) in assembly And using the Stack.
Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦
ZONG Wen Department of Computer Science and Engineering The Chinese University of Hong Kong
Introduction: Exploiting Linux. Basic Concepts Vulnerability A flaw in a system that allows an attacker to do something the designer did not intend,
Objective At the conclusion of this chapter you will be able to:
Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Chapter 7 – Subroutines These are lecture notes to accompany the book SPARC Architecture,
Subroutines, parameters and the stack Bryan Duggan.
Sahar Mosleh California State University San MarcosPage 1 Stack operations, Applications and defining procedures.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014.
1 The Stack and Procedures Chapter 5. 2 A Process in Virtual Memory  This is how a process is placed into its virtual addressable space  The code is.
October 1, 2003Serguei A. Mokhov, 1 SOEN228, Winter 2003 Revision 1.2 Date: October 25, 2003.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures Lecture 19: Procedures Procedure’s parameters (c) Pearson Education, 2002.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015.
MIPS Subroutines Subroutine Call – jal subname Saves RA in $31 and jumps to subroutine entry label subname Subroutine Return – jr $31 Loads PC with return.
Computer Organization & Assembly Language University of Sargodha, Lahore Campus Prepared by Ali Saeed.
1 Assembly Language: Function Calls Jennifer Rexford.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
Stack Operations Dr. Hadi AL Saadi.
C function call conventions and the stack
The Stack.
William Stallings Computer Organization and Architecture 8th Edition
Chapter 10 And, Finally... The Stack
Introduction to Compilers Tim Teitelbaum
Microcomputer Programming
Chapter 10 The Stack.
CSCE Fall 2013 Prof. Jennifer L. Welch.
by Richard P. Paul, 2nd edition, 2000.
Chapter 10 And, Finally... The Stack
Data structures.
Morgan Kaufmann Publishers Computer Organization and Assembly Language
CSCE Fall 2012 Prof. Jennifer L. Welch.
X86 Assembly Review.
Where is all the knowledge we lost with information? T. S. Eliot
CSC 497/583 Advanced Topics in Computer Security
Computer Organization and Assembly Language
Computer Architecture and Assembly Language
(The Stack and Procedures)
Presentation transcript:

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

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

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

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 –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 process_dataprint_outputobtain_input

Code for the ‘main’ function.section.text main:callobtain_input callprocess_data callprint_output ret.globlmain

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?