ICS312 Set 14 MACROS. Macros The program structure is similar to that for procedures. As for procedure names, macro names represent a group of instructions.

Slides:



Advertisements
Similar presentations
The Assembly Language Level
Advertisements

Macro Processor.
Lecture 2 Introduction to C Programming
Subroutines: Passing Arguments Using the Stack. Passing Arguments via the Stack Arguments to a subroutine are pushed onto the stack. The subroutine accesses.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#4)
Chapter 8: Programming the Microprocessor. Copyright ©2009 by Pearson Education, Inc. Upper Saddle River, New Jersey All rights reserved. The Intel.
Kip Irvine: Assembly Language for Intel-Based Computers Overview Stack Operations (PUSH and POP) Procedures Procedure Parameters Software Interrupts MS-DOS.
Chapter 12: High-Level Language Interface. Chapter Overview Introduction Inline Assembly Code C calls assembly procedures Assembly calls C procedures.
Chapter 4 Macro Processors
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
Guide To UNIX Using Linux Third Edition
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.
INTRODUCTION TO IBM PC ASSEMBLY LANGUAGE
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
1 CSC103: Introduction to Computer and Programming Lecture No 26.
Chapter 2 Software Tools and Assembly Language Syntax.
ICS312 Set 4 Program Structure. Outline for a SMALL Model Program Note the quiz at the next lecture will be to reproduce this slide.MODEL SMALL.586 ;
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Micro-Computer Applications: Procedures & Interrupts Dr. Eng. Amr T. Abdel-Hamid ELECT 707 Fall 2011.
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.
Chapter 3 Elements of Assembly Language. 3.1 Assembly Language Statements.
chap13 Chapter 13 Programming in the Large.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 251 Introduction to Computer Organization.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 6: User-Defined Functions
Chapter 13 Programming in the Large Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
Today's topics Multi-dimensional arrays Multi-dimensional arrays String processing String processing Macros Macros.
Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C Chapter 7 – Subroutines These are lecture notes to accompany the book SPARC Architecture,
L AB 2. P ROGRAM STRUCTURE The assembly language program consist of code, data and stack. Data segment: contains all the variable definition..Data Code.
1 Remember: Examination is a chance not ability. By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL.
21/11/2005CAP2411 Input & Output Instructions CPU communicates with the peripherals through I/O registers called I/O ports. There are 2 instructions, IN.
Chapter 8 Advanced Programming Applications Objectives: Linking separate object files together Creating and using an object code library Predicting the.
by Richard P. Paul, 2nd edition, 2000.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 10: Structures and Macros (c) Pearson Education, All rights reserved. You.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Lecture 9 (The Stack and Procedures). 1 Lecture Outline Introduction The Stack The PUSH Instruction The POP Instruction Terminology of Procedures INDEC.
Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1.
Macro Processors Basic Functions Machine-Independent Features Design Options Implementation Examples.
 A macro represents a commonly used group of statements in the source programming language.  The macro processor replaces each macro instruction with.
ICS312 Set 12 Subroutines: Passing Arguments Using the Stack.
Programming Fundamentals Enumerations and Functions.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Assembly Language Lecture 2. Lecture Outline Program Structure Memory models Data Segment Stack Segment Code Segment Input and Output Instructions INT.
File Operations. FILE PROCESSING For the purposes of the following discussion, reading means copying all or part of an existing file into memory Writing.
1 Chapter 4 Macro Processors. 2 Introduction A macro instruction (abbreviated to macro) is simply a notational convenience for the programmer. A macro.
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 11–Macro-Processors.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Macro Processor Design Options Recursive Macro Expansion General-Purpose Macro Processors Macro Processing within Language Translators.
Assembly language programming
Chapter 8: Programming the Microprocessor
Chapter 6: User-Defined Functions I
Assembly Language Lab 9.
Format of Assembly language
MODULAR PROGRAMMING Many programs are too large to be developed by one person. programs are routinely developed by teams of programmers The linker program.
Additional Assembly Programming Concepts
Microprocessor and Assembly Language
Computer Organization & Assembly Language
(The Stack and Procedures)
User-Defined Functions
Arithmetic Instructions
MACRO Processors CSCI/CMPE 3334 David Egle.
Programming 8086 – Part IV Stacks, Macros
(The Stack and Procedures)
Chapter 4 –Requirements for coding in Assembly Language
Morgan Kaufmann Publishers Computer Organization and Assembly Language
(The Stack and Procedures)
Procedures & Macros Introduction Syntax Difference.
Procedures and Macros.
Presentation transcript:

ICS312 Set 14 MACROS

