Assembly Language for Intel-Based Computers, 5 th Edition Chapter 10: Structures and Macros (c) Pearson Education, 2006-2007. All rights reserved. You.

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.
Assembly Language for Intel-Based Computers Chapter 15: BIOS-Level Programming (c) Pearson Education, All rights reserved. You may modify and.
Assembly Language Procedures.
Assembly Language for Intel-Based Computers Chapter 8: Advanced Procedures Kip R. Irvine.
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, 4 th Edition Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Symbolic Constants Equal-Sign Directive Calculating.
Runtime Stack Managed by the CPU, using two registers
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Intrinsic Data Types (1 of 2) BYTE, SBYTE 8-bit unsigned.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
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 1: Basic Concepts (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers, 4 th Edition Lecture 23: Finite State Machines, WHILE operator (c) Pearson Education, All rights reserved.
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.
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 3: Assembly Language Fundamentals Assembling, Linking and Running Programs Example Programs.
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.
CSC 221 Computer Organization and Assembly Language Lecture 12: Addressing Modes in Assembly.
Assembly Language for Intel-Based Computers, 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
Sahar Mosleh California State University San MarcosPage 1 Nested Procedure calls and Flowcharts.
Assembly Language for Intel-Based Computers Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify and copy.
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.
Structures and Macros Computer Organization and Assembly Languages Yung-Yu Chuang 2005/12/08 with slides by Kip Irvine.
CT215: Assembly Language Programming Chapter 10: Macros (c) Pearson Education, All rights reserved. You may modify and copy this slide show for your.
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 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.
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.
Assembly Language for Intel-Based Computers, 4 th Edition Lecture 22: Conditional Loops (c) Pearson Education, All rights reserved. You may modify.
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.
CSC 221 Computer Organization and Assembly Language
Chapter 5: Procedures.
Assembly Language Lab 9.
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
Computer Organization and Assembly Language
More on operators and procedures in the Linked
Presentation transcript:

Assembly Language for Intel-Based Computers, 5 th Edition Chapter 10: Structures and 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. Slide show prepared by the author Revision date: May 5, 2006 Kip R. Irvine

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Chapter Objectives Understand the syntax for defining structures and unions Know how to reference structure and union variables in code Know how to work with nested structures Know how to create macros with multiple parameters Know how to create nested macros Be familiar with the macros in the book's macro library

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Macros Introducing Macros Defining Macros Invoking Macros Macro Examples Nested Macros Example Program: Wrappers

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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.

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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".

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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.

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Invoking Macros (2 of 2) Relationships between macros, arguments, and parameters:

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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.

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Blank Argument If you pass a blank argument, the error is also caught when the expanded code is assembled. Example:.code mPutchar 1push eax 1mov al, 1call WriteChar 1pop eax

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Macro Examples mReadStr - reads string from standard input mGotoXY - locates the cursor on screen mDumpMem - dumps a range of memory

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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.

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, mGotoXY mGotoxy MACRO X:REQ, Y:REQ push edx mov dh,Y mov dl,X call Gotoxy pop edx ENDM The mGotoXY macro ets the console cursor position by calling the Gotoxy library procedure. The REQ next to X and Y identifies them as required parameters.

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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.

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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.

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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.

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, mAskForString 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 mAskForString MACRO row, col, prompt, inbuf call Clrscr mGotoXY col, row mWrite prompt mReadStr inbuf ENDM

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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 TITLE Procedure Wrapper Macros (Wraps.ASM) ; This program demonstrates macros as wrappers ; for library procedures. Contents: mGotoxy, mWrite, ; mWriteLn, mWriteStr, mReadStr, and mDumpMem. ; Last update: 3/16/02 INCLUDE Irvine32.inc INCLUDE Macros.inc; macro definitions.data array DWORD 1,2,3,4,5,6,7,8 firstName BYTE 31 DUP(?) lastName BYTE 31 DUP(?)

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Wrap (Cont.). code main PROC mGotoXY 20,0 mWriteLn "Sample Macro Program" mGotoXY 0,5 mWrite "Please enter your first name: " mReadStr firstName call Crlf mWrite "Please enter your last name: " mReadStr lastName call Crlf ; Display the person's complete name: mWrite "Your name is " mWriteStr firstName mWrite " " mWriteStr lastName ; Display a dump of the array: call Crlf mDumpMem OFFSET array,LENGTHOF array, TYPE array exit main ENDP END main