Microprocessors ( ) Fall 2010/2011 Lecture Notes # 15. Stack Operations. 10 top

Similar documents
Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 7. Procedures and the Stack

Assembly Language Lab # 9

Assembly Language for Intel-Based Computers, 4 th Edition

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 5: Procedures. Chapter Overview. The Book's Link Library

Lecture 15 Intel Manual, Vol. 1, Chapter 3. Fri, Mar 6, Hampden-Sydney College. The x86 Architecture. Robb T. Koether. Overview of the x86

Assembly Language. Lecture 5 Procedures

Assembly Language. Lecture 5 Procedures

The x86 Architecture

Stack, subprograms. procedures and modular programming role of stack while using procedures stack implementation (Pentium)

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 5: Procedures

Libraries and Procedures

Experiment 8 8 Subroutine Handling Instructions and Macros

Procedures and the Stack. Chapter 4 S. Dandamudi

Marking Scheme. Examination Paper. Module: Microprocessors (630313)

Libraries and Procedures

Objectives. ICT106 Fundamentals of Computer Systems Topic 8. Procedures, Calling and Exit conventions, Run-time Stack Ref: Irvine, Ch 5 & 8

Computer Architecture and Assembly Language. Practical Session 3

COE 205. Computer Organization and Assembly Language Dr. Aiman El-Maleh

Marking Scheme. Examination Paper Department of CE. Module: Microprocessors (630313)

MODE (mod) FIELD CODES. mod MEMORY MODE: 8-BIT DISPLACEMENT MEMORY MODE: 16- OR 32- BIT DISPLACEMENT REGISTER MODE

Marking Scheme. Examination Paper. Module: Microprocessors (630313)

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

UMBC. contain new IP while 4th and 5th bytes contain CS. CALL BX and CALL [BX] versions also exist. contain displacement added to IP.

6/20/2011. Introduction. Chapter Objectives Upon completion of this chapter, you will be able to:

Addressing Modes on the x86

complement) Multiply Unsigned: MUL (all operands are nonnegative) AX = BH * AL IMUL BH IMUL CX (DX,AX) = CX * AX Arithmetic MUL DWORD PTR [0x10]

Procedure. Computer Organization and Assembly Languages Yung-Yu Chuang 2007/12/24. with slides by Kip Irvine

Chapter 3: Addressing Modes

Lab 4: Basic Instructions and Addressing Modes

The Stack. Lecture 15: The Stack. The Stack. Adding Elements. What is it? What is it used for?

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 10. Advanced Procedures

Module 3 Instruction Set Architecture (ISA)

Basic Execution Environment

16.317: Microprocessor Systems Design I Fall 2015

EEM336 Microprocessors I. Data Movement Instructions

Registers. Ray Seyfarth. September 8, Bit Intel Assembly Language c 2011 Ray Seyfarth

Stack -- Memory which holds register contents. Will keep the EIP of the next address after the call

Digital Forensics Lecture 3 - Reverse Engineering

UMBC. A register, an immediate or a memory address holding the values on. Stores a symbolic name for the memory location that it represents.

UMBC. 1 (Feb. 9, 2002) seg_base + base + index. Systems Design & Programming 80x86 Assembly II CMPE 310. Base-Plus-Index addressing:

16.317: Microprocessor Systems Design I Spring 2015

We can study computer architectures by starting with the basic building blocks. Adders, decoders, multiplexors, flip-flops, registers,...

EXPERIMENT WRITE UP. LEARNING OBJECTIVES: 1. Get hands on experience with Assembly Language Programming 2. Write and debug programs in TASM/MASM

Chapter 11. Addressing Modes

SYSC3601 Microprocessor Systems. Unit 2: The Intel 8086 Architecture and Programming Model

Assembly Language Programming: Procedures. EECE416 uc. Charles Kim Howard University. Fall

EEM336 Microprocessors I. Addressing Modes

Introduction to IA-32. Jo, Heeseung

x86 assembly CS449 Fall 2017

The Microprocessor and its Architecture

INTRODUCTION TO IA-32. Jo, Heeseung

Lab 2: Introduction to Assembly Language Programming

