Stack Operations Dr. Hadi AL Saadi.

Slides:



Advertisements
Similar presentations
Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
Advertisements

Lecture 6 Machine Code: How the CPU is programmed.
Computer Organization And Assembly Language
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
Runtime Stack Managed by the CPU, using two registers
© 2006 Pearson Education, Upper Saddle River, NJ All Rights Reserved.Brey: The Intel Microprocessors, 7e Chapter 2 The Microprocessor and its Architecture.
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Accessing parameters from the stack and calling functions.
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
CS2422 Assembly Language and System Programming Procedures Department of Computer Science National Tsing Hua University.
CS2422 Assembly Language & System Programming October 24, 2006.
ICS312 Set 3 Pentium Registers. Intel 8086 Family of Microprocessors All of the Intel chips from the 8086 to the latest pentium, have similar architectures.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Stack Operations Runtime Stack PUSH Operation POP.
© 2006 Pearson Education, Upper Saddle River, NJ All Rights Reserved.Brey: The Intel Microprocessors, 7e Chapter 2 The Microprocessor and its Architecture.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Procedures and the Stack Chapter 10 S. Dandamudi.
Procedures and the Stack Chapter 5 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
CDP ECE Spring 2000 ECE 291 Spring 2000 Lecture 7: More on Addressing Modes, Structures, and Stack Constantine D. Polychronopoulos Professor, ECE.
Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦
Stack Operations LIFO structure (last-in,first-out) –The last value put into the stack is the first value taken out Runtime stack –A memory array that.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
Assembly Language for Intel-Based Computers, 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting.
Sahar Mosleh California State University San MarcosPage 1 Stack operations, Applications and defining procedures.
Stack and Procedures Dr Adnan Gutub aagutub ‘at’ uqu.edu.sa
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
Computer Organization and Assembly Languages Yung-Yu Chuang 2006/11/13
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.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures Lecture 18 Linking to External Library The Book’s Link Library Stack Operations.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015.
MOV Instruction MOV destination,source  MOV AX,BX  MOV SUM,EAX  MOV EDX,ARRAY[EBX][ESI]  MOV CL,5  MOV DL,[BX]
CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.
Assembly Language Data Movement Instructions. MOV Instruction Move source operand to destination mov destination, source The source and destination are.
CSC 221 Computer Organization and Assembly Language Lecture 15: STACK Related Instructions.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
Practical Session 3.
Computer Architecture and Assembly Language
Microprocessor and Assembly Language
Chapter 4 Data Movement Instructions
Introduction to Compilers Tim Teitelbaum
Basic Microprocessor Architecture
Assembly IA-32.
(The Stack and Procedures)
Basic of Computer Organization
Symbolic Instruction and Addressing
CS 301 Fall 2002 Control Structures
Data Addressing Modes • MOV AX,BX; This instruction transfers the word contents of the source-register(BX) into the destination register(AX). • The source.
Stack and Subroutines Module M17.1 Section 11.2.
8086 Registers Module M14.2 Sections 9.2, 10.1.
Stack Frames and Advanced Procedures
Libraries and Procedures
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Symbolic Instruction and Addressing
Practical Session 4.
(The Stack and Procedures)
Assembly Language for Intel-Based Computers, 4th Edition
The Microprocessor & Its Architecture
Symbolic Instruction and Addressing
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Computer Architecture CST 250
X86 Assembly Review.
Chapter 6 –Symbolic Instruction and Addressing
(The Stack and Procedures)
CSC 497/583 Advanced Topics in Computer Security
Computer Organization and Assembly Language
Computer Architecture and System Programming Laboratory
Presentation transcript:

Stack Operations Dr. Hadi AL Saadi

What is a Stack? Stack is a Last-In-First-Out (LIFO) data structure Analogous to a stack of plates in a cafeteria Plate on Top of Stack is directly accessible Two basic stack operations Push: inserts a new element on top of the stack Pop: deletes top element from the stack View the stack as a linear array of elements Insertion and deletion is restricted to one end of array Stack has a maximum capacity When stack is full, no element can be pushed When stack is empty, no element can be popped

Runtime Stack Runtime stack: array of consecutive memory locations Managed by the processor using two registers Stack Segment register SS Not modified in protected mode, SS points to segment descriptor Stack Pointer register ESP For 16-bit real-address mode programs, SP register is used ESP register points to the top of stack Always points to last data item placed on the stack Only words and doublewords can be pushed and popped But not single bytes Stack grows downward toward lower memory addresses

Runtime Stack Imagine a stack of plates . . . plates are only added to the top plates are only removed from the top LIFO structure

Runtime Stack Allocation .STACK directive specifies a runtime stack Operating system allocates memory for the stack Runtime stack is initially empty The stack size can change dynamically at runtime Stack pointer ESP ESP is initialized by the operating system Typical initial value of ESP = 0012FFC4h The stack grows downwards The memory below ESP is free ESP is decremented to allocate stack memory ESP = 0012FFC4 ? . low address high address

Stack Instructions Two basic stack instructions: push source pop destination Source can be a word (16 bits) or doubleword (32 bits) General-purpose register Segment register: CS, DS, SS, ES, FS, GS Memory operand, memory-to-stack transfer is allowed Immediate value Destination can be also a word or doubleword Segment register, except that pop CS is NOT allowed Memory, stack-to-memory transfer is allowed

