Microprocessor and Assembly Language

Slides:



Advertisements
Similar presentations
Programming 8086 – Part IV Stacks, Macros
Advertisements

Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
There are two types of addressing schemes:
Procedures and Stacks. Outline Stack organization PUSH and POP instructions Defining and Calling procedures.
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
ICS312 Set 11 Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization.
Assembly Language – Lab 5
Micro-Computer Applications: Procedures & Interrupts Dr. Eng. Amr T. Abdel-Hamid ELECT 707 Fall 2011.
Procedures and the Stack Chapter 10 S. Dandamudi.
Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing.
Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External 2. The type.
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.
Objective At the conclusion of this chapter you will be able to:
INT- interrupt program execution 1. It decrements the sp by 2 and pushes the flag registers on the stack. 2. Decrement the sp by 2 and push the content.
Strings, Procedures and Macros
Sahar Mosleh California State University San MarcosPage 1 Stack operations, Applications and defining procedures.
Writing and using procedures
Lecture 9 (The Stack and Procedures). 1 Lecture Outline Introduction The Stack The PUSH Instruction The POP Instruction Terminology of Procedures INDEC.
Copyright 2006 by Timothy J. McGuire, Ph.D. 1 MIPS Programming Model CS 333 Sam Houston State University Dr. Tim McGuire.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
Stack Operations Dr. Hadi AL Saadi.
Assembly language programming
Instruction set Architecture
Interrupts and interrupt responses
Format of Assembly language
Microprocessor and Assembly Language
Segment Definition The CPU has several segment registers:
Assembly Language programming
COURSE OUTCOMES OF MICROPROCESSOR AND PROGRAMMING
Instruksi Set Prosesor 8088
16.317: Microprocessor System Design I
Microprocessor and Assembly Language
Microprocessor and Assembly Language
University of Gujrat Department of Computer Science
Microprocessor and Assembly Language
(The Stack and Procedures)
Chapter 3 Addressing Modes
Microprocessor and Assembly Language
Defining Types of data expression Dn [name] expression Dn [name]
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.
Programming 8086 – Part IV Stacks, Macros
Microprocessor and Assembly Language
8086 Registers Module M14.2 Sections 9.2, 10.1.
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Symbolic Instruction and Addressing
Microprocessor and Assembly Language
EECE.3170 Microprocessor Systems Design I
(The Stack and Procedures)
Chapter 4 –Requirements for coding in Assembly Language
Symbolic Instruction and Addressing
Chapter 6 - Procedures and Macros
Chapter 4 –Requirements for coding in Assembly Language
Morgan Kaufmann Publishers Computer Organization and Assembly Language
EE6502/MPMC/UNIT II/STACK AND SUBROUTINE/T.THARANKUMAR
EECE.3170 Microprocessor Systems Design I
Microprocessor and Assembly Language
8051 ASSEMBLY LANGUAGE PROGRAMMING
Assembler Directives end label end of program, label is entry point
Chapter 6 –Symbolic Instruction and Addressing
(The Stack and Procedures)
Computer Organization and Assembly Language
Computer Operation 6/22/2019.
Microprocessor and Assembly Language
(The Stack and Procedures)
Procedures and Macros.
Presentation transcript:

Microprocessor and Assembly Language Lecture-8-Stack and Procedures Muhammad Hafeez Department of Computer Science GC University Lahore

Today’s Agenda What is Stack Use of Stack Procedures Calls

Stack .Stack 100H Reserve 256 bytes in stack segment Stack is Last-In-First-Out (LIFO) structure That’s why Stack always have Top of Stack (TOS) When a stack is not empty SS holds segment address and SP holds offset address Stack in 8086 family of processors is decremented stack Stack has two instruction associated with it PUSH and POP

Use of Stack Preserving temporary data and registers Supporting procedures and interrupt mechanism Passing Parameters to functions Allocating local variable

Push Instruction Add a new word to Stack Syntax PUSH Destination Destination can be 16-bit Register or Memory Location Function of Push Instruction SP is decremented by 2 A copy of destination content is moved on top of stack, destination unchanged

Push Instruction Example TOS

Example MOV AX,FFAB MOV BX,CDEA PUSH AX PUSH BX

Pop Instruction Remove a word from Top of Stack (TOS) Syntax POP Destination Destination can be 16-bit Register or Memory Location Function of POP Instruction SP is incremented by 2 A copy of TOS is moved to destination

Pop instruction Example

Pushf and Popf Instructions Pushf is used to push flag register on TOS Popf is used to pop flag register from TOS

Procedures Used to top-down design of programs Divide main problem into subproblem Subproblems are implemented as separate procedures Accept well defined input, perform certain actions and optionally return output Inputs are passed by value or passed by reference

Steps to Execute Procedure Step 1: Save Return Address Step 2: Call Procedure Step 3: Execute Procedure Step 4: Return Execution to Saved Returned Address at step 1

Procedure Declaration Name PROC type ;body of procedure RET Name ENDP Type can be NEAR or FAR

Procedures Declaration Main Procedure User defined called Procedure

Call Instruction To invoke a procedure CALL instruction is used Two types of CALL instructions Direct In-Direct Syntax CALL name ;for direction call CALL address_expression ; for indirect call Address_Expression can be register or memory location

Working of Call Instruction Push return address of calling program on Top of Stack This is the address of very next instruction after CALL instruction If CALL is a NEAR call then only IP is pushed If CALL is a FAR call both CS:IP are pushed IP is pushed, then CS is pushed

Ret Instruction Return the control to calling procedure Syntax In case of NEAR procedure, POP IP from TOS, that causes control return to calling procedure In case of FAR procedure, both CS:IP are popped.

Procedure Directives In a NEAR procedure, both calling and called procedures are in the same code segment. Called and calling procedures are in two different segments in a FAR procedure. PROC and ENDP do not generate any code; they are directives, not instructions. If the operand to a PROC directive is NEAR then all RET instructions between that PROC directive and the corresponding ENDP directive are assembled as near returns. If, on the other hand, the operand to a PROC directive is FAR, then all RET instructions within that procedure are assembled as far return. If the PROC directive is without any operand, the assembler automatically makes the procedure near or far according to the memory model selected with the .MODEL directive. Tiny, Small, Compact NEAR Medium , Large, Huge, Far

Program Modules Two Reasons: Procedures can be put in separate files for large programs Two Reasons: Procedures can be coded, assembled and tested separately When Procedures are coded and assembled separately one can use same names for the variables

Program Modules Program modules are created in separate .ASM file This .ASM file is assembled separately and .OBJ file is produces Linker takes in all .OBJ files and produces a single .EXE file

Program Modules

Extrn and Public Pseudo-Op The Use of Near and Far Pseudo-op can be seen by using Extrn and Public pseudo-ops However, a procedure can be NEAR even it is assembled separately A FAR procedure is used when it is impossible for a calling procedure to fit in a single code segment

Extrn Pseudo-Op When we use DATA or CODE (procedure) in a module (.ASM) file, that is declared to written in another module (.ASM) file. We Inform Assembler with the help of EXTRN pseudo-op Assembler will not generate undefined message Syntax is: EXTRN external_name_list EX: EXTRN PROC1:NEAR, DATA1:BYTE, PROC2:FAR EXTRN may appear anywhere but before first use of external_name_lists

Public Pseudo-Op When we want DATA or CODE (procedure) in a module (.ASM) file, to be used in another module (.ASM) file. We Inform Assembler with the help of PUBLIC pseudo-op Syntax is: PUBLIC external_name_list EX: PUBLIC PROC1, DATA1, PROC2 PUBLIC may appear at start of module

Questions ??????????????????????????