This repository has been archived by the owner on Feb 23, 2022. It is now read-only.
forked from scfduffy/8086-assembler
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathASM.A
4227 lines (4209 loc) · 67.9 KB
/
ASM.A
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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; `ASM.A' -- Source code for 8086 assembler main executable `ASM.COM'.
;
; Copyright (c) 2001 Stephen Duffy <[email protected]>
; Copyright (c) 2020-2021 Robert Riebisch <[email protected]>
;
; Usage of the works is permitted under the terms of the GNU GPL v2.
; See `LICENSE' file for details.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
maxexplen equ 80
maxsymlen equ 30
symstack equ 0b000h
buflen equ 80
jmp start ; jump to main
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; data starts here
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; user-interface messages except errors
msg_banner: db "8086 Assembler for DOS",13,10
db "Copyright (c) 2001 Stephen Duffy",13,10
db "Copyright (c) 2020-2021 Robert Riebisch",13,10
db "[Under GNU GPL v2]",13,10
db 13,10,0
msg_pass: db ">> Pass ",0
msg_started: db " started",13,10,0
msg_finished: db "<< Finished successfully",13,10,0
msg_linesproc: db " lines processed",13,10,0
msg_byteswrit: db " bytes written",13,10,0
msg_errline: db "Error at line ",0
msg_colon: db ": ",0
msg_tab: db " ",0
msg_crlf: db 13,10,0
; error messages regarding source code
; return code for DOS + ASCIIZ message for user
errmsg_strtoolong: db 1,"String too long",13,10,0
errmsg_linetoolong: db 2,"Line too long",13,10,0
errmsg_unknownmne: db 3,"Unknown mnemonic",13,10,0
errmsg_invparams: db 4,"Illegal parameters",13,10,0
errmsg_illidir: db 5,"Illegal expression",13,10,0
errmsg_ptrexp: db 6,"`PTR' expected",13,10,0
errmsg_ptrtoolarge: db 7,"Pointer too large",13,10,0
errmsg_inviform: db 8,"Invalid instruction format",13,10,0
errmsg_badregmix: db 9,"Invalid register size",13,10,0
errmsg_invnumform: db 10,"Bad literal",13,10,0
errmsg_littoolarge: db 11,"Literal too large",13,10,0
errmsg_offtoolarge: db 12,"Address too far",13,10,0
errmsg_toomanysym: db 13,"Too many symbols",13,10,0
errmsg_invsymnam: db 14,"Symbol name too long",13,10,0
errmsg_undefsym: db 15,"Unknown symbol",13,10,0
; other error messages ("runtime environment")
; return codes >200 will skip calling printinst() function on error
errmsg_inferr: db 201,"Input file error",13,10,0
errmsg_ouferr: db 202,"Output file error",13,10,0
; special error message for DOS 1.x (no return code possible)
errmsg_baddos: db "DOS 2.0 or higher required",13,10,0
; temporary data
; When `ASM.COM' will support uninitialized data, these are candidates to
; save 880 bytes disk space. Need to call an initialization function on
; program start then.
; each 80 bytes large
lineStore:
dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
labelStore:
dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
mnemonicStore:
dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
expsetStore:
dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
exp1Store:
dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
exp2Store:
dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
; each 160 bytes large
codeStore:
dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
tempBuffer:
dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
; FIXME
; Move `getsbuffer' before `codeStore' and on `ASM.COM ASM.A' you will get:
; >> Pass 1 started
; Error at line 50: Bad literal
; errmsg_littoolarge db 11,"
; Probably something gets overwritten.
; 80 bytes large
getsbuffer:
dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
dw 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
___getsbuffer:
digitlist:
db "00112233445566778899aAbBcCdDeEfFgGhHiIjJkK"
db "lLmMnNoOpPqQrRsStToOpPqQrRsStTuUvVwWxXyYzZ",0
destructivedl:
db "00112233445566778899aAbBcCdDeEfFgGhHiIjJkK"
db "lLmMnNoOpPqQrRsStToOpPqQrRsStTuUvVwWxXyYzZ",0
delimiters:
db "ª `Ý!",'"',"%^&*()-+=[{]};:'~,<.>/?",92,"|",0
indirectRegs:
db "bp",0,1
db "bx",0,2
db "di",0,4
db "si",0,8
___indirectRegs:
iregcomb: ;convs dl from "decodeIndirect" to exp type
db 10,6,9,5,8,4,0,2,1
db 0,1,2,3,4,5,6,7,6
___iregcomb:
bptr: db "byte",0
wptr: db "word",0
dptr: db "dword",0
qptr: db "qword",0
aptr: db "ptr",0
farstring:
db "far",0
regnames:
db "ah",0,32,4
db "al",0,32,0
db "ax",0,48,0
db "bh",0,32,7
db "bl",0,32,3
db "bp",0,48,5
db "bx",0,48,3
db "ch",0,32,5
db "cl",0,32,1
db "cs",0,64,1
db "cx",0,48,1
db "dh",0,32,6
db "di",0,48,7
db "dl",0,32,2
db "ds",0,64,3
db "dx",0,48,2
db "es",0,64,0
db "si",0,48,6
db "sp",0,48,4
db "ss",0,64,2
___regnames:
segregs:
db "cs",0
db "ds",0
db "es",0
db "ss",0
___segregs:
mnemonics:
db "aaa",0,0,0,0, 1ah
db "aad",0,0,0,0, 1dh
db "aam",0,0,0,0, 1ch
db "aas",0,0,0,0, 1bh
db "adc",0,0,0,0, 02h
db "add",0,0,0,0, 00h
db "and",0,0,0,0, 04h
db "call",0,0,0, 3ah
db "cbw",0,0,0,0, 64h
db "clc",0,0,0,0, 88h
db "cld",0,0,0,0, 8ch
db "cli",0,0,0,0, 8ah
db "cmc",0,0,0,0, 85h
db "cmp",0,0,0,0, 07h
db "cmpsb",0,0, 56h
db "cmpsw",0,0, 57h
db "cs:",0,0,0,0, 09h
db "cwd",0,0,0,0, 65h
db "daa",0,0,0,0, 18h
db "das",0,0,0,0, 19h
db "dec",0,0,0,0, 39h
db "div",0,0,0,0, 36h
db "ds:",0,0,0,0, 0bh
db "es:",0,0,0,0, 08h
db "hlt",0,0,0,0, 84h
db "idiv",0,0,0, 37h
db "imul",0,0,0, 35h
db "in",0,0,0,0,0, 76h
db "inc",0,0,0,0, 38h
db "int",0,0,0,0, 6fh
db "into",0,0,0, 70h
db "iret",0,0,0, 71h
db "ja",0,0,0,0,0, 47h
db "jae",0,0,0,0, 43h
db "jb",0,0,0,0,0, 42h
db "jbe",0,0,0,0, 46h
db "jc",0,0,0,0,0, 42h
db "jcxz",0,0,0, 0e3h
db "je",0,0,0,0,0, 44h
db "jg",0,0,0,0,0, 4fh
db "jge",0,0,0,0, 4dh
db "jl",0,0,0,0,0, 4ch
db "jle",0,0,0,0, 4eh
db "jmp",0,0,0,0, 3ch
db "jna",0,0,0,0, 46h
db "jnae",0,0,0, 42h
db "jnb",0,0,0,0, 43h
db "jnbe",0,0,0, 47h
db "jnc",0,0,0,0, 43h
db "jne",0,0,0,0, 45h
db "jng",0,0,0,0, 4eh
db "jnge",0,0,0, 4ch
db "jnl",0,0,0,0, 4dh
db "jnle",0,0,0, 4fh
db "jno",0,0,0,0, 41h
db "jnp",0,0,0,0, 4bh
db "jns",0,0,0,0, 49h
db "jnz",0,0,0,0, 45h
db "jo",0,0,0,0,0, 40h
db "jp",0,0,0,0,0, 4ah
db "jpe",0,0,0,0, 4ah
db "jpo",0,0,0,0, 4bh
db "js",0,0,0,0,0, 48h
db "jz",0,0,0,0,0, 44h
db "lahf",0,0,0, 6ah
db "lds",0,0,0,0, 6dh
db "lea",0,0,0,0, 62h
db "les",0,0,0,0, 6ch
db "lock",0,0,0, 80h
db "lodsb",0,0, 5ch
db "lodsw",0,0, 5dh
db "loop",0,0,0, 0e2h
db "loope",0,0, 0e1h
db "loopne",0, 0e0h
db "loopnz",0, 0e0h
db "loopz",0,0, 0e1h
db "mov",0,0,0,0, 61h
db "movsb",0,0, 54h
db "movsw",0,0, 55h
db "mul",0,0,0,0, 34h
db "neg",0,0,0,0, 33h
db "nop",0,0,0,0, 63h
db "not",0,0,0,0, 32h
db "or",0,0,0,0,0, 01h
db "out",0,0,0,0, 77h
db "pop",0,0,0,0, 3fh
db "popf",0,0,0, 68h
db "push",0,0,0, 3eh
db "pushf",0,0, 67h
db "rcl",0,0,0,0, 12h
db "rcr",0,0,0,0, 13h
db "rep",0,0,0,0, 83h
db "repe",0,0,0, 83h
db "repne",0,0, 82h
db "repnz",0,0, 82h
db "repz",0,0,0, 83h
db "ret",0,0,0,0, 6bh
db "retf",0,0,0, 6eh
db "rol",0,0,0,0, 10h
db "ror",0,0,0,0, 11h
db "sahf",0,0,0, 69h
db "sar",0,0,0,0, 17h
db "sbb",0,0,0,0, 03h
db "scasb",0,0, 5eh
db "scasw",0,0, 5fh
db "shl",0,0,0,0, 14h
db "shr",0,0,0,0, 15h
db "ss:",0,0,0,0, 0ah
db "stc",0,0,0,0, 89h
db "std",0,0,0,0, 8dh
db "sti",0,0,0,0, 8bh
db "stosb",0,0, 5ah
db "stosw",0,0, 5bh
db "sub",0,0,0,0, 05h
db "test",0,0,0, 30h
db "wait",0,0,0, 66h
db "xchg",0,0,0, 60h
db "xlat",0,0,0, 72h
db "xor",0,0,0,0, 06h
___mnemonics:
mneTranslate:
db 01ah,037h,01dh,0d5h,01ch,0d4h,01bh,03fh,064h,098h,088h,0f8h,08ch,0fch
db 08ah,0fah,085h,0f5h,056h,0a6h,057h,0a7h,009h,02eh,065h,099h,018h,027h
db 019h,02eh,00bh,03eh,008h,026h,084h,0f4h,070h,0ceh,071h,0cfh,06ah,09fh
db 080h,0f0h,05ch,0ach,05dh,0adh,054h,0a4h,055h,0a5h,063h,090h,068h,09dh
db 067h,09ch,082h,0f2h,083h,0f3h,06bh,0c3h,06eh,0cbh,069h,09eh,05eh,0aeh
db 05fh,0afh,00ah,036h,089h,0f9h,08dh,0fdh,08bh,0fbh,05ah,0aah,05bh,0abh
db 066h,09bh,072h,0d7h
equalityText:
db "equ",0
PseudoOps:
db "db",0,0,0,0,0,0,0
db "dw",0,0,0,0,0,0,1
db "equ",0,0,0,0,0,2
db "=",0,0,0,0,0,0,0,2
___PseudoOps:
assumedbase: dw 10
nextSymbol: dw symstack
symbolLoad: dw 0
curip: dw 0
codebase: dw 100h
codelen: dw 0
line: dw 0
curpass: db 1
addrspec: dw 0
containslabel: dw 0
inf: dw 0
ouf: dw 0
nextpos: dw 0
bufend: dw 0
eof: dw 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; code starts here (no more data)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
strlen:
push di
push cx
mov al,0
mov cx,-1
cmp cx,cx
cld
repnz
scasb
neg cx
dec cx
dec cx
xchg ax,cx
pop cx
pop di
ret
strpos:
push di
push cx
push ax
call strlen
xchg cx,ax
pop ax
mov cx,-1
cld
repnz
scasb
neg cx
dec cx
dec cx
xchg ax,cx
pop cx
pop di
ret
strnpos:
push di
push cx
push ax
call strlen
xchg cx,ax
pop ax
mov cx,-1
cld
repz
scasb
neg cx
dec cx
dec cx
xchg ax,cx
pop cx
pop di
ret
strrev:
push si
push di
push ax
mov si,di
call strlen
dec ax
add si,ax
___strrev:
mov al,[di]
mov ah,[si]
mov [si],al
mov [di],ah
inc di
dec si
cmp di,si
jl ___strrev
pop ax
pop di
pop si
ret
strlwr:
push ax
push bx
xor bx,bx
xor ah,ah
dec bx
___nextlwr:
inc bx
mov al,[bx+di]
cmp al,0
je ___strlwr
cmp al,'"'
je ___togq
cmp al,'`'
je ___togq
cmp al,"'"
je ___togq
cmp al,'A'
jl ___nextlwr
cmp al,'Z'
jg ___nextlwr
test ah,1
jnz ___nextlwr
add al,32
mov [bx+di],al
jmp ___nextlwr
___togq:
xor ah,1
jmp ___nextlwr
___strlwr:
pop bx
pop ax
ret
strcpy:
push di
push si
push ax
xchg si,di
call strlen
inc ax
xchg si,di
xchg ax,cx
cmp di,si
cld
repnz
movsb
xchg ax,cx
pop ax
pop si
pop di
ret
strcmp:
push bx
push cx
xor bx,bx ;assume both same length
call strlen
xchg ax,cx
xchg si,di
call strlen
xchg si,di
cmp cx,ax
je ___mainstrcmp ;both same length
jl ___dismall
mov bx,-1
xchg ax,cx
jmp ___mainstrcmp
___dismall:
mov bx,1
___mainstrcmp:
push si
push di
cld
repz
cmpsb
jl ___diless
jg ___siless
xchg ax,bx ;length determines
jmp ___strcmp
___diless:
mov ax,-1
jmp ___strcmp
___siless:
mov ax,1
___strcmp:
pop di
pop si
pop cx
pop bx
ret
replacetabs:
push ax
push bx
mov ax,02009h
mov bx,0
dec bx
___replacetabs:
inc bx
cmp [di+bx],al
jne ___nottab
mov [di+bx],ah
___nottab:
cmp byte ptr[di+bx],0
jne ___replacetabs
pop bx
pop ax
ret
removetrails:
push si
push cx
push ax
xor ah,ah
mov al,' '
call strnpos
mov si,di
add si,ax
call strcpy
pop ax
pop cx
pop si
ret
removeleads:
call strrev
call removetrails
call strrev
ret
removespaces:
push si
push di
push ax
push dx
mov si,di
call strlen
xchg dx,ax
___nextspace:
mov al,' '
call strpos
cmp ax,dx
jge ___removespaces
sub dx,ax
add di,ax
mov si,di
inc si
call strcpy
cmp dx,0
jne ___nextspace
___removespaces:
pop dx
pop ax
pop di
pop si
ret
removecomment:
push ax
push bx
mov al,';'
call outOfQuote
mov bx,ax
mov byte ptr[di+bx],0
pop bx
pop ax
ret
unbracket:
mov cx,1
call removetrails;
cmp byte ptr[di],'['
je ___openpres
xor cx,cx
___openpres:
inc di
call strrev
call removetrails
cmp byte ptr[di],']'
je ___closepres
xor cx,cx
___closepres:
inc di
call strrev
ret
ntos: ;(ax=num,bx=base,di=string)
push ax
push bx
push cx
push dx
push di
___ntos:
xor dx,dx
div bx
xchg ax,cx
mov al,dl
cmp al,9
jle ___notalpha
add al,7
___notalpha:
add al,48
mov [di],al
inc di
xchg cx,ax
cmp ax,0
jne ___ntos
mov [di],0
pop di
call strrev
pop dx
pop cx
pop bx
pop ax
ret
ston: ;(di,bx=base)->ax
push cx
push dx
push si
push di
push bx
mov si,1;
xor cx,cx;
call strlen
xchg ax,bx
___decloop:
dec bx
mov al,[di+bx];
cmp al,'a'
jl ___upperdigit
sub al,32
___upperdigit:
cmp al,'A'
jl ___normdigit
sub al,7
___normdigit:
sub al,48
xor ah,ah
xor dx,dx
mul si
add cx,ax
pop ax
push ax
xor dx,dx
mul si
xchg ax,si
cmp bx,0
jne ___decloop
mov ax,cx
pop bx
pop di
pop si
pop dx
pop cx
ret
testdigits: ;(di,bx=base)->cx=err
push ax
push dx
push bx
call strlen
xchg dx,ax
push si
push di
mov di,destructivedl
mov si,digitlist
call strcpy
pop si ;str now in si
mov ax,2
push dx
mul bx
pop dx
xchg bx,ax
mov byte ptr[di+bx],0
call strlen
xchg ax,cx
xor bx,bx
___nextdigit:
mov al,[si+bx]
call strpos
cmp ax,cx
jl ___validdigit
mov cx,0
jmp ___testdigits
___validdigit:
inc bx
cmp bx,dx
jl ___nextdigit
mov cx,1
___testdigits:
pop di
xchg si,di
pop bx
pop dx
pop ax
ret
aton: ;(di)->ax,cx=err
push di
push bx
push dx
push si
mov bx,1
cmp byte ptr [di],'+'
je ___signposi
cmp byte ptr [di],'-'
jne ___nosign
mov bx,-1
___signposi:
inc di
___nosign:
push bx
mov ax,word ptr[assumedbase]
xchg ax,dx
call strlen
xchg cx,ax
mov al,'x'
call strpos
cmp ax,cx
jg ___noleadx
add di,ax
inc di
mov dx,16
___noleadx:
call strlen
xchg ax,bx
cmp bx,0
jle ___atedge
dec bx
___atedge:
cmp byte ptr[di+bx],'t'
jne ___testhex
mov si,[di+bx]
mov byte ptr[di+bx],0
mov dx,10
jmp ___notrailchar
___testhex:
cmp byte ptr[di+bx],'h'
jne ___testoct
mov si,[di+bx]
mov byte ptr[di+bx],0
mov dx,10h
jmp ___notrailchar
___testoct:
cmp byte ptr[di+bx],'o';
jne ___testbin
mov si,[di+bx]
mov byte ptr[di+bx],0
mov dx,8h
jmp ___notrailchar
___testbin:
cmp byte ptr[di+bx],'n';
jne ___trailends
mov si,[di+bx]
mov byte ptr[di+bx],0
mov dx,2
jmp ___notrailchar
___trailends:
mov si,[di+bx]
___notrailchar:
push bx
mov bx,dx
call testdigits
jcxz ___aton
call ston
mov cx,1
___aton:
pop bx
mov [di+bx],si
pop bx
mul bx
pop si
pop dx
pop bx
pop di
ret
outofquote:
push cx
push bx
push si
push di
xor cx,cx
xor bx,bx
dec bx
___nextqtest:
inc bx
cmp byte ptr[bx+di],'"'
je ___togdqte
cmp byte ptr[bx+di],"'"
je ___togsqte
cmp [bx+di],al
je ___testqopen
cmp byte ptr[bx+di],0
jne ___nextqtest
call strlen
jmp ___outofquote
___togdqte:
cmp bx,0
jle ___notlitdqte
cmp byte ptr[bx+di-1],92
je ___nextqtest
___notlitdqte:
test cx,1
jnz ___nextqtest
xor cx,2
jmp ___nextqtest
___togsqte:
cmp bx,0
jle ___notlitsqte
cmp byte ptr[bx+di-1],92
je ___nextqtest
___notlitsqte:
test cx,2
jnz ___nextqtest
xor cx,1
jmp ___nextqtest
___testqopen:
test cx,3
jnz ___nextqtest
mov ax,bx
___outOfQuote:
pop di
pop si
pop bx
pop cx
ret
qtostest: ;(di=string)->(cx=err(open),ax=qtdlen)
push dx
push si
push di
mov si,di
mov dh,[di]
cmp dh,"'"
je ___qtoscont
cmp dh,"`"
je ___qtoscont
cmp dh,'"'
je ___qtoscont
xor cx,cx
jmp ___qtostest
___qtoscont:
inc di
___nextqtchar:
mov al,[di]
cmp al,92
jne ___testqtquote
inc di
inc di
___testqtquote:
cmp al,dh
jne ___testqtend
mov cx,1
jmp ___qtostest
___testqtend:
inc si
inc di
cmp byte ptr[di],0
jne ___nextqtchar
xor cx,cx
___qtostest:
mov ax,di
pop di
sub ax,di
inc ax
pop si
pop dx
ret
qtos: ;(di=string)->(cx=err(open)) ;conv qtd str to str
push ax
push dx
push si
push di
mov si,di
mov dh,[di] ;quote char = first char
xor cx,cx
inc si
call strcpy
dec si
___nextchar:
mov al,[di]
cmp al,92
jne ___testquote
inc si
mov al,[si]
cmp al,'n'
jne ___notnewline
mov byte ptr [si],10
jmp ___delslash
___notnewline:
cmp al,'t'
jne ___nottabchar
mov byte ptr [si],9
jmp ___delslash
___nottabchar:
cmp al,'r'
jne ___notreturn
mov byte ptr [si],13
jmp ___delslash
___notreturn:
___delslash:
call strcpy
dec si
jmp ___testend
___testquote:
cmp al,dh
jne ___testend
mov [di],0
mov cx,1
jmp ___qtos
___testend:
inc si
inc di
cmp byte ptr[di],0
jne ___nextchar
xor cx,cx
___qtos:
pop di
pop si
pop dx
pop ax
ret
btow: ;(si=bytes,di=words,cx=number of bytes) bytes to words
push ax
push si
push cx
push di
add cx,si
xor ah,ah
___btow:
mov al,[si]
mov [di],ax
inc di
inc di
inc si
cmp si,cx
jle ___btow
pop di
pop cx
pop si
pop ax
ret
strtok: ;input= str(di), lastdelim(al,0 if first call)
;output = toke(di), next(si), delim(al)
push bx
push dx
push ax
call strlen
mov dx,ax
pop ax
mov si,delimiters
xor bx,bx
cmp al,0
je ___nodelim
mov [di],al
___nodelim:
call removetrails
cld
dec bx
___nextch:
inc bx
mov al,[bx+di]
cmp al,0
jz ___strtok
xchg si,di
push ax
call strlen
xchg cx,ax
pop ax
push di
cld
repnz
scasb
pop di
xchg si,di
jcxz ___nextch
cmp bx,0
jg ___swapdelim
inc bx
___swapdelim:
mov al,byte ptr [bx+di]
mov byte ptr [bx+di],0
___strtok:
mov si,di
add si,bx
pop dx
pop bx
ret
unstrtok:
push bx
push ax
call strlen
pop bx
xchg bx,ax
mov [di+bx],al
pop bx
ret
chopsearch: ;(ds:si = lo, ds:bx = hi, cx = width, di = term, dx = pos)
;best @ ds:si, ax = <,=,>
push bx
push cx
push dx
push di
jmp chop
halfdiff:
mov ax,bx
sub ax,si
div cx
shr ax,1
mul cx
ret
comparemid:
push si
add si,ax