Assembly Language Macros Most assemblers include support for macros. The term macro refers to a word that stands for an entire group of instructions. Macro.

Slides:



Advertisements
Similar presentations
Assembly Language – 1.
Advertisements

1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
MIPS Assembly Language Programming
Lecture 8: MIPS Instruction Set
The Assembly Language Level
Macro Processor.
Lab6 – Debug Assembly Language Lab
Chapter 8: Programming the Microprocessor. Copyright ©2009 by Pearson Education, Inc. Upper Saddle River, New Jersey All rights reserved. The Intel.
EECC250 - Shaaban #1 lec #18 Winter Assembly Language Macros Most assemblers include support for macros. The term macro refers to a word that.
CSCE 121, Sec 200, 507, 508 Fall 2010 Prof. Jennifer L. Welch.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Table 1. Software Hierarchy Levels.. Essential Tools An assembler is a program that converts source-code programs into a machine language (object file).
Chapter 4 Macro Processors
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
1 Chapter 4 Macro Processors Professor Gwan-Hwan Hwang Dept. Computer Science and Information Engineering National Taiwan Normal University 9/17/2009.
Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#6)
Chapter 2 Software Tools and Assembly Language Syntax.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
C++ for Engineers and Scientists Third Edition
A Simple Two-Pass Assembler
Chapter 3 Elements of Assembly Language. 3.1 Assembly Language Statements.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali.
EEC4133 Computer Organization & Architecture Chapter 6: Languages and the Machine by Muhazam Mustapha, May 2014.
Today's topics Multi-dimensional arrays Multi-dimensional arrays String processing String processing Macros Macros.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Passing Parameters using Stack Calling program pushes parameters on the stack one element at a time before calling subroutine. Subroutine Call (jsr, bsr)
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
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.
9/20/6Lecture 2 - Prog Model Architecture, Data Types and Addressing Modes.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
CIS 020 Assembly Programming
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.
Subroutines and Stacks. Stack The stack is a special area in memory used by the CPU to store register information or general data information during program.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
1 Chapter 4 Macro Processors. 2 Introduction A macro instruction (abbreviated to macro) is simply a notational convenience for the programmer. A macro.
Macro Processor Design Options Recursive Macro Expansion General-Purpose Macro Processors Macro Processing within Language Translators.
Functions.
User-Written Functions
Chapter 8: Programming the Microprocessor
Figure 8.1 of course package
Addressing Modes in Microprocessors
Lecture 6: Assembly Programs
MODULAR PROGRAMMING Many programs are too large to be developed by one person. programs are routinely developed by teams of programmers The linker program.
Computer Architecture and Assembly Language
Subroutines … passing data
Chapter 3 Machine Language and Assembly Language.
Chapter 3 Machine Language and Assembly Language.
Computer Architecture and Organization Miles Murdocca and Vincent Heuring Chapter 4 – The Instruction Set Architecture.
Subroutines … a 1st look procedures and functions in high level languages are modeled on subroutines typically, assembly code is very modular with.
MACRO Processors CSCI/CMPE 3334 David Egle.
CSCE Fall 2013 Prof. Jennifer L. Welch.
Passing Parameters Data passed to a subroutine is called a parameter.
by Richard P. Paul, 2nd edition, 2000.
68000 Architecture, Data Types and Addressing Modes
Figure 8.1 of course package
A Simple Two-Pass Assembler
Other ISAs Next, we’ll first we look at a longer example program, starting with some C code and translating it into our assembly language. Then we discuss.
Other ISAs Next, we’ll first we look at a longer example program, starting with some C code and translating it into our assembly language. Then we discuss.
CSCE Fall 2012 Prof. Jennifer L. Welch.
Lecture 6: Assembly Programs
Computer Organization and Assembly Language
ECE511: Digital System & Microprocessor
Procedures & Macros Introduction Syntax Difference.
Presentation transcript:

Assembly Language Macros Most assemblers include support for macros. The term macro refers to a word that stands for an entire group of instructions. Macro is a text substitution facility It allows programmer to define their own opcodes and also operands move.wX,d0 mulsd0,d0 sqr move.wd0,X Inline subroutines –Avoids overhead of subroutine calls (jsr, rts) –Faster than subroutine Code is generated when macro is actually used Additional code is generated during each macro call

Differences Between Macros and Subroutines Both permit a group of instructions to be defined as a single entity with a unique given label or name called up when needed. A subroutine is called by the BSR or JSR instructions, while a macro is called by simply using its name. Simpler to write and use (subroutines are more complex, stacks are used) Macros are faster than subroutines (no overheads, no saving of return addresses)

Differences Between Macros and Subroutines Macros are not a substitute for subroutines: –Since the macro is substituted with the code and additional code is generated every time a macro is called, very long macros that are used many times in a program will result in an enormous expansion of the code size Wastage of storage due to multiple copies –In this case, a subroutine would be a better choice, since the code in the body of the subroutine is not inserted into source code many when called. Support for subroutines is provided by the CPU --here, the as part of the instruction set, while support for macros is part of the assembler (similar to assembler directives).