Macros The program structure is similar to that for procedures. As for procedure names, macro names represent a group of instructions

Macros (Cont.) A procedure is called at execution time; control transfers to the procedure and returns after executing its statements. A macro is invoked at assembly time. The assembler copies the macro's statements into the program at the position of the invocation. (This is called a macro expansion.)

Definition A macro is a block of text that has been given a name. When the assembler encounters the name during assembly, it inserts the block into the program. The text may consist of instructions, pseudo-ops, comments, or references to other macros. Syntax: macro_name MACRO d1, d2, …, dn statements (body of macro) ENDM Where d1, d2, …, dn is a list of dummy arguments separated by commas.

Example We know that the operands of the MOV instruction cannot both be word variables, but we can define a macro to move a word source variable to a word destination variable. MOVW MACRO WORD1, WORD2 PUSH WORD2 POP WORD1 ENDM

How to use a Macro To use a macro in a program, we invoke it. Syntax: macro_name a1, a2, a3,…,an where a1, a2, …, an is a list of actual arguments separated by commas.

Example on using Macro Invoke the macro MOVW to move B to A, where A and B are word variables..DATA A DW ? B DW ?.CODE … MOVW A, B

Macro expansion Macro expansion - character substitution of actual arguments separated by commas. These instructions: MOVW A, DX MOVW A+2, B insert the following (equivalent) code into a program: PUSH DX POP A PUSH B POP A+2

Restoring Registers Good programming practice : A procedure should restore the registers that it uses, unless they contain output values. The same is true for macros.

Example Program Listing.MODEL SMALL.STACK 100H.DATA A DW 1,2 B DW 3.CODE MOVW MACRO WORD1,WORD2 PUSH WORD2 POP WORD1 ENDM

Program Listing (Cont.) MAIN PROC MOV MOV DS,AX MOVW A,DX MOVW A+2,B ;dos exit MOV AH,4C00H INT 21H MAIN ENDP END MAIN

Listing Files If your program is called e.g. prog1.asm, then: ml /Fl prog1.asm will produce a listing file called prog1.lst, with the invoked macros expanded,and error messages following the lines they refer to. Finding Assembly Errors Errors found during macro expansion are listed at the point of the macro invocation. To find where the mistake really is, you need to inspect the macro expansion in the.LST file - this is especially helpful if the macro invokes other macros.

Local Labels If such a macro is invoked more than once in a program, duplicate labels occur, causing an assembly error. To avoid this, get the Assembler translator to substitute unique labels at each invocation of the macro by declaring the label as local. The LOCAL declaration must appear on the first line of the macro following the name of the macro. Syntax: LOCAL list of labels

Example A macro to place the largest of two words in AX: GET_BIG MACRO WORD1, WORD2 LOCAL OUT ; local label MOV AX, WORD1 CMP AX, WORD2 JG OUT MOV AX, WORD2 OUT: ENDM Successive invocations of this macro causes the assembler to insert labels ??0000, ??0001, ??0002, etc. into the program in place of “OUT”. These labels are unique and not likely to conflict with labels chosen by the user.

Macros that Invoke Other Macros Macros may invoke other macros, or themselves. When this happens, the assembler expands each macro as it is encountered.

Macro Libraries Macros may be stored in separate files, called libraries. Use the INCLUDE pseudo-op to include the library file in a program. This copies all of the macros defined in the library file into the program listing. The INCLUDE statement can appear anywhere before the included macros are invoked in the program. Example of syntax: INCLUDE A:\MACROS.INC ; path and filename

Tradeoffs between Macros and Procedures Assembly Time A program containing macros normally takes longer to assemble than a program containing procedures because of the time required to do the macro expansions. Execution Time A program containing macros typically executes faster than a program with procedures, because the macro code is "in-line" and there is no requirement to transfer control.

Tradeoffs between Macros and Procedures (Cont.) Program Size A program containing macros will usually be larger than a program with procedures because of the additional lines of code generated during the macro expansion. Best Uses of Macros and Procedures Macros are very good for short, frequently executed tasks (e.g., short utility functions). They are easier to write and have greater readability than procedures. Procedures are good for handling longer, more complicated (and less frequently used) tasks.

Assembling a routine with macros If the program is called prog1, then employ: ml /Fl /Zi prog1.asm util.lib The “/Fl” option causes a listing program called, in this case, prog1.lst, to be generated. It shows were assembler errors occur, and supplies the macro expansions. The “/Zi” option is to produce a program (prog1.exe), which you can debug using: cv prog1

Textbook Reading (Jones): Chapter 8