Structures and Macros - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Structures and Macros

Description:

mov bx,radix. call Writeint. pop bx. pop ax. endm. 13. Conditional ... mWriteint macro value,radix. ifb value ;; if blank, exitm ;; exit macro. endif. push ax ... – PowerPoint PPT presentation

Number of Views:94
Avg rating:3.0/5.0
Slides: 23
Provided by: kip59
Category:

less

Transcript and Presenter's Notes

Title: Structures and Macros


1
Structures and Macros
  • Chapter 8

2
Structures
  • Template or pattern that can be overlayed on an
    area of memory
  • Multiple fields of different types
  • May initialize fields to default values
  • Structure definition
  • STUNUMBER_SIZE 7
  • LASTNAME_SIZE 20
  • typStudent struc
  • IdNum db STUNUMBER_SIZE 1 dup(0)
  • Lastname db LASTNAME_SIZE 1 dup(0)
  • Credits dw ?
  • Status db ?
  • typStudent ends

3
Declaring a Structure Variable
  • Declare a typStudent Variable called srec
  • .data
  • srec typStudent ltgt
  • You can override default values when declaring a
    structure variable
  • .data
  • srec typStudent lt1234,Jones,32,0gt
  • myRec typStudent lt,,50,0gt

4
Using a Structure Variable
  • Refer to individual field offsets by name
  • .data
  • mov dl, srec.IdNum
  • mov ax, srec.Credits
  • mov ax, bx.Credits
  • mov dl, srecsi.Status
  • (the last two examples require the use of the /Zm
    option when using ML.EXE)
  • Alternate notation (for ML.EXE without /Zm)
  • mov ax, (typStudent PTR bx).Credits

5
Student Structure Demo Program
  • Array of typStudent Structures
  • Demonstrates input/output operations on
    individual fields.
  • Program listing struc.asm

6
Introducing Macros
  • Symbolic name given to one or more assembly
    language statements
  • A macro is invoked, not called. A copy of the
    macro is inserted directly into the program.
  • The mPutchar macro (no parameters)
  • mPutchar macro
  • mov ah,2
  • int 21h
  • endm

7
Macros with Parameters
  • Parameters make a macro more flexible, usable in
    different situations
  • A macro is invoked, not called. A copy of the
    macro is inserted directly into the program.
  • The mPutchar macro (with parameter)
  • mPutchar macro char
  • push ax
  • push dx
  • mov ah,2
  • mov dl,char
  • int 21h
  • pop dx
  • pop ax
  • endm

8
Defining a Macro
  • Macro definition syntax
  • macroname macro parameter-1, parameter-2
  • statement-list
  • endm
  • Required parameters
  • mGotoRowCol macro rowREQ, columnREQ
  • The mDisplayStr macro writes a string to standard
    output
  • The mGotoRowCol macro locates the cursor on a
    specific row/column on the screen.

9
Macros that Allocate Storage
  • Macro definition syntax
  • macroname macro parameter-1, parameter-2
  • statement-list
  • endm
  • Macro definition
  • mAlloc macro varname,numbytes
  • varname db numbytes( 0,0,0,0)
  • endm
  • Invoking the Macro
  • mAlloc value1,20
  • Generated Code
  • value1 db 20 dup( ,0,0,0,0)

10
Creating Local Labels
  • Local labels permit the assembler to generate a
    unique label each time the macro is invoked
  • mRepeat macro char, count
  •     local L1
  •     mov  cx,count
  • L1 mov  ah,2
  •    mov  dl,char
  •    int  21h
  •    loop  L1
  • endm

11
Nested Macros
  • A macro containing other macros is called a
    nested macro
  • mDisplayRowCol macro row,col,string
  •   mGotoRowCol row,col
  •   mDisplayStr string
  • endm
  • Sample call
  • .data
  • greeting db "Hello from row 10, column 15."
  • .code
  • mDisplayRowCol 10, 15, greeting

12
Using a Macro to Call a Procedure
  • A macro makes a good shell for calling
    procedures. For example, the mWriteInt macro
    calls the Writeint procedure
  • mWriteint macro value, radix
  • push ax
  • push bx
  • mov ax,value
  • mov bx,radix
  • call Writeint
  • pop bx
  • pop ax
  • endm

13
Conditional-Assembly Directives
  • Checking for missing macro arguments
  • mWriteint macro value,radix
  • ifb ltvaluegt if blank,
  • exitm exit macro
  • endif
  • push ax
  • push bx
  • ifb ltradixgt
  • mov bx,10 default radix
  • endif
  • mov ax,value
  • (etc.)

14
Displaying Messages During Assembly
  • Write a string to standard output during assembly
  • Useful for identifying invalid macro arguments.
    For example,
  • out Invalid parameter passed to macro

15
Substitute Operator ()
  • Concatenates text in the macro with a macro
    argument
  • mDOSmsg macro example
  • mDOSmsg macro num,string
  • msgnum db DOS error string, 0
  • endm
  • Invoke the macro
  • mDOSmsg 1,ltInvalid functiongt
  • Generated code
  • msg1 db DOS error Invalid function,0

16
Expression Operator ()
  • Permits the calculated value of an expression to
    be passed as a macro argument
  • The value, not the expression itself, is passed
  • mDirectVideo Example
  • mDirectVideo ((row 160) (col 2)), string1

17
Literal-Text Operator ( ltgt )
  • Ties together a string of characters
  • Can be used to pass a single macro argument
  • Example containing embedded characters
  • ltEfficiency is 50, fallinggt

18
A Simple Macro Library
  • mWriteLiteral (write string literal)
  • mCondCall (conditional Call)
  • mCompJmp (compare and jump)
  • mMult16 (memory multiply/16)
  • mMove (memory to memory move)
  • mLongLoop (long loop)

19
REPT, IRP, and IRPC Directives
  • REPT repeats a block of statements based on a
    counter
  • Can be used to create repeated data structures
  • IRP creates a repeate block in which each
    repetition contains a different value
  • IRP parameter,ltargument ,argumentgt
  • statements
  • ENDM

20
Linked List Example
  • ListNode struc
  • NodeData dw ? the node's data
  • NextPtr dw ? pointer to next node
  • ends
  • .data
  • COUNT 0
  • NUMBER_OF_NODES 50
  • Create multiple ListNode instances
  • LinkedList label word
  • rept NUMBER_OF_NODES
  • COUNT COUNT 1
  • ListNode lt COUNT, ( 2) gt
  • endm

21
Extended Jump Macro
  • Uses the IRP Directive to generate macros on an
    as needed basis
  • irp cond,lta,ae,b,be,e,ne,z,nz,g,ge,l,le,c, \
  • nc,o,no,p,np,s,nsgt
  • jxcond macro dest
  • local L1,L2
  • jcond L1 condition true?
  • jmp short L2 no exit
  • L1 jmp dest yes jump to destination
  • L2
  • endm
  • endm

22
The Texts MACRO Library
  • Use this macro library by copying it to your
    programs directory and placing the following
    line before the .data directive
  • INCLUDE MACROS.INC
  • Listing of the MACROS.INC file
Write a Comment
User Comments (0)
About PowerShow.com