-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacros.asm
5530 lines (4910 loc) · 170 KB
/
macros.asm
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
comment * -----------------------------------------------------------------
Preprocessor code for high level language simulation in MASM32
Updated 2nd December 2011
---------------------------------------------------------------- *
; *******************************************************************
; The following block of macros are macro functions that are designed
; to be called by other macros. In part they function as a library of
; components for writing other macros without having to repeatedly
; reproduce the same capacity. Effectively macro code reuse.
; *******************************************************************
; -----------------------------------------------------------
; This macro replaces quoted text with a DATA section OFFSET
; and returns it in OFFSET "name" format. It is used by other
; macros that handle optional quoted text as a parameter.
; NOTE that while this macro behaves identically on single
; byte characters, it now supports 2 byte characters if the
; __UNICODE__ equate is set.
; -----------------------------------------------------------
reparg MACRO arg
LOCAL nustr
LOCAL quot
quot SUBSTR <arg>,1,1
IFIDN quot,<"> ;; if 1st char = "
IFNDEF __UNICODE__
.data
nustr db arg,0 ;; write arg to .DATA section
.code
EXITM <OFFSET nustr> ;; append name to OFFSET operator
ELSE
EXITM <uni$(arg)> ;; use the "uni$()" macro
ENDIF
ELSE
EXITM <arg> ;; else return arg
ENDIF
ENDM
; -------------------------------------
; variation returns address in register
; so it can be assigned to a variable.
; -------------------------------------
repargv MACRO arg
LOCAL nustr
quot SUBSTR <arg>,1,1
IFIDN quot,<"> ;; if 1st char = "
.data
nustr db arg,0 ;; write arg to .DATA section
.code
mov eax, OFFSET nustr
EXITM <eax> ;; return data section offset in eax
ELSE
mov eax, arg
EXITM <eax> ;; else return arg
ENDIF
ENDM
; -----------------------------------------------------------
; replace a quoted string with its OFFSET in the data section
; -----------------------------------------------------------
repargof MACRO arg
LOCAL nustr
quot SUBSTR <arg>,1,1
IFIDN quot,<"> ;; if 1st char = "
.data
nustr db arg,0 ;; write arg to .DATA section
.code
EXITM <OFFSET nustr> ;; append name to OFFSET operator
ELSE
EXITM <arg> ;; else return arg
ENDIF
ENDM
; -------------------------------------------------------
; This is a parameter checking macro. It is used to test
; if a parameter in a macro is a quoted string when a
; quoted string should not be used as a parameter. If it
; is a user defined error message is displayed at
; assembly time so that the error can be fixed.
; -------------------------------------------------------
tstarg MACRO arg
quot SUBSTR <arg>,1,1
IFIDN quot,<"> ;; if 1st char = "
% echo *****************
% echo QUOTED TEXT ERROR
% echo *****************
% echo argument = arg
% echo valid memory buffer address required
% echo *****************
.ERR
EXITM <arg>
ELSE
EXITM <arg> ;; else return arg
ENDIF
ENDM
; -----------------------------------------------
; count the number of arguments passed to a macro
; This is a slightly modified 1990 MASM 6.0 macro
; -----------------------------------------------
argcount MACRO args:VARARG
LOCAL cnt
cnt = 0
FOR item, <args>
cnt = cnt + 1
ENDM
EXITM %cnt ;; return as a number
ENDM
; ---------------------------------------------------
; return an arguments specified in "num" from a macro
; argument list or "-1" if the number is out of range
; ---------------------------------------------------
getarg MACRO num:REQ,args:VARARG
LOCAL cnt, txt
cnt = 0
FOR arg, <args>
cnt = cnt + 1
IF cnt EQ num
txt TEXTEQU <arg> ;; set "txt" to content of arg num
EXITM
ENDIF
ENDM
IFNDEF txt
txt TEXTEQU <-1> ;; return -1 if num out of range
ENDIF
EXITM txt
ENDM
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
; -------------
; expand prefix
; -------------
expand_prefix MACRO txtitm
LOCAL prefix1,wrd,nu,varname
prefix1 SUBSTR <txtitm>,1,1
;; usable characters are "&" "*" "@" "#" "?" "^" "~" "`" "/"
IFIDN prefix1,<&> ;; reference operator
nu SUBSTR <txtitm>,2
wrd CATSTR <ADDR >,nu
EXITM <wrd>
ENDIF
IFIDN prefix1,<*> ;; indirection operator
nu SUBSTR <txtitm>,2
.data?
varname dd ?
.code
push ebx
mov ebx, nu
mov ebx, [ebx] ;; dereference variable in EBX
mov varname, ebx
pop ebx
EXITM <varname>
ENDIF
EXITM <txtitm> ;; exit with original argument
ENDM
; ----------------------------------------------------------------
; invoke enhancement. Add quoted text support to any procedure
; or API call by using this macro instead of the standard invoke.
; LIMITATION : quoted text must be plain text only, no ascii
; values or macro reserved characters IE <>!() etc ..
; use chr$() or cfm$() for requirements of this type.
; ----------------------------------------------------------------
fn MACRO FuncName:REQ,args:VARARG
p@arg equ <invoke FuncName> ;; construct invoke and function name
FOR var,<args> ;; loop through all arguments
p@arg CATSTR p@arg,<,expand_prefix(reparg(var))> ;; replace quotes and append p@arg
ENDM
p@arg ;; write the invoke macro
ENDM
; ------------------------------------------------
; Function return value version of the above macro
; ------------------------------------------------
rv MACRO FuncName:REQ,args:VARARG
the@arg equ <invoke FuncName> ;; construct invoke and function name
FOR var,<args> ;; loop through all arguments
the@arg CATSTR the@arg,<,expand_prefix(reparg(var))> ;; replace quotes and append the@arg
ENDM
the@arg ;; write the invoke macro
EXITM <eax> ;; EAX as the return value
ENDM
; ---------------------------------------------------
; The two following versions support C style escapes.
; ---------------------------------------------------
fnc MACRO FuncName:REQ,args:VARARG
the@arg equ <invoke FuncName> ;; construct invoke and function name
FOR var,<args> ;; loop through all arguments
the@arg CATSTR the@arg,<,expand_prefix(cfm$(var))> ;; replace quotes and append the@arg
ENDM
the@arg ;; write the invoke macro
ENDM
rvc MACRO FuncName:REQ,args:VARARG
the@arg equ <invoke FuncName> ;; construct invoke and function name
FOR var,<args> ;; loop through all arguments
the@arg CATSTR the@arg,<,expand_prefix(cfm$(var))> ;; replace quotes and append the@arg
ENDM
the@arg ;; write the invoke macro
EXITM <eax> ;; EAX as the return value
ENDM
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
; -----------------------------------------
; MSVCRT ASCII & UNICODE integer conversion
; -----------------------------------------
ustr$ MACRO number
LOCAL buffer
.data?
buffer TCHAR 40 dup (?)
align 4
.code
IFNDEF __UNICODE__
invoke crt__itoa,number,ADDR buffer,10
ELSE
invoke crt__itow,number,ADDR buffer,10
ENDIF
EXITM <eax>
ENDM
sstr$ MACRO number
LOCAL buffer
.data?
buffer TCHAR 40 dup (?)
align 4
.code
IFNDEF __UNICODE__
invoke crt__ltoa,number,ADDR buffer,10
ELSE
invoke crt__ltow,number,ADDR buffer,10
ENDIF
EXITM <eax>
ENDM
uval MACRO lpstring
IFNDEF __UNICODE__
invoke crt_atoi,reparg(lpstring)
ELSE
invoke crt__wtoi,reparg(lpstring)
ENDIF
EXITM <eax>
ENDM
val equ <uval>
sval MACRO lpstring
IFNDEF __UNICODE__
invoke crt_atol,reparg(lpstring)
ELSE
invoke crt__wtol,reparg(lpstring)
ENDIF
EXITM <eax>
ENDM
; ---------------------------------
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
A2WDAT MACRO quoted@@text,dataname:VARARG
;; ------------------------------------------------------
;; ASCII literal string to UNICODE "dw" conversion.
;; The macro has the ASCII character range 0 - 255
;; and will convert 1 byte characters in the quoted
;; string to 2 byte UNICODE characters written as "dw"
;; in the initialised data section.
;; The final string length is dictated by the MASM line
;; length limit and is set at 240 characters.
;;
;; This MACRO is primarily designed to be called by
;; other macros, it does not create a .DATA section
;; and it does not terminate the string data it writes.
;;
;; This characteristic is so the macro can be called
;; repeatedly by another macro. The calling macro
;; then terminates the string when it has no more
;; to write.
;;
;; The optional dataname label is designed to be used
;; on the first call and ommitted on subsequent calls
;; when adding text to the original label address.
;;
;; You can write a .DATA section entry in this manner.
;;
;; .data
;; A2WDAT "First block of text", mytext
;; A2WDAT "Second block of text"
;; A2WDAT "Third block of text"
;; A2WDAT "Fourth block of text"
;; dw 0
;; .code
;;
;; mov eax, OFFSET mytext
;; ------------------------------------------------------
LOCAL s_l_e_n
LOCAL c_n_t_r
;; LOCAL item
LOCAL add@str1
LOCAL isquot
LOCAL argz
LOCAL lcnt
LOCAL char
LOCAL cntr
LOCAL new@str1
LOCAL slice@1
LOCAL slice@2
LOCAL slice@3
LOCAL slice@4
LOCAL slice@5
LOCAL slice@6
add@str1 equ <>
new@str1 equ <>
slice@1 equ <>
slice@2 equ <>
slice@3 equ <>
slice@4 equ <>
slice@5 equ <>
slice@6 equ <>
s_l_e_n SIZESTR <quoted@@text>
;; item TEXTEQU %(s_l_e_n)
;; % echo string length = item characters
if s_l_e_n gt 240
echo ------------------------------------------
echo *** STRING EXCEEDS 240 character limit ***
echo ------------------------------------------
.ERR
EXITM
endif
isquot SUBSTR <quoted@@text>,1,1
IFDIF isquot,<">
echo -----------------------------
echo *** MISSING LEADING QUOTE ***
echo -----------------------------
.ERR
EXITM
ENDIF
isquot SUBSTR <quoted@@text>,s_l_e_n,1
IFDIF isquot,<">
echo ------------------------------
echo *** MISSING TRAILING QUOTE ***
echo ------------------------------
.ERR
EXITM
ENDIF
;; ============================================
lcnt SIZESTR <quoted@@text>
lcnt = lcnt - 2
cntr = 2
c_n_t_r = 0
:lpstart
argz SUBSTR <quoted@@text>,cntr,1
if c_n_t_r lt 1
slice@1 CATSTR slice@1,<">,argz,<">
goto nxt
elseif c_n_t_r lt 40
slice@1 CATSTR slice@1,<,">,argz,<">
goto nxt
elseif c_n_t_r lt 41
slice@2 CATSTR slice@2,<">,argz,<">
goto nxt
elseif c_n_t_r lt 80
slice@2 CATSTR slice@2,<,">,argz,<">
goto nxt
elseif c_n_t_r lt 81
slice@3 CATSTR slice@3,<">,argz,<">
goto nxt
elseif c_n_t_r lt 120
slice@3 CATSTR slice@3,<,">,argz,<">
goto nxt
elseif c_n_t_r lt 121
slice@4 CATSTR slice@4,<">,argz,<">
goto nxt
elseif c_n_t_r lt 160
slice@4 CATSTR slice@4,<,">,argz,<">
goto nxt
elseif c_n_t_r lt 161
slice@5 CATSTR slice@5,<">,argz,<">
goto nxt
elseif c_n_t_r lt 200
slice@5 CATSTR slice@5,<,">,argz,<">
goto nxt
elseif c_n_t_r lt 201
slice@6 CATSTR slice@6,<">,argz,<">
goto nxt
elseif c_n_t_r lt 240
slice@6 CATSTR slice@6,<,">,argz,<">
goto nxt
endif
:nxt
c_n_t_r = c_n_t_r + 1
cntr = cntr + 1
lcnt = lcnt - 1
if lcnt ne 0
goto lpstart
endif
;; ============================================
;; ---------------------------------------------------------
;; add a label if one is supplied else add a normal DW entry
;; ---------------------------------------------------------
IFDIF <dataname>,<>
% s_l_e_n SIZESTR <slice@1>
if s_l_e_n ne 0
slice@1 CATSTR <dataname dw >,slice@1
slice@1
endif
ELSE
% s_l_e_n SIZESTR <slice@1>
if s_l_e_n ne 0
slice@1 CATSTR <dw >,slice@1
slice@1
endif
ENDIF
;; ---------------------------------------------------------
% s_l_e_n SIZESTR <slice@2>
if s_l_e_n ne 0
slice@2 CATSTR <dw >,slice@2
slice@2
endif
% s_l_e_n SIZESTR <slice@3>
if s_l_e_n ne 0
slice@3 CATSTR <dw >,slice@3
slice@3
endif
% s_l_e_n SIZESTR <slice@4>
if s_l_e_n ne 0
slice@4 CATSTR <dw >,slice@4
slice@4
endif
% s_l_e_n SIZESTR <slice@5>
if s_l_e_n ne 0
slice@5 CATSTR <dw >,slice@5
slice@5
endif
% s_l_e_n SIZESTR <slice@6>
if s_l_e_n ne 0
slice@6 CATSTR <dw >,slice@6
slice@6
endif
ENDM
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
WSTR MACRO lblname,arglist:VARARG
LOCAL qflg ;; quote flag
LOCAL isqt ;; 1st character
LOCAL arg ;; FOR loop argument
qflg = 0 ;; clear quote flag
.data ;; write data to the DATA section
for arg, <arglist>
isqt SUBSTR <arg>,1,1 ;; get 1st character
IFIDN isqt,<"> ;; test if its a quote
IF qflg eq 0 ;; if 1st arg, add label
A2WDAT arg,lblname ;; write data section first entry
ENDIF
IF qflg eq 1 ;; else just write data
A2WDAT arg ;; write subsequent entry
ENDIF
ENDIF
IFDIF isqt,<"> ;; if not quoted
IF qflg eq 0 ;; if 1st arg, add label
lblname dw arg ;; write data section first entry as DW number
ENDIF
IF qflg eq 1 ;; if 1st arg, add label
dw arg ;; write subsequent entry as DW number
ENDIF
ENDIF
qflg = 1 ;; set flag for non 1st char
ENDM
dw 0 ;; terminate data entry
align 4 ;; 4 byte align after terminator
.code ;; change back to CODE section
ENDM
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
uni$ MACRO arglist:VARARG
LOCAL DATA@NAME
WSTR DATA@NAME,arglist
EXITM <OFFSET DATA@NAME>
ENDM
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
; --------------------------------
; initialised GLOBAL string value
; --------------------------------
; -------------------------------------------------------
; The dataname passed to STRING is addressed as an OFFSET
; mov eax, OFFSET data_label
; -------------------------------------------------------
STRING MACRO data_label,quoted_text:VARARG
IFNDEF __UNICODE__
.data
data_label db quoted_text,0
align 4
.code
ELSE
WSTR data_label,quoted_text,0
ENDIF
ENDM
; -------------------------------------------------------------------
; The dataname passed to STRADD is addressed as a POINTER to the data
; mov eax, data_label
; -------------------------------------------------------------------
STRADD MACRO data_label,args:VARARG
LOCAL dataname
IFNDEF __UNICODE__
.data
dataname db args
align 4
data_label dd dataname
.code
ELSE
WSTR dataname,args
.data
align 4
data_label dd dataname
.code
ENDIF
ENDM
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
; ********************************************************
; format a C style string complete with escape characters
; and return the offset of the result to the calling macro
;
; 3 versions are presented here,
; 1. acfm$ = ASCII only version
; 2. ucfm$ = UNICODE only version
; 3. cfm$ = Either ASCII or UNICODE depending on the
; __UNICODE__ equate being present in the source file
;
; This allows you to force either ASCII, UNICODE or either
; depending on the presence of the __UNICODE__ equate
; ********************************************************
; ********************************************************
; branchless ASCII version of cfm$ with no ELSE clauses.
; ********************************************************
acfm$ MACRO txt:VARARG
LOCAL ch1,char,nu$,tmp,flag,lbuf,rbuf,cpos,sln
ch1 equ <>
nu$ equ <>
flag = 0
ch1 SUBSTR <txt>,1,1 ;; check if 1st character is a quote
IFDIF ch1,<">
EXITM <txt> ;; exit with original "txt" if it is not
ENDIF
FORC char,<txt> ;; scan through characters in "txt"
IFIDN <char>,<\> ;; increment the flag if "\" escape character
flag = flag + 1
ENDIF
; -----------------------------------------------
IF flag EQ 0 ;; <<< if flag = 0 then normal APPEND character
nu$ CATSTR nu$,<char>
ENDIF
IF flag EQ 1 ;; <<< if flag = 1 then perform replacement
IFIDN <char>,<n>
nu$ CATSTR nu$,<",13,10,"> ;; \n = CRLF
flag = 0
ENDIF
IFIDN <char>,<t>
nu$ CATSTR nu$,<",9,"> ;; \t = TAB
flag = 0
ENDIF
IFIDN <char>,<q>
nu$ CATSTR nu$,<",34,"> ;; \q = quote
flag = 0
ENDIF
IFIDN <char>,<0>
nu$ CATSTR nu$,<",0,"> ;; \0 = embedded zero
flag = 0
ENDIF
;; ---------------------
;; masm specific escapes
;; ---------------------
IFIDN <char>,<l>
nu$ CATSTR nu$,<",60,"> ;; \l = <
flag = 0
ENDIF
IFIDN <char>,<r>
nu$ CATSTR nu$,<",62,"> ;; \r = >
flag = 0
ENDIF
IFIDN <char>,<x>
nu$ CATSTR nu$,<",33,"> ;; \x = !
flag = 0
ENDIF
IFIDN <char>,<a>
nu$ CATSTR nu$,<",40,"> ;; \a = (
flag = 0
ENDIF
IFIDN <char>,<b>
nu$ CATSTR nu$,<",41,"> ;; \b = )
flag = 0
ENDIF
ENDIF
IF flag EQ 2 ;; <<< if flag = 2 APPEND the "\" character
IFIDN <char>,<\>
nu$ CATSTR nu$,<",92,"> ;; \\ = \
flag = 0
ENDIF
ENDIF
; -----------------------------------------------
ENDM
;; ---------------------------------------------
;; strip any embedded <"",> characters sequences
;; ---------------------------------------------
nu$ CATSTR nu$,<,0,0,0> ;; append trailing zeros
cpos INSTR nu$,<"",> ;; test for leading junk
IF cpos EQ 1
nu$ SUBSTR nu$,4 ;; chomp off any leading junk
ENDIF
cpos INSTR nu$,<"",>
WHILE cpos
lbuf SUBSTR nu$,1,cpos-1 ;; read text before junk
rbuf SUBSTR nu$,cpos+3 ;; read text after junk
nu$ equ <> ;; clear nu$
nu$ CATSTR lbuf,rbuf ;; concantenate the two
cpos INSTR nu$,<"",> ;; reload cpos for next iteration
ENDM
sln SIZESTR nu$
nu$ SUBSTR nu$,1,sln-6 ;; trim off tail padding
.data
tmp db nu$,0
align 4
.code
EXITM <OFFSET tmp> ;; return the DATA section OFFSET
ENDM
; **********************************************************
; branchless UNICODE version of cfm$ with no ELSE clauses.
; **********************************************************
ucfm$ MACRO txt:VARARG
LOCAL ch1,char,nu$,tmp,flag,lbuf,rbuf,cpos,sln
ch1 equ <>
nu$ equ <>
flag = 0
ch1 SUBSTR <txt>,1,1 ;; check if 1st character is a quote
IFDIF ch1,<">
EXITM <txt> ;; exit with original "txt" if it is not
ENDIF
FORC char,<txt> ;; scan through characters in "txt"
IFIDN <char>,<\> ;; increment the flag if "\" escape character
flag = flag + 1
ENDIF
; -----------------------------------------------
IF flag EQ 0 ;; <<< if flag = 0 then normal APPEND character
nu$ CATSTR nu$,<char>
ENDIF
IF flag EQ 1 ;; <<< if flag = 1 then perform replacement
IFIDN <char>,<n>
nu$ CATSTR nu$,<",13,10,"> ;; \n = CRLF
flag = 0
ENDIF
IFIDN <char>,<t>
nu$ CATSTR nu$,<",9,"> ;; \t = TAB
flag = 0
ENDIF
IFIDN <char>,<q>
nu$ CATSTR nu$,<",34,"> ;; \q = quote
flag = 0
ENDIF
IFIDN <char>,<0>
nu$ CATSTR nu$,<",0,"> ;; \0 = embedded zero
flag = 0
ENDIF
;; ---------------------
;; masm specific escapes
;; ---------------------
IFIDN <char>,<l>
nu$ CATSTR nu$,<",60,"> ;; \l = <
flag = 0
ENDIF
IFIDN <char>,<r>
nu$ CATSTR nu$,<",62,"> ;; \r = >
flag = 0
ENDIF
IFIDN <char>,<x>
nu$ CATSTR nu$,<",33,"> ;; \x = !
flag = 0
ENDIF
IFIDN <char>,<a>
nu$ CATSTR nu$,<",40,"> ;; \a = (
flag = 0
ENDIF
IFIDN <char>,<b>
nu$ CATSTR nu$,<",41,"> ;; \b = )
flag = 0
ENDIF
ENDIF
IF flag EQ 2 ;; <<< if flag = 2 APPEND the "\" character
IFIDN <char>,<\>
nu$ CATSTR nu$,<",92,"> ;; \\ = \
flag = 0
ENDIF
ENDIF
; -----------------------------------------------
ENDM
;; ---------------------------------------------
;; strip any embedded <"",> characters sequences
;; ---------------------------------------------
nu$ CATSTR nu$,<,0,0,0> ;; append trailing zeros
cpos INSTR nu$,<"",> ;; test for leading junk
IF cpos EQ 1
nu$ SUBSTR nu$,4 ;; chomp off any leading junk
ENDIF
cpos INSTR nu$,<"",>
WHILE cpos
lbuf SUBSTR nu$,1,cpos-1 ;; read text before junk
rbuf SUBSTR nu$,cpos+3 ;; read text after junk
nu$ equ <> ;; clear nu$
nu$ CATSTR lbuf,rbuf ;; concantenate the two
cpos INSTR nu$,<"",> ;; reload cpos for next iteration
ENDM
sln SIZESTR nu$
nu$ SUBSTR nu$,1,sln-6 ;; trim off tail padding
% WSTR tmp,nu$
EXITM <OFFSET tmp> ;; return the DATA section OFFSET
ENDM
; ****************************************************
; ****************************************************
cfm$ MACRO txt:VARARG
IFDEF __UNICODE__
EXITM <ucfm$(txt)> ;; UNICODE only version
ENDIF
EXITM <acfm$(txt)> ;; ASCII only version
ENDM
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
; --------------------------------------------------------------
; This macro is written to behave as closely as possible to the
; C runtime function "printf". The lack of return value is
; to allow the closest method to writing C code. It supports
; both ASCII and UNICODE and uses the C runtime function
; "wprintf" to provide the UNICODE support.
;
; printf("%d\t%d\t%Xh\n", 123, 456, 1024);
;
; The return value is available in the EAX register if required.
; The original ASCII version was written by Michael Webster.
; --------------------------------------------------------------
printf MACRO format:REQ, args:VARARG
IFNDEF __UNICODE__
IFNB <args>
fn crt_printf, cfm$(format), args
ELSE
fn crt_printf, cfm$(format)
ENDIF
EXITM <>
ELSE
IFNB <args>
fn crt_wprintf, cfm$(format), args
ELSE
fn crt_wprintf, cfm$(format)
ENDIF
EXITM <>
ENDIF
ENDM
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
comment * -------------------------------------------------------
Each of the following macros has its own dedicated 260
CHARACTER buffer. The OFFSET returned by each macro can be
used directly in code but if the macro is called again
the data in the dedicated buffer will be overwritten
with the new result.
mov str1, ptr$(buffer)
mov str2, pth$()
invoke szCopy str2,str1 ; ASCII
invoke ucCopy str2,str1 ; UNICODE
or the macro
cst str2, str1 ; __UNICODE__ aware
Empty brackets should be used with these macros as they
take no parameters. pth$() CurDir$() etc ...
------------------------------------------------------- *
pth$ MACRO ;; application path OFFSET returned
IFNDEF pth__equate__flag
.data?
pth__260_CHAR__buffer TCHAR MAX_PATH dup (?)
.code
pth__equate__flag equ <1>
ENDIF
IFNDEF __UNICODE__
invoke GetAppPath,ADDR pth__260_CHAR__buffer
ELSE
invoke GetAppPathW
ENDIF
EXITM <eax>
ENDM
CurDir$ MACRO
IFNDEF cdir__equate__flag
.data?
cdir__260_CHAR__buffer TCHAR MAX_PATH dup (?)
.code
cdir__equate__flag equ <1>
ENDIF
invoke GetCurrentDirectory,MAX_PATH,ADDR cdir__260_CHAR__buffer
mov eax, OFFSET cdir__260_CHAR__buffer
EXITM <eax>
ENDM
SysDir$ MACRO
IFNDEF sys__equate__flag
.data?
sysdir__260_CHAR__buffer TCHAR MAX_PATH dup (?)
.code
sys__equate__flag equ <1>
ENDIF
invoke GetSystemDirectory,ADDR sysdir__260_CHAR__buffer,MAX_PATH
mov eax, OFFSET sysdir__260_CHAR__buffer
EXITM <eax>
ENDM
WinDir$ MACRO
IFNDEF wdir__equate__flag
.data?
windir__260_CHAR__buffer TCHAR MAX_PATH dup (?)
.code
wdir__equate__flag equ <1>
ENDIF
invoke GetWindowsDirectory,ADDR windir__260_CHAR__buffer,MAX_PATH
mov eax, OFFSET windir__260_CHAR__buffer
EXITM <eax>
ENDM
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
; ******************************************
; DOS style directory manipulation macros *
; The parameters passed to these directory *
; macros should be zero terminated string *
; addresses. *
; ******************************************
chdir MACRO pathname
invoke SetCurrentDirectory,reparg(pathname)
ENDM
CHDIR equ <chdir>
mkdir MACRO dirname
invoke CreateDirectory,reparg(dirname),NULL
ENDM
MKDIR equ <mkdir>
rndir MACRO oldname,newname
invoke MoveFile,reparg(oldname),reparg(newname)
ENDM
RNDIR equ <rndir>
rmdir MACRO dirname
invoke RemoveDirectory,reparg(dirname)
ENDM
RMDIR equ <rmdir>
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
ascii MACRO quoted_text:VARARG
LOCAL txtname
.data
txtname db quoted_text,0
.code
EXITM <OFFSET txtname>
ENDM
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
; ******************************************************
; BASIC style conversions from string to 32 bit integer
; ******************************************************
hval MACRO lpstring ; hex string to unsigned 32 bit integer
invoke htodw, reparg(lpstring)
EXITM <eax>
ENDM
; ********************************
; BASIC string function emulation
; ********************************
add$ MACRO lpSource,lpAppend
IFNDEF __UNICODE__
invoke szCatStr,tstarg(lpSource),reparg(lpAppend)
EXITM <eax>
ELSE
push esi
mov esi, tstarg(lpSource)
invoke ucCatStr,tstarg(lpSource),reparg(lpAppend)
mov eax, esi
pop esi
EXITM <eax>
ENDIF
ENDM
append$ MACRO string,buffer,location
IFNDEF __UNICODE__
invoke szappend,string,reparg(buffer),location
ELSE