Hardware and Software Architecture. Chapter 2

Computer Architecture and System Software Lecture 06: Assembly Language Programming

COMPUTER ENGINEERING DEPARTMENT

16.317: Microprocessor Systems Design I Fall 2013

Assembler Programming. Lecture 2

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB

Access. Young W. Lim Fri. Young W. Lim Access Fri 1 / 18

Assembly Language Each statement in an assembly language program consists of four parts or fields.

Scott M. Lewandowski CS295-2: Advanced Topics in Debugging September 21, 1998

16.317: Microprocessor Systems Design I Fall 2014

Access. Young W. Lim Sat. Young W. Lim Access Sat 1 / 19

/ 28 HLL assembly Q4: Conditional instructions / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

Computer Processors. Part 2. Components of a Processor. Execution Unit The ALU. Execution Unit. The Brains of the Box. Processors. Execution Unit (EU)

Data Movement Instructions

X86 Addressing Modes Chapter 3" Review: Instructions to Recognize"

x86 Assembly Tutorial COS 318: Fall 2017

Complex Instruction Set Computer (CISC)

Computer Systems Lecture 9

Q1: Multiple choice / 20 Q2: Protected mode memory accesses

printf("this program adds the value 10 to a given integer number.\n\n");

Assembly Language for Intel-Based Computers, 4 th Edition

A4 Sample Solution Ch3

Chapter 7 Integer Arithmetic

GLOBAL EDITION. Assembly Language. for x86 Processors SEVENTH EDITION. Kip R. Irvine

IA32 Intel 32-bit Architecture

Interfacing Compiler and Hardware. Computer Systems Architecture. Processor Types And Instruction Sets. What Instructions Should A Processor Offer?

CS241 Computer Organization Spring 2015 IA

Code segment Stack segment

COMPUTER ENGINEERING DEPARTMENT

Ex: Write a piece of code that transfers a block of 256 bytes stored at locations starting at 34000H to locations starting at 36000H. Ans.

An Introduction to x86 ASM

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

Computer Architecture and System Software Lecture 07: Assembly Language Programming

Marking Scheme. Examination Paper Department of CE. Module: Microprocessors (630313)

Credits and Disclaimers

x86 Assembly Crash Course Don Porter

CS 16: Assembly Language Programming for the IBM PC and Compatibles

The x86 Instruction Set

CS 161 Computer Security. Week of January 22, 2018: GDB and x86 assembly

Low-Level Essentials for Understanding Security Problems Aurélien Francillon

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB

Addressing Modes. Outline

Assembly Language for Intel-Based Computers, 4 th Edition. Chapter 8:Advanced Procedures

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 03, SPRING 2013

Assembly Language: Function Calls

SHEET-2 ANSWERS. [1] Rewrite Program 2-3 to transfer one word at a time instead of one byte.

Lecture (02) The Microprocessor and Its Architecture By: Dr. Ahmed ElShafee

CMSC Lecture 03. UMBC, CMSC313, Richard Chang

Transcription:

Microprocessors (0630371) Fall 2010/2011 Lecture Notes # 15 Stack Operations Objectives of the Lecture Runtime Stack PUSH Operation POP Operation Initializing the Stack PUSH and POP Instructions Stack Applications ------Using PUSH and POP Related Instructions Example: Reversing a String Runtime Stack Imagine a stack of plates... o plates are only added to the top o plates are only removed from the top o LIFO structure (last-in first-out) 10 top 9 8 7 6 5 4 3 2 1 bottom Managed by the CPU, using two registers o SS (stack segment) o ESP (stack pointer) -------( SP in Real-address mode) PUSH Operation O f f s e t 0 0 0 0 1 0 0 0 0 0 0 0 0 F F C 0 0 0 0 0 F F 8 0 0 0 0 0 F F 4 0 0 0 0 0 F F 0 0 0 0 0 0 0 0 6 E S P

