-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnasm.mac
1826 lines (1679 loc) · 36.1 KB
/
nasm.mac
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
[list -]
;
; General purpose NASM macros.
;
; Jason Hood, April 2005. Public Domain.
;
;
; =====================
; Suffixed Instructions
; =====================
;
; Most instructions that address memory can use an AT&T-style suffix to supply
; the size specifier. Append 'b' for byte, 'w' for word and 'd' for dword.
;
; movb [bx], 0 ==> mov byte [bx], 0
;
; Note: MOV, SHL & SHR use an 'l' suffix for dword, since 'd' conflicts with
; existing instructions.
;
;
; ==========
; Assignment
; ==========
;
; hl: turn two byte values (high, low) into a word value.
; HL: turn two word values (high, low) into a dword value.
;
; mov ax, hl(10, 20) ==> mov ax, 0a14h
; mov eax, HL(10, 20) ==> mov eax, 000a0014h
;
; ld/ldw/ldd: load a register or word/dword via the stack.
;
; ld es, ds ==> push ds
; pop es
;
; ld: load a 32-bit register/memory from a 16-bit register/memory pair.
;
; ld eax, si,di ==> push word si
; push word di
; pop dword eax
;
; ldw: load a 16-bit register/memory pair from a 32-bit register/memory.
;
; ldw si,di, eax ==> push dword eax
; pop word di
; pop word si
;
; swap/swapw/swapd: xchg for segment registers or words/dwords.
;
; swap ds, es ==> push ds
; push es
; pop ds
; pop es
;
; ldhl: load two (16-bit) registers from consecutive memory locations.
;
; ldhl dx,ax, addr ==> mov dx, [addr+2]
; mov ax, [addr]
;
; ldd: load DX:AX (i8086) or EAX from memory.
;
; ldd addr ==> mov dx, [addr+2] mov eax, [addr]
; mov ax, [addr]
;
; lss.: LSS for i8086.
;
; lss. sp, addr ==> mov ss, [addr+2] lss sp, [addr]
; mov sp, [addr]
;
; movzx.: MOVZX for i8086 (AX/BX/CX/DX only).
;
; movzx. ax, [addr] ==> mov al, [addr] movzx ax, [addr]
; mov ah, 0
;
; sthl: store two (16-bit) registers to consecutive memory locations.
;
; sthl ds,dx, addr ==> mov word [addr], dx
; mov [addr+2], ds
;
; sxx: store a segment and (16-bit) register (xx = cs, ds, es, fs, gs, ss).
;
; sds dx, addr ==> mov [addr], dx
; mov [addr+2], ds
;
; mmov: copy memory via a register.
;
; mmov cx, [b], [a] ==> mov cx, [a]
; mov [b], cx
;
; mmov[bwl]: copy memory via AL, AX or EAX.
;
; mmovb [b], [a] ==> mov al, [a]
; mov [b], al
;
; mmovd: if i8086 is defined, uses DX:AX, else EAX; a is optional.
;
; mmovd b, a ==> mov ax, [a] mov eax, [a]
; mov dx, [a+2] mov [b], eax
; mov [b], ax
; mov [b+2], dx
;
; cmov/cmovby/cmovw/cmovd: conditional move.
;
; cmov cc, d, s ==> jncc skip
; mov d, s
; skip:
;
; cmov d, cc, t, f ==> mov d, f
; jncc skip
; mov d, t
; skip:
;
; Notes: cc can be a simple expression (see below).
; in the latter case cc can be cxz (swaps f & t and jumps if true).
; the suffix applies to the destination, not the condition.
;
;
; ========================
; Zero setting and testing
; ========================
;
; zero/zero[bwd]: set register/memory to zero (and set zero, clear carry).
;
; zero ax ==> xor ax, ax
; zerow [var] ==> and word [var], byte 0
;
; isz/isz[bwd]: test register/memory for zero (and clear carry).
;
; isz ax ==> test ax, ax
; iszw [var] ==> cmp word [var], byte 0
;
; jz[rbwd]: jump if register/memory is zero.
; jnz[rbwd]: jump if register/memory is not zero.
;
; jzr ax, label ==> test ax, ax
; jz label
; jnzw [var], lbl ==> cmp word [var], byte 0
; jne lbl
;
; ifz/ifz[bwd]: execute code if register/memory is zero.
; ifnz/ifnz[bwd]: execute code if register/memory is not zero.
;
; ifz ax, ... ==> test ax, ax
; ifz ax jnz fi
; ... ...
; fi fi:
;
;
; ========================
; Flag setting and testing
; ========================
;
; A flag is a boolean byte variable. It is 0 for off and -1 for on (and *not*
; anything else).
;
; dflg: declare and initialise flag variables (allows an array/struc of flags).
; tflg: test a flag (set carry if on).
; sflg: set a flag.
; cflg: clear a flag.
; iflg: invert (toggle) a flag.
;
; flag dflg on,off ==> flag db -1,0
; tflg [flag] ==> rol byte [flag], 1
; sflg [flag] ==> mov byte [flag], -1
; sflg. [flag] ==> dec byte [flag]
; cflg [flag] ==> mov byte [flag], 0
; cflg. [flag] ==> inc byte [flag]
; iflg [flag] ==> not byte [flag]
;
; Note: sflg and cflg have an optional second parameter, assumed to be a
; register with the appropriate value.
;
; jflg/jnflg: jump if flag is on/off
;
; jflg [flag], lbl ==> rol byte [flag], 1
; jc lbl
;
; ifflg/ifnflg: execute an instruction or block if a flag is on/off.
;
; ifflg [flag], ... rol [flag], 1
; ifflg [flag] ==> jnc fi
; ... ...
; fi fi:
;
;
; ================
; Bit manipulation
; ================
;
; bit(): translate a bit index to its value (up to three bits are allowed).
; tbit: test bits (NZ if any one bit is set).
; tbits: test bits (ZR if all bits set; inverts destination).
; sbit: set bits.
; cbit: clear (reset) bits.
; ibit: invert (complement) bits.
; cfbit: set bit 0 of a byte from carry.
;
; test al, bit(0) ==> test al, 01h
; tbit al, 0,1 ==> test al, 03h
; sbit al, 2 ==> or al, 04h
; cbit al, 3 ==> and al, 0f7h
; ibit al, 4 ==> xor al, 10h
;
; tbits al, 0,1 ==> not al
; test al, 3
;
; cfbit [flags] ==> pushf
; shr byte [flags], 1
; popf
; rcl byte [flags], 1
;
; Notes: if an identifier is not detected a memory reference is assumed and a
; size prefix is automatically added based on the highest bit set.
; recognises AX/BX/CX/DX and will use the high or low reg if suitable.
;
;
; ==========
; Preserving
; ==========
;
; save..restore: push and pop a set of registers (or memory locations).
;
; save ax,bx ==> push ax
; ... push bx
; restore ...
; pop bx
; pop ax
;
; savea..restore: push/pop all, plus optional extra.
;
; savea ds ==> push ds
; ... pusha
; restore ...
; popa
; pop ds
;
; Note: if i8086 is defined, pusha becomes "save ax,cx,dx,bx,si,di".
;
; pusha./popa.: pusha/popa for i8086.
;
; Note: AX takes the place of SP.
;
;
; ===========
; Subroutines
; ===========
;
; uses: alternative form of save/savea for use at the start of a subroutine.
;
; uses all,ds,es ==> savea ds,es
;
; return: (near) return from a subroutine.
;
; return ==> .ret: restore
; ret
;
; ret./retif: jump (conditionally) to the return point of a subroutine.
;
; retif al zr ==> test al, al
; jz .ret
;
; Note: uses/return can't be used for local/nested subroutines.
;
;
; ========
; Inlining
; ========
;
; ijmpf/icallf: inlined far jump/call.
;
; ijmpf old2f ==> db 0eah ;jmp far
; old2f dd 0
;
; i[bwd]: inlined variables.
;
; mov ax, i(num) ==> mov ax, 0
; num iw num equ $-2
;
;
; =============
; If statements
; =============
;
; The following statements (except else) all have suffixed versions (in the
; case of "if." the suffix comes before the dot). "cc" can include "cxnz" and
; can also be an expression (see below).
;
; jif: jump if an expression is true.
;
; jif ax ,e, 1, lbl ==> cmp ax, 1
; je lbl
;
; if.: conditionally execute a line of code (simple expression only).
;
; if. cc, ... ==> jncc skip
; ...
; skip:
;
; [ifn..]if..[andif..][elif..][else..]fi: conditionally execute blocks of code.
;
; ifn cc ==> jcc if
; ... ...
; if cc if: jncc elif
; ... ...
; andif cc jncc elif
; ... ...
; fi cc jcc fi
; ... ...
; elif cc jmp fi
; ... elif: jncc else
; else ...
; ... jmp fi
; fi else: ...
; fi:
;
; Note: "ifn" is used as a substitute for an "orif" statement.
; eg: instead of "if e/orif c", do "ifn e/if c".
;
;
; =====
; Loops
; =====
;
; All the statements below accepting conditions also have suffixed counterparts.
; "cc" can be "cxnz", as well as an expression (see below).
;
; repeat..until: loop until a condition is met.
;
; repeat ==> repeat:
; ... ...
; until cc jncc repeat
;
; repeat..next: loop using CX as a counter.
;
; repeat 8 ==> mov cx, 8
; ... rpt: ...
; next [cc] loop[cc] rpt
;
; repeat ,8 ==> mov cl, 8 ;assume CH is zero
; ... rpt: ...
; next loop rpt
;
; Note: next can still be used even if repeat has no parameter.
; use "repeat0" to test CX for zero and skip the loop.
;
; repeat[rbwd]..next: use a register/byte/word/dword as a counter.
;
; repeatr al ==> rpt: ...
; ... dec al
; next jnz rpt
;
; repeatr al,ns ==> rpt: ...
; ... dec al
; next jns rpt
;
; Note: there is a corresponding "repeat0" form, which assumes a positive
; count and jumps to the DEC to test for -1 to skip the loop.
;
; rptlmt..next: repeat a number of times, or until a limit is reached.
;
; rptlmt dx, 8 ==> mov cx, 8
; ... rpt: ...
; next dec dx
; loopnz rpt
;
; Note: a second comma can be used to just set CL (dx,,8).
;
; for..next: update a variable, until a condition is met, or a number of times.
;
; for var, [start], cond|*,[cnt] [,+|step|-[,step]]
;
; var: the variable to update
; start: it's starting value (if not already set)
; cond: simple expression to continue the loop;
; if the initial parameter is blank, var will be used
; *,cnt: loop cnt times; as with repeat cnt can be CX, CL or
; a register/byte/word/dword (or nothing if CX is already set)
; +: INC var
; step: ADD step to var
; -: DEC var
; -,step: SUB step from var
;
; for si, table, {,b, tableend}, entry_size
; ... ==> mov si, table
; next for: ...
; add si, entry_size
; cmp si, tableend
; jb for
;
; for si, table, *,{w,[entries]}, entry_size
; ... ==> mov si, table
; next for: ...
; add si, entry_size
; dec word [entries]
; jnz for
;
; Note: there is also a "for0" statement, which tests the condition before
; starting the loop; as with "repeat0", the counter is assumed to be
; positive and JNS will be used to continue the loop.
;
; do..while: loop while a condition is true.
;
; do ==> do:
; ... ...
; while cc jcc do
;
; while..[is..]wend: loop while a condition is true, or forever.
;
; while ==> while:
; ... ...
; is cc jncc wend
; ... ...
; wend jmp while
; wend:
;
; when..while[..is]: loop while a condition is true.
;
; when ==> jmp while
; ... when:
; while ...
; ... while:
; is cc ...
; jcc when
;
; whilst: starts a new while within a do or when (do..whilst..wend..while).
;
; break: break out of any of the above.
;
; break [cc] ==> jcc <done>
;
; cntnu: skip to the next cycle.
;
; cntnu [cc] --> jcc <next>
;
;
; ===========
; Expressions
; ===========
;
; An expression combines an instruction with a condition code. It also allows
; AND and OR operations (however, a simple expression does not). A single
; expression can be used to test a set of values or a range of values.
;
; Condition code:
; e ==> je true
; cxnz ==> jcxz false
;
; Flag: a memory reference (w/o size prefix) is assumed to be a flag.
; [Flag] ==> rol byte [Flag], 1 / jc true
; !,[Flag] ==> rol byte [Flag], 1 / jnc true
;
; Zero: test a register/byte/word/dword for (not) zero.
; ax zr ==> test ax, ax / jz true
; [bx] nzb ==> cmp byte [bx], 0 / jne true
;
; Bits: test a bit is set/clear.
; al ,&, bit(0,1) ==> test al, 3 / jnz true
; al ,&!, 4 ==> test al, 4 / jz true
;
; Comparison:
; al ,e, 4 ==> cmp al, 4 / je true
;
; Set: test any number of values.
; al ,e, {1,2} ==> cmp al, 1 / je true / cmp al, 2 / je true
; al ,ne, {1,2} ==> cmp al, 1 / je false / cmp al, 2 / jne true
;
; Range (prefix with '-' for signed comparisons, eg "-[]"):
; al ,[], {'0','9'} ==> cmp al, '0' / jb false / cmp al, '9' / jbe true
; al ,[!], {'0','9'} ==> cmp al, '0' / jb true / cmp al, '9' / ja true
; al ,(), {'0','9'} ==> cmp al, '0' / jbe false / cmp al, '9' / jb true
; al ,(!), {'0','9'} ==> cmp al, '0' / jbe true / cmp al, '9' / jae true
;
; AND/OR: combines multiple expressions (left to right).
; {al ,[], '0','9'} OR {al ,[], 'A','Z'} OR {al ,[], 'a','z'}
; ==> true if AL is a digit or letter
; {al ,[!], '0','9'} AND {al ,[!], 'A','Z'} AND {al ,[!], 'a','z'}
; ==> true if AL is not a digit or letter
;
; Note: using a suffixed statement will only apply the size prefix to the
; first expression ("ifb {[bx]} OR {byte [si]}").
;
;
; ==========
; Interrupts
; ==========
;
; intr: perform an INT, setting AH or AX or AH & AL (use %define, not equ).
; dos: intr 21h,
; vid: intr 10h,
; disk: intr 13h,
; kbd: intr 16h,
; amis: intr 2dh,
; mpx: intr 2fh,
; mous: intr 33h, 0,
; exit: intr 21h, 4ch,
; win: intr 2fh, 16h,
;
; dos 25h, 2fh ==> mov ax, 252fh
; int 21h
;
; Note: the preprocessor doesn't recognise negative numbers, so "exit -1"
; will set AH & AL, not AX; use "exit 0-1" as a workaround.
;
; prch: use DOS to print a character.
;
; prch 'a' ==> mov ah, 2
; prch. 'b' mov dl, 'a'
; int 21h
; mov dl, 'b'
; int 21h
;
; Note: the character is optional.
;
; dc: define a C string (processes escapes if CSTR is defined).
; d$, dz, dln: define a '$'-, NUL-, or CRLF-terminated C string.
; dl$, dlz: define a CRLF and '$'- or NUL-terminated C string.
;
; Notes: the string is optional, in which case just the terminator is written.
; the following C escapes are recognised (case sensitive):
; \0 NUL \l linefeed (not C)
; \a alert \n CR+LF (not strictly C)
; \b backspace \r return
; \e escape \t tab
; an unrecognised escape is treated as an escaped literal ("\\" is "\").
;
; %define CSTR
; dl$ "First line.\nSecond line."
; ==> db "First line.",13,10,"Second line.",13,10,'$'
;
; prnt$: use DOS to print a dollar-terminated string.
;
; prnt$ str1 ==> mov ah, 9
; prnt$. str2 mov dx, str1
; int 21h
; mov dx, str2
; int 21h
;
; Notes: str can be an actual string (or a character number); it will be added
; to the STR$ section (default is .data) using d$.
; the string is optional.
; =====================
; Suffixed Instructions
; =====================
; Shift data size from the destination to the instruction.
%macro suffix_ 1-*.nolist
%rep %0
%idefine %1b %1 byte
%idefine %1w %1 word
%idefine %1d %1 dword
%rotate 1
%endrep
%endmacro
suffix_ adc, add, and, cmp, dec, div, idiv
suffix_ imul, inc, mov, mul, neg, not, or
suffix_ pop, push, rcl, rcr, rol, ror, sal
suffix_ sar, sbb, shl, shr, sub, test, xor
suffix_ for, for0
; Use an 'l' suffix for already existing instructions.
%undef movd
%undef shld
%undef shrd
%idefine movl mov dword
%idefine shll shl dword
%idefine shrl shr dword
; An immediate byte can be PUSHed, but not POPped.
%undef popb
; Bug?: Braces are not recognised within a parameter, so the above solution
; won't work with braced parameters (ie: "byte {[bx] ,e, 1}" fails).
%macro size_ 3-4+.nolist
%1 {%2 %3},%4
%endmacro
%macro ssuffix_ 1-*.nolist
%rep %0
%idefine %1b size_ %1, byte,
%idefine %1w size_ %1, word,
%idefine %1d size_ %1, dword,
%rotate 1
%endrep
%endmacro
ssuffix_ retif, jif, ifn, if, andif, elif, fi
ssuffix_ rptlmt, while, whilst, is, break, cntnu, next, until
; ==========
; Assignment
; ==========
; Translate two bytes to a word, or two words to a dword.
%define hl(hi, lo) (((hi) << 8) | (lo & 0xff))
%define HL(hi, lo) (((hi) << 16) | (lo & 0xffff))
%imacro ld 2.nolist
push %2
pop %1
%endmacro
%imacro ldw 2.nolist
pushw %2
popw %1
%endmacro
%imacro ldd 2.nolist
pushd %2
popd %1
%endmacro
; Copy 16-bit pair to 32-bit (rm32, hi16, lo16).
%imacro ld 3.nolist
pushw %2
pushw %3
popd %1
%endmacro
; Copy 32-bit to 16-bit pair (hi16, lo16, rm32).
%imacro ldw 3.nolist
pushd %3
popw %2
popw %1
%endmacro
; Swap two segment registers or memory locations.
%macro swap_ 2-3.nolist
push%1 %2
push%1 %3
pop%1 %2
pop%1 %3
%endmacro
%idefine swap swap_ ,
%idefine swapw swap_ w,
%idefine swapd swap_ d,
; Load two 16-bit registers from memory (high, low, address w/o brackets).
%imacro ldhl 3.nolist
mov %1, [%3+2]
movw %2, [%3] ; generates warning if 32-bit reg
%endmacro
; Macros coded for the 8086.
%ifdef i8086
; Load DX:AX from consecutive memory locations.
%imacro ldd 1.nolist
ldhl dx,ax, %1
%endmacro
%idefine lss. ldhl ss,
%imacro movzx. 2.nolist
%push zx
%ifidni %1,ax
%define %$r a
%elifidni %1,bx
%define %$r b
%elifidni %1,cx
%define %$r c
%elifidni %1,dx
%define %$r d
%else
%define %$r %1
%error "`%$r' not supported by `movzx.'"
%define %$r a ; prevent additional errors
%endif
mov %{$r}l, %2
mov %{$r}h, 0
%pop
%endmacro
; Store DX:AX to memory, after optionally loading from memory.
%imacro mmovd 1-2.nolist
%if %0 == 2
mov ax, [%2]
mov dx, [%2+2]
%endif
mov [%1], ax
mov [%1+2], dx
%endmacro
%else ; 386 macros
; Load EAX from memory.
%imacro ldd 1.nolist
mov eax, [%1]
%endmacro
%imacro lss. 2.nolist
lss %1, [%2]
%endmacro
%idefine movzx. movzx
; Store EAX to memory, after optionally loading from memory.
%imacro mmovd 1-2.nolist
%if %0 == 2
mov eax, [%2]
%endif
mov [%1], eax
%endmacro
%endif
; Store two registers (high, low [16-bit], address).
%imacro sthl 3.nolist
movw [%3], %2 ; generates warning if 32-bit reg
mov [%3+2], %1
%endmacro
; Store a segment and register (16-bit).
%idefine scs sthl cs,
%idefine sds sthl ds,
%idefine ses sthl es,
%idefine sfs sthl fs,
%idefine sgs sthl gs,
%idefine sss sthl ss,
; Copy memory via a register (reg, dst, src).
%imacro mmov 3.nolist
mov %1, %3
mov %2, %1
%endmacro
; Copy memory via the accumulator.
%idefine mmovb mmov al,
%idefine mmovw mmov ax,
%idefine mmovl mmov eax,
; Conditional move.
%macro cmov_ 4.nolist ; if (cc) rm = val
if. {%2}, mov%1 %3, %4
%endmacro
%macro cmov_ 5.nolist ; rm = (cc) ? val1 : val2
%ifidni %3,cxz
mov%1 %2, %4
if. cxnz, mov%1 %2, %5
%else
mov%1 %2, %5
if. {%3}, mov%1 %2, %4
%endif
%endmacro
%idefine cmov cmov_ ,
%idefine cmovby cmov_ b, ; cmovb is already an instruction
%idefine cmovw cmov_ w,
%idefine cmovd cmov_ l,
; ========================
; Zero setting and testing
; ========================
; Zero a register (and set zero, clear carry).
%imacro zero 1.nolist
xor %1, %1
%endmacro
; Zero a byte/word/dword (and set zero, clear carry).
%imacro zero_ 1.nolist
and %1, byte 0
%endmacro
%idefine zerob zero_ byte
%idefine zerow zero_ word
%idefine zerod zero_ dword
; Test a register for zero (and clear carry).
%imacro isz 1.nolist
test %1, %1
%endmacro
; Test a byte/word/dword for zero (and clear carry).
%macro isz_ 1.nolist
cmp %1, byte 0
%endmacro
%idefine iszb isz_ byte
%idefine iszw isz_ word
%idefine iszd isz_ dword
; Helper macro for zero and flag jumping.
%macro j_ 4.nolist
%1 %3
j%2 %4
%endmacro
; Helper macro for zero and flag if'ing.
%macro if_ 3-4+.nolist
%1 %3
%if %0 == 3
if %2
%else
if. %2, %4
%endif
%endmacro
; Jump if a register/byte/word/dword is (not) zero.
%idefine jzr j_ isz, z,
%idefine jzb j_ iszb, e,
%idefine jzw j_ iszw, e,
%idefine jzd j_ iszd, e,
%idefine jnzr j_ isz, nz,
%idefine jnzb j_ iszb, ne,
%idefine jnzw j_ iszw, ne,
%idefine jnzd j_ iszd, ne,
; Execute code if a register/byte/word/dword is (not) zero.
%idefine ifz if_ isz, z,
%idefine ifzb if_ iszb, e,
%idefine ifzw if_ iszw, e,
%idefine ifzd if_ iszd, e,
%idefine ifnz if_ isz, nz,
%idefine ifnzb if_ iszb, ne,
%idefine ifnzw if_ iszw, ne,
%idefine ifnzd if_ iszd, ne,
; ========================
; Flag setting and testing
; ========================
; Declare flag variables (as on or off).
%imacro dflg 1-*.nolist
%rep %0
%ifidni %1,on
db -1
%elifidni %1,off
db 0
%else
db %1
%endif
%rotate 1
%endrep
%endmacro
; Test a flag (set carry if on).
%imacro tflg 1.nolist
rolb %1, 1
%endmacro
; Set a flag.
%imacro sflg 1-2.nolist -1
movb %1, %2
%endmacro
%idefine sflg. decb
; Clear a flag.
%imacro cflg 1-2.nolist 0
movb %1, %2
%endmacro
%idefine cflg. incb
; Invert (toggle) a flag.
%idefine iflg notb
; Jump depending on a flag.
%idefine jflg j_ tflg, c,
%idefine jnflg j_ tflg, nc,
; Execute code depending on a flag.
%idefine ifflg if_ tflg, c,
%idefine ifnflg if_ tflg, nc,
; ================
; Bit manipulation
; ================
; Translate a bit index to its value.
%idefine bit(n) (1 << (n))
%idefine bit(n1,n2) (bit(n1) + bit(n2))
%idefine bit(n1,n2,n3) (bit(n1) + bit(n2) + bit(n3))
; Translate multiple bits to a value.
%macro mbit_ 1-*.nolist
%assign bitval_ 0
%rep %0
%assign bitval_ bitval_ + bit(%1)
%rotate 1
%endrep
%endmacro
; Check destination size against source value.
%macro bitsize_ 1.nolist
%ifid %1 ; assume register or already-sized memory
%define bitdst_ %1
%ifidni %1,ax
%define %%r a
%elifidni %1,bx
%define %%r b
%elifidni %1,cx
%define %%r c
%elifidni %1,dx
%define %%r d
%endif
%ifdef %%r
%if bitval_ < 100h
%define bitdst_ %%r %+ l
%elif bitval_ & 0ffh == 0
%define bitdst_ %%r %+ h
%assign bitval_ bitval_ >> 8
%endif
%endif
%else ; assume memory
%if bitval_ < 100h
%define bitdst_ byte %1
%elif bitval_ < 10000h
%define bitdst_ word %1
%else
%define bitdst_ dword %1
%endif
%endif
%endmacro
; Test/set/clear/invert bits.
%macro bit_ 3+.nolist
mbit_ %3
bitsize_ %2
%ifidn %1,and
%assign bitval_ ~bitval_
%endif
%1 bitdst_, bitval_
%endmacro
%idefine tbit bit_ test,
%idefine sbit bit_ or,
%idefine cbit bit_ and,
%idefine ibit bit_ xor,
; Test if all bits are set (but inverts destination).
%imacro tbits 2+.nolist
mbit_ %2
bitsize_ %1
not bitdst_
test bitdst_, bitval_
%endmacro
; Set bit 0 of a byte from carry.
%imacro cfbit 1.nolist
pushf
shrb %1, 1
popf
rclb %1, 1
%endmacro
; ==========
; Preserving
; ==========
; PUSH/POP a list.
%macro push_ 1-*.nolist
%rep %0
push %1
%rotate 1
%endrep
%endmacro
%macro pop_ 1-*.nolist
%rep %0
%rotate -1
pop %1
%endrep
%endmacro
; Save a set of registers (or memory locations).
%imacro save 1+.nolist
%push save
%define %$regs %1
push_ %1
%endmacro
%ifdef i8086
; Save all registers, plus extra.
%imacro savea 0-1+.nolist
%if %0 > 0
save %1,ax,cx,dx,bx,si,di
%else
save ax,cx,dx,bx,si,di
%endif
%endmacro
; Push/pop all general purpose registers (except SP).
%idefine pusha. save ax,cx,dx,bx,ax,bp,si,di
%idefine popa. restore
%else ; 386
%imacro savea 0-1+.nolist
%if %0 > 0
save %1
%endif
%push savea
pusha