Push Instruction Push source32 (r/m32 or imm32) Push source16 (r/m16) ESP is first decremented by 4 ESP = ESP – 4 (stack grows by 4 bytes) 32-bit source is then copied onto the stack at the new ESP [ESP] = source32 Push source16 (r/m16) ESP is first decremented by 2 ESP = ESP – 2 (stack grows by 2 bytes) 16-bit source is then copied on top of stack at the new ESP [ESP] = source16 Operating system puts a limit on the stack capacity Push can cause a Stack Overflow (stack cannot grow)

Examples on the Push Instruction Suppose we execute: PUSH EAX ; EAX = 125C80FFh PUSH EBX ; EBX = 2Eh PUSH ECX ; ECX = 9B61Dh The stack grows downwards The area below ESP is free ESP 0012FFC4 BEFORE 0012FFC0 0012FFBC 0012FFB8 0012FFB4 ESP 0012FFC4 0012FFC0 0012FFBC 0012FFB8 0012FFB4 125C80FF AFTER 0000002E 0009B61D

Pop Instruction Pop dest32 (r/m32) Pop dest16 (r/m16) 32-bit doubleword at ESP is first copied into dest32 dest32 = [ESP] ESP is then incremented by 4 ESP = ESP + 4 (stack shrinks by 4 bytes) Pop dest16 (r/m16) 16-bit word at ESP is first copied into dest16 dest16 = [ESP] ESP is then incremented by 2 ESP = ESP + 2 (stack shrinks by 2 bytes) Popping from an empty stack causes a stack underflow

Examples on the Pop Instruction Suppose we execute: POP EAX POP EBX The stack shrinks upwards The area at & above ESP is allocated ESP 0012FFC4 0012FFC0 0012FFBC 0012FFB8 0012FFB4 125C80FF BEFORE 0000002E 0009B61D 0012FFC4 0012FFC0 0012FFBC 0012FFB8 0012FFB4 125C80FF AFTER 0000002E 0009B61D ESP

Uses of the Runtime Stack Runtime Stack can be utilized for Temporary storage of data and registers Transfer of program control in procedures and interrupts Parameter passing during a procedure call Allocating local variables used inside procedures Stack can be used as temporary storage of data Example: exchanging two variables in a data segment push var1 ; var1 is pushed push var2 ; var2 is pushed pop var1 ; var1 = var2 on stack pop var2 ; var2 = var1 on stack

Temporary Storage of Registers Stack is often used to free a set of registers push EBX ; save EBX push ECX ; save ECX . . . ; EBX and ECX can now be modified pop ECX ; restore ECX first, then pop EBX ; restore EBX Example on moving DX:AX into EBX push DX ; push most significant word first push AX ; then push least significant word pop EBX ; EBX = DX:AX

Using PUSH and POP Save and restore registers when they contain important values. PUSH and POP instructions occur in the opposite order. push esi ; push registers push ecx push ebx mov esi,OFFSET dwordVal ; display some memory mov ecx,LENGTHOF dwordVal mov ebx,TYPE dwordVal call DumpMem pop ebx ; restore registers pop ecx pop esi

L1: . . . ; begin the outer loop L2: . . . ; begin the inner loop Example: Nested Loop When writing a nested loop, push the outer loop counter ECX before entering the inner loop, and restore ECX after exiting the inner loop and before repeating the outer loop mov ecx, 100 ; set outer loop count L1: . . . ; begin the outer loop push ecx ; save outer loop count mov ecx, 20 ; set inner loop count L2: . . . ; begin the inner loop . . . ; inner loop loop L2 ; repeat the inner loop . . . ; outer loop pop ecx ; restore outer loop count loop L1 ; repeat the outer loop

Example: Write a program to prompt the user to enter a single line ( Character by Character ) . The program should print out the entered line in reverse order ( Using Stack ) TITLE PS: RVERSE INPUT .MODEL SMALL .STACK 100h .CODE MAIN PROC ;display prompt MOV AH,2 MOV DL,”?” INT 21h ;initialize character ;count XOR CX,CX ;read a character MOV AH,1 ; while character is not ; ;carriage return do WHILE_: CMP AL,0Dh JE END_WHILE ; save character on the stack ;and increment count PUSH AX INC CX ; read a character INT 21h JMP WHILE_ END_WHILE MOV AH,2 MOV DL,0DH ;CR

MOV DL,0AH ; Line feed INT 21H JCXZ EXIT1 ; exit if no character read ; for count times do TOP: ; pop a character from the stack POP DX ; get A character from stack ; display it INT 21h LOOP TOP ; end for EXIT1: MOV AH,4CH INT 21 h MAIN ENDP END MAIN

Push/Pop All Registers pushad Pushes all the 32-bit general-purpose registers EAX, ECX, EDX, EBX, ESP, EBP, ESI, and EDI in this order Initial ESP value (before pushad) is pushed ESP = ESP – 32 pusha Same as pushad but pushes all 16-bit registers AX through DI ESP = ESP – 16 popad Pops into registers EDI through EAX in reverse order of pushad ESP is not read from stack. It is computed as: ESP = ESP + 32 popa Same as popad but pops into 16-bit registers. ESP = ESP + 16

Stack Instructions on Flags Special Stack instructions for pushing and popping flags pushfd Push the 32-bit EFLAGS popfd Pop the 32-bit EFLAGS No operands are required Useful for saving and restoring the flags For 16-bit programs use pushf and popf Push and Pop the 16-bit FLAG register