A 32-bit push operation decrements the stack pointer by 4 and copies a value into the location pointed to by the stack pointer. B E F O R E A F T E R 00001000 00000006 E S P 00001000 00000006 000000A 5 E S P Same stack after pushing two more integers: O f f s e t 0 0 0 0 1 0 0 0 0 0 0 0 0 F F C 0 0 0 0 0 F F 8 0 0 0 0 0 F F 4 0 0 0 0 0 F F 0 0 0 0 0 0 0 0 6 0 0 0 0 0 0 A 5 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 2 E S P The stack grows downward. The area below ESP is always available (unless the stack has overflowed). POP Operation Copies value at stack [ESP] into a register or variable. Adds n to ESP, where n is either 2 or 4. o value of n depends on the attribute of the operand receiving the data BEFORE AFTER 00001000 00000006 00001000 00000006 000000A5 000000A5 00000001 00000001 ESP 00000002 ESP

Initializing the Stack When the stack area is initialized, load both the stack segment (SS) register and the stack pointer (SP) register. Figure shows how this value causes data to be pushed onto the top of the stack segment with a PUSH CX instruction. All segments are cyclic in nature. o the top location of a segment is contiguous with the bottom location of the segment The PUSH CX instruction, showing the cyclical nature of the stack segment. This instruction is shown just before execution, to illustrate that the stack bottom is contiguous to the top. PUSH and POP Instructions PUSH syntax: PUSH reg/m16 PUSH reg/m32 PUSH imm32 Always transfers 2 bytes of data to the stack; o 80386 and above transfer 2 or 4 bytes POP syntax: POP reg/m16 POP reg/m32 Performs the inverse operation of PUSH. POP removes data from the stack and places it in a target 16-bit register, segment register, or a 16-bit memory location. o not available as an immediate POP

Example: POP BX Stack Applications ---- Using PUSH and POP Saves procedure linking information on the stack Local Variables for Calling Procedure. Parameters Passed to Called Procedure Storing the contents of the registers including the flag register. Example1: 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 pop ecx pop esi ; restore registers Example 2: Nested Loop: Remember the nested loop we created in previous lecture? It's easy to push the outer loop counter before entering the inner 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 ; ; loop L2 ; repeat the inner loop pop ecx ; restore outer loop count loop L1 ; repeat the outer loop

Related Instructions Flags: PUSHF (push flags) instruction copies the contents of the flag register (FLAGS) to the stack. POPF instruction retrieves and loads the flag register (FLAGS) from the stack. PUSHFD and POPFD o push and pop the EFLAGS register General-purpose registers PUSHA instruction copies contents of the internal register set, except the segment registers, to the stack. o PUSHA (push all) instruction copies the registers to the stack in the following order: AX, CX, DX, BX, SP, BP, SI, and DI. PUSHAD pushes the 32-bit general-purpose registers on the stack o order: EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI POPAD pops the same registers (32-bit general-purpose registers) off the stack in reverse order POPA do the same for 16-bit registers Example: PUSHA o Requires 16 bytes of stack memory space to store all eight 16-bit registers. o After all registers are pushed, the contents of the SP register are decremented by 16. o PUSHA is very useful when the entire register set of 80286 and above must be saved. o PUSHAD instruction places 32-bit register set on the stack in 80386 - Core2. o PUSHAD requires 32 bytes of stack storage

Example: Reversing a String Use a loop with indexed addressing Push each character on the stack Start at the beginning of the string, pop the stack in reverse order, insert each character back into the string Programming Example TITLE Reversing a String (RevStr.asm) ; This program reverses a string. INCLUDE Irvine32.inc.data aname BYTE "Abraham Lincoln",0 namesize = ($ - aname) - 1.code main PROC ; Push the name onto the stack. mov ecx,namesize mov esi,0 L1: movzx eax,aname[esi] ; get character push eax ; push on stack inc esi loop L1 ; Pop the name from the stack, in reverse, ; and store in the aname array. mov ecx,namesize mov esi,0 L2: pop eax ; get character mov aname[esi],al ; store in string inc esi loop L2 ; Display the name. mov edx,offset aname call Writestring call Crlf exit main ENDP END main Q: Why must each character be put in EAX before it is pushed? Because only word (16-bit) or double word (32-bit) values can be pushed on the stack.