Chapter 8 Structures and MacrosTopicsStructuresStructures ContinuedSlide 5Slide 6Slide 7Defining MacrosDefining Macros ContinuedSlide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Macros Calling ProceduresMacros Calling Procedures ContinuedConditional-Assembly DirectivesAdvance Macros and DirectivesCSC 20601/14/19 1Chapter 8 Structures and MacrosInstructor: James HutchinsonCSC 20601/14/19 2TopicsStructuresDefining MacrosMacros Calling ProceduresThe Rep MacroApplicationCSC 20601/14/19 3Structures Assembly Language StructureDefinition: A structure is a composite data type consisting of a group of possibly dissimilar data components that can be accessed as a unit or by component.General Form: name STRUC field-declaration name ENDSWhere: name is the name of the structure.STRUC is an assembler keyword used to specify a structure.field-declaration is a list of one or more variable declarations.ENDS is an assembler keyword that marks the end of the structure declaration.CSC 20601/14/19 4Structures ContinuedA Structure declaration defines a data type.Memory is not allocated until an instance of structure is defined. Defining Variables of structure typeGeneral Form: [Variable_Name] TypeName <[initializer], [initializer]>OR [Variable_Name] TypeName constant DUP <initializer>Where:Variable_Name is a valid Assembler identifier that is the symbolic name of the variableTypeName is the name of the structure.Constant is an optional numeric constant the defines the number of time the structure definition should be repeated.CSC 20601/14/19 5Structures ContinuedExample of a Date Structure:Date strucmonth db ?Day db 0Year dw 0 Date endsNewYear Date <1,1,2003> //declaration of a variable of typed dateIndependence Date <7,4,1776>Date is a structure type consisting of three fieldsAny number of fields can be defined within a struc.The structure type declaration does not actually place a structure in memory. It describes the format that any structure variable of this type will have.CSC 20601/14/19 6Structures ContinuedThe DUP operator can be used to define an array of structure.Example:StudentType Struc StudentID dw ? FirstName db 10 LastName db 10 Grades db 16 dup (0)StudentType EndsMike StudentType <1239, ‘Mike’,’Harris’,>CSC206 StudentType 35 DUP (<>)Accessing the components of a structure.Structures variables can be accessed by name.Fields within the structure also is accessed individually by name or address using a dot (.) notation.VariableName.FieldNameCSC 20601/14/19 7Structures ContinuedExample of Structure Access.Mov al, NewYear.Day //Store a 1 in alMov ax, Independance.Year //store 1776 in AXMov si, Offset MikeMov di, 2Mov ax, si.grades[di] //store Mike’s second score in grades.Access Elements of an Array of Structures.RecordSize = 38Mov si, 4 //Relative record indexMov di, 1//Load the score indexMov RecordSize ax//Load the size of the structurexor dx,dx //Clear DXmul simov ax, siMov al, CSC206[SI].Grades[di]CSC 20601/14/19 8Defining MacrosDefinition:A macro is a group of assembler statements referenced by a single symbolic identifier.A macro expands inline and does not place a return address on the stack.General Form of a Macro definition.macroName Macro [parameters]Where:macroName is a valid Assembler identifier.Macro is an Assembler Language reserve word that designates the group of statements is a macro.Parameters is a list of dummy parameters that will be replaced at run time by actual paramters.CSC 20601/14/19 9Defining Macros ContinuedExample: A macro to display a string.datastring db ‘The house is blue ‘DisplayString Macro dstring mov dx. Offset dstring mov ah, 9 int 21hEndMHow called: DisplayString stringA macro definition consists of three parts:The Macro HeaderThe Macro BodyThe ENDM DirectiveCSC 20601/14/19 10Defining Macros ContinuedThe Macro Heading Contains The macro-nameThe Key-word MacroThe Macro Parameter listExample; Print_Character Macro charInvocation Print_char al ;al contains the char to be printed.The Macro BodyConsists of the group of statements you would like the assembler to insert when the macro is invoked.The sequence of statement can be instructions, data definitions, directives, interrupts, or other macros.The Macro Body can also contain conditional and repeat statements to provide for complex logic within the macro expansion.CSC 20601/14/19 11Defining Macros ContinuedThe Macro Ending consists only of the ENDM (End Macro) statement.The endm directive marks the end of the macro.When the Macro is invoked, the macro name is expanded to all of the statements contained in the body of the macro.The endm directive does not require the name of the macro.One and only on endm directive must appear in each macro.Macro Example: Define a macro that inputs a single character from the keyboardGetChar Macro Headingmov ah, 1 ;;designate DOS function 1int 21h ;;Call the OSEndm EndingBodyCSC 20601/14/19 12Defining Macros ContinuedMacro ParametersParameters are the mechanism used to supply variable information to the macro.Parameters are valid assembler identifiers that appear in the macro heading separated by commas.There is no limit to the number of parameters in the macro definition.The Parameter appear in statements within the body of the macro definition.When the macro is invoke, values are supplied for the parameters. These values are substituted for the parameters during macro expansion.ExampleDisplay_Message Macro Messagemov dx, Messagemov ah, 9int 21hENDMCSC 20601/14/19 13Defining Macros ContinuedInvocation of the macro:Prompt db ‘Enter your Name => $’Name db 20 dup(‘ ‘)Display_Message PromptThe assembler will expand Display_Message Prompt into the following:mov dx, Promptmov ah, 9int 21hMacro Expansion occurs at Assembly. At each place where the macro name is used, the assembler will replace the name of the macro with the statements contained within the body of the macro definition.CSC 20601/14/19 14Defining Macros ContinuedRequired ParametersA parameter can be designated as required by use of the REQ qualifier.General form of the REQ qualifier:Macro_Name Macro Parm1:REQ, Parm2:REQ, …,ParmN:REQIf a parameter is defined with the REQ
View Full Document