This repository has been archived by the owner on Jan 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Macros.inc
350 lines (297 loc) · 8.77 KB
/
Macros.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
.NOLIST
COMMENT !
; (Macros.inc) - Include file containing Macros
; for Assembly Language for Intel-Based Computers
; 7th Edition, by Kip Irvine. Copyright 2014, Prentice-Hall Inc,
; all rights reserved.
;
; Update history:
; 1/1/2014 - Revised for 7th Edition
; List of macros in this file:
mDump MACRO varName:REQ, useLabel
-- Displays a variable, using its name and default attributes.
-- useLabel is optional; if nonblank, the variable name will be displayed
-- just before the dump.
mDumpMem MACRO address:REQ, itemCount:REQ, componentSize:REQ
-- Displays a dump of a range of memory.
mGotoxy MACRO X:REQ, Y:REQ
-- Sets the cursor position.
mReadString MACRO varName:REQ
-- Reads a string from the keyboard.
mShow MACRO itsName:REQ, format:=<HIN>
-- Displays a variable or register in various formats.
mShowRegister MACRO regName, regValue
-- Displays a 32-bit register name and contents in hexadecimal.
mWrite MACRO text:REQ
-- Writes a string literal to standard output.
mWriteLn MACRO text:REQ
-- Writes a string literal to standard output. *** DEPRECATED in 5th Edition ***
mWriteSpace MACRO count
-- Writes one or more spaces to the console window.
-- <count> is optional
mWriteString MACRO buffer:REQ
-- Writes a string variable's contents to the console window.
END COMMENT ***************************************** !
; Introduced in Chapter 10 as an example of a Macro function.
IsDefined MACRO symbol
IFDEF symbol
EXITM <-1> ;; True
ELSE
EXITM <0> ;; False
ENDIF
ENDM
;----------------------------------------------------
mDump MACRO varName:REQ, useLabel
;
; Displays a variable, using its known attributes
; Receives: varName, the name of a variable.
; If useLabel is nonblank, the name of the
; variable is displayed.
;----------------------------------------------------
call Crlf
IFNB <useLabel>
mWrite "Variable name: &varName"
ELSE
mWrite " "
ENDIF
mDumpMem OFFSET varName, LENGTHOF varName, TYPE varName
ENDM
;------------------------------------------------------
mDumpMem MACRO address:REQ, itemCount:REQ, componentSize:REQ
;
; Receives: memory offset, count of the number of items
; to display, and the size of each memory component.
; Avoid passing EBX, ECX, and ESI as arguments.
;------------------------------------------------------
push ebx
push ecx
push esi
mov esi,address
mov ecx,itemCount
mov ebx,componentSize
call DumpMem
pop esi
pop ecx
pop ebx
ENDM
;------------------------------------------------------
mGotoxy MACRO X:REQ, Y:REQ
;
; Sets the cursor position.
; Receives: X and Y coordinates (type BYTE). Avoid
; passing DH and DL as arguments.
;------------------------------------------------------
push edx
mov dh,Y
mov dl,X
call Gotoxy
pop edx
ENDM
;------------------------------------------------------
mReadString MACRO varName:REQ
;
; Reads from standard input into a buffer.
; Receives: the name of the buffer. Avoid passing
; ECX and EDX as arguments.
;------------------------------------------------------
push ecx
push edx
mov edx,OFFSET varName
mov ecx,SIZEOF varName
call ReadString
pop edx
pop ecx
ENDM
;---------------------------------------------------
mShow MACRO itsName:REQ, format:=<HIN>
LOCAL tempStr
; Displays a register's or variable's name and contents.
;
; Receives:
; itsName is the name of a register or variable.
; format is an ordered string consisting of the format codes:
; H - hexadecimal
; D - unsigned decimal
; I - signed decimal
; B - binary
; N - append a newline (CR/LF) (may appear multiple times)
;
; The default format is "HIN"
;
; The variable or register's value is displayed in each format,
; in the order specified by the caller, on a single output line.
; Create by Gerald Cahill and Kip Irvine.
;---------------------------------------------------
.data
tempStr BYTE " &itsName = ",0
.code
pushad
;;If itsName is a register name, define its type
IF (OPATTR (itsName)) AND 00010000b ;;true if register name
MSHOWITSNAMETYPE = 0 ;; initialize TYPE to not found
FOR reg8,<al,ah,bl,bh,cl,ch,dl,dh>
IFIDNI <itsName>,<reg8>
MSHOWITSNAMETYPE = 1
movzx ecx,itsName ;; get unsigned value
movsx edx,itsName ;; get signed value
ENDIF
ENDM
FOR reg16,<ax,bx,cx,dx,si,di,bp,sp>
IFIDNI <itsName>,<reg16>
MSHOWITSNAMETYPE = 2
movzx ecx,itsName ;; get unsigned value
movsx edx,itsName ;; get signed value
ENDIF
ENDM
FOR regseg,<cs,ds,es,fs,gs,ss>
IFIDNI <itsName>,<regseg>
MSHOWITSNAMETYPE = 2
mov ax,itsName ;; get value into general purpose reg.
movsx edx,ax ;; get signed value (who would want it?)
movzx ecx,ax ;; get unsigned value
ENDIF
ENDM
FOR reg32,<eax,ebx,ecx,edx,esi,edi,ebp,esp>
IFIDNI <itsName>,<reg32>
MSHOWITSNAMETYPE = 4
mov ecx,itsName ;; get unsigned value
mov edx,itsName ;; get signed value
ENDIF
ENDM
ELSE ;; itsName is not a register name, assume variable name
MSHOWITSNAMETYPE = TYPE itsName
IF MSHOWITSNAMETYPE EQ 4
mov ecx,itsName ;; get unsigned value
mov edx,ecx ;; get signed value
ELSE
movzx ecx,itsName ;; get unsigned value
movsx edx,itsName ;; get signed value
ENDIF
ENDIF ;;OPATTR
;; Display the register or variable's name
push edx
mov edx,OFFSET tempStr
call WriteString
pop edx
;; Display the register or variable's contents
FORC fmt,<format>
IFIDNI <fmt>,<H> ;; H - write unsigned hex
mov eax,ecx ;; get unsigned
mov ebx,MSHOWITSNAMETYPE
call WriteHexB ;; write in hexadecimal
mWrite "h "
ENDIF
IFIDNI <fmt>,<D> ;; D - write unsigned dec
mov eax,ecx ;; get unsigned
call WriteDec
mWrite "d "
ENDIF
IFIDNI <fmt>,<I> ;; I - write signed Integer
mov eax,edx ;; get signed
call WriteInt
mWrite "d "
ENDIF
IFIDNI <fmt>,<B> ;; B - write unsigned binary
mov eax,ecx ;; get unsigned
mov ebx,MSHOWITSNAMETYPE
call WriteBinB ;; display binary
mWrite "b "
ENDIF
IFIDNI <fmt>,<N> ;; N - write new line
call Crlf
ENDIF
ENDM ;end FORC
popad
ENDM ;; end mShow macro
;---------------------------------------------------
mShowRegister MACRO regName, regValue
LOCAL tempStr
;
; Displays a 32-bit register name and contents.
; Receives: the register name, the register value.
;---------------------------------------------------
.data
tempStr BYTE " ®Name=",0
.code
push eax
; Display the register name
push edx
mov edx,OFFSET tempStr
call WriteString
pop edx
; Display the register contents
mov eax,regValue
call WriteHex
pop eax
ENDM
;------------------------------------------------------
mWrite MACRO text:REQ
;
; Writes a string literal to standard output.
; Receives: a string enclosed in single or double
; quotes (null terminator not required).
;------------------------------------------------------
LOCAL string
.data ;; local data
string BYTE text,0 ;; define the string
.code
push edx
mov edx,OFFSET string
call WriteString
pop edx
ENDM
;------------------------------------------------------
mWriteLn MACRO text:REQ
;
; Writes a string literal to standard output, followined by Crlf
; Receives: a string enclosed in single or double
; quotes (null terminator not required).
; DEPRECATED in the Fifth edition.
;------------------------------------------------------
mWrite text
call Crlf
ENDM
;------------------------------------------------------
mWriteSpace MACRO count:=<1>
;
; Writes one or more spaces to standard output.
; Receives: an integer specifying the number of spaces.
; If count is blank, a single space is written.
;------------------------------------------------------
LOCAL spaces
.data
spaces BYTE count DUP(' '),0
.code
push edx
mov edx,OFFSET spaces
call WriteString
pop edx
ENDM
;------------------------------------------------------
mWriteString MACRO buffer:REQ
;
; Writes a string variable to standard output.
; Receives: string variable name.
;------------------------------------------------------
push edx
mov edx,OFFSET buffer
call WriteString
pop edx
ENDM
Startup MACRO
IF IsDefined( RealMode )
mov ax,@data
mov ds,ax
ENDIF
ENDM
;----------------------------------------------------------------------
; We may later decide to add some macros that are specific to 32-bit
; mode, or to 16-bit mode. The @MODEL value is defined either by
; Irvine16.inc or by Irvine32.inc, when they use the .MODEL directive.
;----------------------------------------------------------------------
IFDEF @MODEL ; @MODEL defined?
IF @MODEL EQ 7 ; 32-bit unique macros begin here...
ELSE ; 16-bit unique macros begin here...
ENDIF ; IF @MODEL...
ENDIF ; IFNDEF @MODEL
.LIST