Assembly Language Macros Using macros in an assembly program involves two steps: 1 Defining a macro: The definition of a macro consists of three parts: the header, body, and terminator: MACRO The header.... The body: instructions to be executed ENDM The terminator Example: sqrmacro moveX,d0 muls d0,d0 moved0,X endm

Assembly Language Macros Using macros in an assembly program involves two steps: 2 Invoking a macro by using its given on a separate line followed by the list of parameters used if any: [parameter list] When macro is called it is replaced by the body of the macro Parameters – order of parameters is important

A Macro Example AddMul MACROMacro definition ADD.B#7,D0 D0 = D0 + 7 AND.W#00FF,D0 Mask D0 to a byte MULU#12,D0D0 = D0 x 12 ENDMEnd of macro def. MOVE.BX,D0Get X AddMulCall the macro... MOVE.BY,D0Get Y AddMulCall the macro Defining the macro: Invoking the macro:

Macros and Parameters A macro parameter is designated within the body of the macro by a backslash "\" followed by a single digit or capital letter: \1,\2,\3... \A,\B,\C... \Z Thus, up to 35 different, substitutable arguments may used in the body of a macro definition. The enumerated sequence corresponds to the sequence of parameters passed on invocation. –The first parameter corresponds to \1 and the 10 th parameter corresponds to \A. –At the time of invocation, these arguments are replaced by the parameters given in the parameter list. –If less number of operands than in the body of macro, null string is assigned to the excess operands in body

Macro Example with Parameter Substitution AddMul MACROMacro definition ADD.B#7,\1 Reg = Reg + 7 AND.W#00FF,\1 Mask Reg to a byte MULU#12,\1Reg = Reg x 12 ENDMEnd of macro def. MOVE.BX,D0Get X AddMulD0Call the macro... MOVE.BY,D1Get Y AddMulD1Call the macro Defining the macro: Invoking the macro:

Another Macro Example with Parameter Substitution Add3 MACROMacro definition move.l\1, \4 add.l\2, \4 add.l\3, \4 ENDMEnd of macro def. Add3D2,D5,D6,D0Call the macro move.l D2,D0 add.lD5,D0 macro expansion add.lD6,D0 Add3#2,D2,D3,D7Call the macro move.l#2,D7 add.lD2,D7 macro expansion add.lD3,D7 Defining the macro: Invoking the macro:

Labels Within Macros Since a macro may be invoked multiple times within the same program, it is essential that there are no conflicting labels result from the multiple invocation. BusyWaitmacro movem.l d0-d1, -(a7) outermove.w \1, d1 move.w #$FFFF, d0 innerdbra d0, inner dbra d1, outer movem.l (a7)+, d0-d1 endm If macro in invoked more than once, it will lead to multiple declaration of symbols outer and inner

Labels Within Macros Multiple invocation problem can be corrected by using two local symbols and two extra parameters BusyWaitmacro movem.l d0-d1, -(a7) \3move.w \1, d1 move.w #$FFFF, d0 \2dbra d0, \2 dbra d1, \3 movem.l (a7)+, d0-d1 endm To invoke the macro, a new set of parameters should be provided. BusyWait x, outer1, inner1 BusyWait x, outer2, inner2 BusyWait x, outer3, inner3

Labels Within Macros Instead of keeping track of the labels generated, the special designator is used to request unique labels from the assembler macro preprocessor. For each macro invocation, the designator is replaced by a number unique to that particular invocation. It is replaced by.nnn (number of macro expansions that have already occurred) The is appended to the end of a label.

Labels Within Macros BusyWaitmacro movem.l d0-d1, -(a7) \1, d1 move.w #$FFFF, d0 d0, dbra d1, movem.l (a7)+, d0-d1 endm If macro in invoked more than once: –first invocation will replace it with outer.001 and inner.001 –second invocation will replace it with outer.002 and inner.002

Internal Macro Label Example Macro SUM adds the sequence of integers in the range: i, i+1, …., n Macro Definition: SUM MACRO\1 = start \2 = stop \3 = sum CLR.W\3sum = 0 ADDQ.W#1,\2stop = stop +1 ADD.W\1,\3For i = start to stop ADD.W#1,\1sum = sum + i CMP.W\1,\2 ENDM Sample macro SUM invocation: SUMD1,D2,D3 D1 = start D2 = stop D3 = sum

Macro Example: ToUpper, A String Conversion Macro * ToUpper Address-Register * This macro converts a string from lower case to upper case. * The argument is an address register. The string MUST be * terminated with $0 * ToUpper macro #0,(\1) test for end of string beq cmpi.b #'a',(\1) if < 'a' not lower case blt cmpi.b #'z',(\1) if <= 'z' is a lower case ble #1,\1 bra #32,(\1)+convert to upper case bra endmEnd of macro