Procedures and Macros.

Slides:



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

1 Procedural Programming Paradigm Stacks and Procedures.
10-1 ECE 424 Design of Microprocessor-Based Systems Haibo Wang ECE Department Southern Illinois University Carbondale, IL Subroutine and Interrupt.
Procedures and Stacks. Outline Stack organization PUSH and POP instructions Defining and Calling procedures.
Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.
Assembly Language for Intel-Based Computers Chapter 8: Advanced Procedures Kip R. Irvine.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
INVOKE Directive The INVOKE directive is a powerful replacement for Intel’s CALL instruction that lets you pass multiple arguments Syntax: INVOKE procedureName.
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.
8.4 Instruction Execution Times TOBIN PROC FAR SUB AX,AX MOV DX,AX MOV CX,4 NEXTD: PUSH CX SUB BP,BP MOV CX,4 GETNUM: RCL BX,1 RCL BP,1 LOOP GETNUM.
Factorial of a number data segment x1 db 4 fact dw ? data ends
Micro-Computer Applications: Procedures & Interrupts Dr. Eng. Amr T. Abdel-Hamid ELECT 707 Fall 2011.
Procedures and the Stack Chapter 5 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
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.
Computer Architecture Lecture 13 – part 2 by Engineer A. Lecturer Aymen Hasan AlAwady 7/4/2014 University of Kufa - Information Technology Research and.
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.
Today’s topics Parameter passing on the system stack Parameter passing on the system stack Register indirect and base-indexed addressing modes Register.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 8: Advanced Procedures (c) Pearson Education, All rights reserved. You may.
Objective At the conclusion of this chapter you will be able to:
Strings, Procedures and Macros
Lecture 7 A closer look at procedures Dr. Dimitrios S. Nikolopoulos CSL/UIUC.
Writing and using procedures
ECE 353 Introduction to Microprocessor Systems Michael G. Morrow, P.E. Week 6.
Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.
EEL 3801 Part IV The Assembler. OFFSET Operator Returns address of variable used as operand. Actually, it represents the offset from the beginning of.
Lecture 9 (The Stack and Procedures). 1 Lecture Outline Introduction The Stack The PUSH Instruction The POP Instruction Terminology of Procedures INDEC.
ECE 353 Introduction to Microprocessor Systems Michael J. Schulte Week 6.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
ICS312 Set 12 Subroutines: Passing Arguments Using the Stack.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
BITS Pilani Pilani Campus Pawan Sharma Lecture /12/ EEE /INSTR/CS F241 ES C263 Microprocessor Programming and Interfacing.
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
Procedures Dr. Hadi Al Saadi Large problems can be divided into smaller tasks to make them more manageable A procedure is the ASM equivalent of a Java.
Chapter 14 Functions.
Assembly language programming
Format of Assembly language
Microprocessor and Assembly Language
COURSE OUTCOMES OF Microprocessor and programming
Additional Assembly Programming Concepts
Instruksi Set Prosesor 8088
Subroutines and the Stack
Microprocessor and Assembly Language
Microprocessor and Assembly Language
Machine control instruction
(The Stack and Procedures)
Chapter 3 Addressing Modes
CS 301 Fall 2002 Control Structures
Stack and Subroutines Module M17.1 Section 11.2.
For Example: User level quicksort program Three address code.
Programming 8086 – Part IV Stacks, Macros
8086 Registers Module M14.2 Sections 9.2, 10.1.
Microprocessor and Assembly Language
Subroutines and the Stack
(The Stack and Procedures)
Microprocessor Lab CSL1543 0:0:2
Symbolic Instruction and Addressing
Chapter 6 - Procedures and Macros
Flow Control Instructions
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Miscellaneous Topics.
X86 Assembly Review.
Subroutines and the Stack
(The Stack and Procedures)
By Nasser Halasa Assembly Language.
The JUMP GROUP Unconditional Jump (JMP).
Computer Organization and Assembly Language
Procedures & Macros Introduction Syntax Difference.
(The Stack and Procedures)
Presentation transcript:

