Chapt 10 PartIIbw
Chapt 10 PartIIbw
• Introducing Macros
• Defining Macros
• Invoking Macros
• Macro Examples
• Nested Macros
• Example Program: Wrappers
.code
mNewLine ; invoke the macro
.code
Invocation: mPutchar 'A'
1 push eax
1 mov al,'A' viewed in the
Expansion: 1 call WriteChar listing file
1 pop eax
macro
text
invocation
statement
consists
passes of
assembly
code
argument
generates
replaces
declared
parameter macro
inside
1 push edx
1 mov edx,OFFSET str1
1 call WriteString
1 pop edx
.code
mPutchar 1234h
1 push eax
1 mov al,1234h ; error!
1 call WriteChar
1 pop eax
.code
mPutchar
1 push eax
1 mov al,
1 call WriteChar
1 pop eax
2 .data
2 ??0002 BYTE "My Sample Macro Program",0
2 .code
2 push edx
2 mov edx,OFFSET ??0002
2 call Writestring
2 pop edx
1 call Crlf
nesting level
Irvine, Kip R. Assembly Language for Intel-Based Computers, 2003. 17
Your turn . . .
.data
acctNum BYTE 30 DUP(?)
.code
main proc
mAskForString 5,10,"Input Account Number: ", \
acctNum
Solution . . .
IF boolean-expression
statements
[ELSE
statements]
ENDIF
IF RealMode EQ 1
mov ax,@data
mov ds,ax
ENDIF
Macro expansion:
tempStr BYTE " EDX=",0
1 push edx
1 mov dl,50
1 mov dh,7
1 call Gotoxy
1 pop edx
The first macro call passes three arguments. The second call
passes a single argument:
IF IsDefined( RealMode )
mov ax,@data
mov ds,ax
ENDIF
• WHILE Directive
• REPEAT Directive
• FOR Directive
• FORC Directive
• Example: Linked List
WHILE constExpression
statements
ENDM
.data
val1 = 1
val2 = 1
DWORD val1 ; first two values
DWORD val2
val3 = val1 + val2
REPEAT constExpression
statements
ENDM
iVal = 10
REPEAT 100
DWORD iVal
iVal = iVal + 10
ENDM
rows = 10
columns = 5
.data
iVal = 10
REPEAT rows * columns
DWORD iVal
iVal = iVal + 10
ENDM
FOR parameter,<arg1,arg2,arg3,...>
statements
ENDM
Window STRUCT
FOR color,<frame,titlebar,background,foreground>
color DWORD ?
ENDM
Window ENDS
Window STRUCT
frame DWORD ?
titlebar DWORD ?
Generated code: background DWORD ?
foreground DWORD ?
Window ENDS
FORC code,<ABCDEFG>
Group_&code WORD ?
ENDM
Group_A WORD ?
Group_B WORD ?
Group_C WORD ?
Generated code: Group_D WORD ?
Group_E WORD ?
Group_F WORD ?
Group_G WORD ?
ListNode STRUCT
NodeData DWORD ? ; the node's data
NextPtr DWORD ? ; pointer to next node
ListNode ENDS
TotalNodeCount = 15
NULL = 0
Counter = 0
offset contents
00000000 00000001
00000008 NextPtr
00000008 00000002
00000010
00000010 00000003
00000018
00000018 00000004
00000020
00000020 (etc.)
1
2
3
4
5
6
7
Sample output: 8
9
10
11
12
13
14
15