CT215: Assembly Language Programming Chapter 10: Macros (c) Pearson Education, 2002. All rights reserved. You may modify and copy this slide show for your.

Slides:



Advertisements
Similar presentations
CSC 221 Computer Organization and Assembly Language Lecture 21: Conditional and Block Structures: Assembly Programs.
Advertisements

Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
CS2422 Assembly Language and System Programming Structure and Macros Department of Computer Science National Tsing Hua University.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
Runtime Stack Managed by the CPU, using two registers
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
CS2422 Assembly Language and System Programming Procedures Department of Computer Science National Tsing Hua University.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 10: Structures and Macros (c) Pearson Education, All rights reserved. You may modify.
1 Structure A template or pattern given to a logically related group of variables. field - structure member containing data Program access to a structure:
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
CS2422 Assembly Language & System Programming October 31, 2006.
CS2422 Assembly Language & System Programming October 24, 2006.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 10: Structures and Macros (c) Pearson Education, All rights reserved. You.
Coding.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Defining and Using Procedures Creating Procedures.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Stack Operations Runtime Stack PUSH Operation POP.
CS2422 Assembly Language and System Programming High-Level Language Interface Department of Computer Science National Tsing Hua University.
1 Lecture 6 Conditional Processing Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
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 ;
Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 8: Advanced Procedures (c) Pearson Education, All rights reserved. You may.
Today's topics Multi-dimensional arrays Multi-dimensional arrays String processing String processing Macros Macros.
Assembly Language for Intel-Based Computers, 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
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.
Sahar Mosleh California State University San MarcosPage 1 Nested Procedure calls and Flowcharts.
Assembly Language for x86 Processors 7th Edition Chapter 13: High-Level Language Interface (c) Pearson Education, All rights reserved. You may modify.
CSC 221 Computer Organization and Assembly Language
ASSEMBLY LANGUAGE FOR INTEL-BASED COMPUTERS, PROCEDURES.
Assembly Language for x86 Processors 6th Edition Chapter 10: Structures and Macros (c) Pearson Education, All rights reserved. You may modify and.
Chapter 8 Advanced Programming Applications Objectives: Linking separate object files together Creating and using an object code library Predicting the.
Structures and Macros Computer Organization and Assembly Languages Yung-Yu Chuang 2005/12/08 with slides by Kip Irvine.
Computer Organization and Assembly Languages Yung-Yu Chuang 2006/11/13
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 10: Structures and Macros (c) Pearson Education, All rights reserved. You.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 10: Structures and Macros (c) Pearson Education, All rights reserved. You.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
CHAPTER 5: PROCEDURES ASSEMBLY LANGUAGE FOR INTEL- BASED COMPUTERS, 5 TH EDITION (c) Pearson Education, All rights reserved. You may modify and copy.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures Lecture 18 Linking to External Library The Book’s Link Library Stack Operations.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures Lecture 19: Procedures Procedure’s parameters (c) Pearson Education, 2002.
Procedure Computer Organization and Assembly Languages Yung-Yu Chuang 2005/10/27 with slides by Kip Irvine.
Lecture 17 Structures and Macros Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.
Assembly Programming Practical2. Initialising Variables (Recap) Initialising variables are done in the.data section Initialising variables are done in.
CSC 221 Computer Organization and Assembly Language Lecture 15: STACK Related Instructions.
Chapter 10 Structures and Macros Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
Assembly Language for x86 Processors 7th Edition Chapter 10: Structures and Macros (c) Pearson Education, All rights reserved. You may modify and.
3/10/2003 Lecture 9: Procedures Assembly Language for Intel-Based Computers 4th edition Kip R. Irvine.
CSC 221 Computer Organization and Assembly Language
Assembly Language for x86 Processors 6th Edition
Chapter 5: Procedures.
Assembly Language Lab 9.
Computer Architecture and Assembly Language
Additional Assembly Programming Concepts
High-Level Language Interface
MACRO Processors CSCI/CMPE 3334 David Egle.
Assembly Language for x86 Processors 7th Edition
Chapter 10: Structures and Macros
Stack Frames and Advanced Procedures
Assembly Language for Intel-Based Computers, 4th Edition
Assembly Language for Intel-Based Computers, 5th Edition
The Stack and Procedures
Computer Organization and Assembly Language
More on operators and procedures in the Linked
Computer Architecture and System Programming Laboratory
Presentation transcript:

CT215: Assembly Language Programming Chapter 10: Macros (c) Pearson Education, All rights reserved. You may modify and copy this slide show for your personal use, or for use in the classroom, as long as this copyright statement, the author's name, and the title are not changed.

CS3280, Fall 05. Chapter 10 2 Introducing Macros A macro 1 is a named block of assembly language statements. Once defined, it can be invoked (called) one or more times. During the assembler's preprocessing step, each macro call is expanded into a copy of the macro. The expanded code is passed to the assembly step, where it is checked for correctness. 1 Also called a macro procedure.

CS3280, Fall 05. Chapter 10 3 Defining Macros A macro must be defined before it can be used. Parameters are optional. Each parameter follows the rules for identifiers. It is a string that is assigned a value when the macro is invoked. Syntax: macroname MACRO [parameter-1, parameter-2,...] statement-list ENDM