Procedures and Macros

PROCEDURES AND MACROS When we need to use a group of instructions several times throughout a program there are two ways we can avoid having to write the group of instructions each time we want to use them. 1 One way is to write the group of instructions as a separate procedure. Another way we can use macros.

Procedures The procedure is a group of instructions stored as a separate program in the memory and it is called from the main program whenever required. Procedures are also called as ‘Sub-programs’ or ‘Sub-routines’. A Procedure is a separate group of instructions apart from the main program. Procedures are accessed by CALL Instruction during the execution of the program. A RET Instruction at the end of the procedure returns execution to the main program. So, Procedures represent Top-Down Approach.

Procedure Block Syntax <procedure name> PROC (FAR | NEAR) ………………..Instructions…………… RET <procedure name> ENDP

Near Procedure Example CODE_SEG SEGMENT : : CALL A A PROC NEAR RET A ENDP CODE_SEG ENDS

Far Procedure Example CODE_SEG SEGMENT : : CALL B CODE_SEG ENDS : : CALL B CODE_SEG ENDS CODE_SEG1 SEGMENT B PROC FAR RET B ENDP CODE_SEG1 ENDS

CALL Instruction CALL Instruction is used to call a procedure from the main program. The CALL instructions take the same forms as the JMP instructions. There are two types of CALL Instructions. i) Near CALL :- Procedure in same Code Segment Pushes the 16-bit offset of the next instruction following the call onto the stack. Copies the 16-bit effective address of procedure into the IP register. Execution continues at the first instruction of the procedure.

Passing Parameters to and from Procedures Parameters represent the Address or Data passed back and forth between main program and Procedure. The types for passing parameters are Registers. Memory Locations. Pointers accessed by Registers. Stack.

Re-entrant Procedures A Procedure which can be Interrupted, Used and Re-entered is known as Re-entrant Procedure. Push the values used in procedure into stack. Passing parameters through registers and stack are only allowed in re-entrant procedures.

Reentrant Procedure Diagram

Recursive Procedures A Recursive procedure calls the procedure itself. Recursive procedures are used in complex data structures like TREES. Ex: Factorial procedure

Recursive Procedure A recursive procedure is procedure which calls itself.

Recursive Procedure Flow diagram Pseudo code

Factorial program ALP for Factorial of number using recursive procedures CODE SEGMENT ASSUME CS:CODE START: MOV AX,7 CALL FACT MOV AH,4CH INT 21H FACT PROC NEAR MOV BX,AX DEC BX BACK: MUL BX JNZ BACK RET ENDP CODE ENDS END START

Macros Macro is a group of Instructions, which when CALLed, inserts those group of Instructions in the place of CALL. Macro should have less no. of instructions. There is no need of transferring the execution like a procedure.

Macro Block without Parameters <Macro name> MACRO ………………..Instructions…………… ENDM Code Segment <Macro name> Code ends

Macro Block with Parameters <Macro name> MACRO (arg1,arg2,....) ………………..Instructions…………… ENDM Code Segment <Macro name> (arg1,arg2,..) Code ends

Advantage of Procedure and Macros: Procedures: Advantages The machine codes for the group of instructions in the procedure only have to be put once. Disadvantages Need for stack Overhead time required to call the procedure and return to the calling program. Macros: Advantages Macro avoids overhead time involving in calling and returning from a procedure. Generating in line code each time a macro is called is that this will make the program take up more memory than using a procedure.

Procedures and Macros - Comparison Code of procedure is once loaded in memory. Procedures will use CALL instruction for accessing. More Time taken for CALL and RET. Stack is needed. More no. of Instructions Code of macro have to be loaded repeatedly in memory. No CALL Instruction No-over head time for CALL and RET. No Need of Stack. Less no. of Instructions.