CS3280, Fall 05. Chapter 10 4 mNewLine Macro Example mNewLine MACRO; define the macro call Crlf ENDM.data.code mNewLine; invoke the macro This is how you define and invoke a simple macro. The assembler will substitute "call crlf" for "mNewLine".

CS3280, Fall 05. Chapter 10 5 mPutChar Macro mPutchar MACRO char push eax mov al,char call WriteChar pop eax ENDM Writes a single character to standard output. Definition:.code mPutchar 'A' Invocation: 1push eax 1mov al,'A' 1call WriteChar 1pop eax Expansion: viewed in the listing file

CS3280, Fall 05. Chapter 10 6 Invoking Macros (1 of 2) When you invoke a macro, each argument you pass matches a declared parameter. Each parameter is replaced by its corresponding argument when the macro is expanded. When a macro expands, it generates assembly language source code. Arguments are treated as simple text by the preprocessor.

CS3280, Fall 05. Chapter 10 7 Invoking Macros (2 of 2) Relationships between macros, arguments, and parameters:

CS3280, Fall 05. Chapter 10 8 mWriteStr Macro (1 of 2) mWriteStr MACRO buffer push edx mov edx,OFFSET buffer call WriteString pop edx ENDM.data str1 BYTE "Welcome!",0.code mWriteStr str1 Provides a convenient way to display a string, by passing the string name as an argument.

CS3280, Fall 05. Chapter 10 9 mWriteStr Macro (2 of 2) 1push edx 1mov edx,OFFSET str1 1call WriteString 1pop edx The expanded code shows how the str1 argument replaced the parameter named buffer: mWriteStr MACRO buffer push edx mov edx,OFFSET buffer call WriteString pop edx ENDM

CS3280, Fall 05. Chapter Invalid Argument If you pass an invalid argument, the error is caught when the expanded code is assembled. Example:.code mPutchar 1234h 1push eax 1mov al,1234h; error! 1call WriteChar 1pop eax

CS3280, Fall 05. Chapter Blank Argument If you don’t pass an argument, it may cause an error (possibly) caught when the expanded code is assembled. Example:.code mPutchar 1push eax 1mov al, 1call WriteChar 1pop eax

CS3280, Fall 05. Chapter Macro Examples mReadStr - reads string from standard input mGotoXY - locates the cursor on screen mDumpMem - dumps a range of memory

CS3280, Fall 05. Chapter mReadStr mReadStr MACRO varName push ecx push edx mov edx,OFFSET varName mov ecx,(SIZEOF varName) - 1 call ReadString pop edx pop ecx ENDM.data firstName BYTE 30 DUP(?).code mReadStr firstName The mReadStr macro provides a convenient wrapper around ReadString procedure calls.

CS3280, Fall 05. Chapter mGotoXY mGotoxy MACRO X:REQ, Y:REQ push edx mov dh,Y mov dl,X call Gotoxy pop edx ENDM The mGotoXY macro sets the console cursor position by calling the Gotoxy library procedure. The REQ next to X and Y identifies them as required parameters.

CS3280, Fall 05. Chapter mDumpMem mDumpMem MACRO address, itemCount, componentSize push ebx push ecx push esi mov esi,address mov ecx,itemCount mov ebx,componentSize call DumpMem pop esi pop ecx pop ebx ENDM The mDumpMem macro streamlines calls to the link library's DumpMem procedure.

CS3280, Fall 05. Chapter N.b., macros can call macros mDump mDump MACRO varName:REQ, useLabel IFB EXITM ENDIF call Crlf IFNB mWrite "Variable name: &varName" ELSE mWrite " " ENDIF mDumpMem OFFSET varName, LENGTHOF varName, TYPE varName ENDM The mDump macro displays a variable, using its known attributes. If is nonblank, the name of the variable is displayed.

CS3280, Fall 05. Chapter mWrite mWrite MACRO text LOCAL string.data;; data segment string BYTE text,0;; define local string.code;; code segment push edx mov edx,OFFSET string call Writestring pop edx ENDM The mWrite macro writes a string literal to standard output. It is a good example of a macro that contains both code and data. The LOCAL directive prevents string from becoming a global label.

CS3280, Fall 05. Chapter Nested Macros The mWriteLn macro contains a nested macro (a macro invoked by another macro). mWriteLn MACRO text mWrite text call Crlf ENDM mWriteLn "My Sample Macro Program" 2.data 2??0002 BYTE "My Sample Macro Program",0 2.code 2 push edx 2 mov edx,OFFSET ?? call Writestring 2 pop edx 1 call Crlf nesting level

CS3280, Fall 05. Chapter Your turn... Write a nested macro named mAskForString that clears the screen, locates the cursor at a given row and column, prompts the user, and inputs a string. Use any macros shown so far. Use the following code and data to test your macro:.data acctNum BYTE 30 DUP(?).code main proc mAskForString 5,10,"Input Account Number: ", \ acctNum Solution...

CS3280, Fall 05. Chapter Solution mAskForString MACRO row, col, prompt, inbuf call Clrscr mGotoXY col, row mWrite prompt mReadStr inbuf ENDM View the solution program

CS3280, Fall 05. Chapter Example Program: Wrappers The Wraps.asm program demonstrates various macros from this chapter. It shows how macros can simplify the passing of register arguments to library procedures. View the source codesource code