-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassembl.c
2045 lines (2027 loc) · 63.8 KB
/
assembl.c
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
// Free Disassembler and Assembler -- Assembler
//
// Copyright (C) 2001 Oleh Yuschuk
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// 16.01.2002 - corrected error in processing of immediate constants.
#define STRICT
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
//#include <dir.h>
#include <math.h>
#include <float.h>
#pragma hdrstop
#include "disasm.h"
////////////////////////////////////////////////////////////////////////////////
///////////////////////////// ASSEMBLER FUNCTIONS //////////////////////////////
// Scanner modes.
#define SA_NAME 0x0001 // Don't try to decode labels
#define SA_IMPORT 0x0002 // Allow import pseudolabel
// Types of input tokens reported by scanner.
#define SCAN_EOL 0 // End of line
#define SCAN_REG8 1 // 8-bit register
#define SCAN_REG16 2 // 16-bit register
#define SCAN_REG32 3 // 32-bit register
#define SCAN_SEG 4 // Segment register
#define SCAN_FPU 5 // FPU register
#define SCAN_MMX 6 // MMX register
#define SCAN_CR 7 // Control register
#define SCAN_DR 8 // Debug register
#define SCAN_OPSIZE 9 // Operand size modifier
#define SCAN_JMPSIZE 10 // Jump size modifier
#define SCAN_LOCAL 11 // Address on stack in form LOCAL.decimal
#define SCAN_ARG 12 // Address on stack in form ARG.decimal
#define SCAN_PTR 20 // PTR in MASM addressing statements
#define SCAN_REP 21 // REP prefix
#define SCAN_REPE 22 // REPE prefix
#define SCAN_REPNE 23 // REPNE prefix
#define SCAN_LOCK 24 // LOCK prefix
#define SCAN_NAME 25 // Command or label
#define SCAN_ICONST 26 // Hexadecimal constant
#define SCAN_DCONST 27 // Decimal constant
#define SCAN_OFS 28 // Undefined constant
#define SCAN_FCONST 29 // Floating-point constant
#define SCAN_EIP 30 // Register EIP
#define SCAN_SIGNED 31 // Keyword "SIGNED" (in expressions)
#define SCAN_UNSIGNED 32 // Keyword "UNSIGNED" (in expressions)
#define SCAN_CHAR 33 // Keyword "CHAR" (in expressions)
#define SCAN_FLOAT 34 // Keyword "FLOAT" (in expressions)
#define SCAN_DOUBLE 35 // Keyword "DOUBLE" (in expressions)
#define SCAN_FLOAT10 36 // Keyword "FLOAT10" (in expressions)
#define SCAN_STRING 37 // Keyword "STRING" (in expressions)
#define SCAN_UNICODE 38 // Keyword "UNICODE" (in expressions)
#define SCAN_MSG 39 // Pseudovariable MSG (in expressions)
#define SCAN_SYMB 64 // Any other character
#define SCAN_IMPORT 65 // Import pseudolabel
#define SCAN_ERR 255 // Definitely bad item
// Definition used by Assembler to report command matching errors.
#define MA_JMP 0x0001 // Invalid jump size modifier
#define MA_NOP 0x0002 // Wrong number of operands
#define MA_TYP 0x0004 // Bad operand type
#define MA_NOS 0x0008 // Explicit operand size expected
#define MA_SIZ 0x0010 // Bad operand size
#define MA_DIF 0x0020 // Different operand sizes
#define MA_SEG 0x0040 // Invalid segment register
#define MA_RNG 0x0080 // Constant out of expected range
typedef struct t_asmoperand {
int type; // Operand type, see beginning of file
int size; // Operand size or 0 if yet unknown
int index; // Index or other register
int scale; // Scale
int base; // Base register if present
long offset; // Immediate value or offset
int anyoffset; // Offset is present but undefined
int segment; // Segment in address if present
int jmpmode; // Specified jump size
} t_asmoperand;
static char* asmcmd = { 0 }; // Pointer to 0-terminated source line
static int scan; // Type of last scanned element
static int prio; // Priority of operation (0: highest)
static char sdata[TEXTLEN] = { 0 }; // Last scanned name (depends on type)
static long idata; // Last scanned value
static long double fdata; // Floating-point number
static char* asmerror = { 0 }; // Explanation of last error, or NULL
// Simple and slightly recursive scanner shared by Assemble(). The scanner is
// straightforward and ineffective, but high speed is not a must here. As
// input, it uses global pointer to source line asmcmd. On exit, it fills in
// global variables scan, prio, sdata, idata and/or fdata. If some error is
// detected, asmerror points to error message, otherwise asmerror remains
// unchanged.
static void Scanasm(int mode)
{
int i, j, base, maxdigit;
long decimal, hex;
long double floating, divisor;
char s[TEXTLEN] = { 0 }, * pcmd = { 0 };
sdata[0] = '\0';
idata = 0;
if (asmcmd == NULL)
{
asmerror = "NULL input line";
scan = SCAN_ERR;
return;
}
while (*asmcmd == ' ' || *asmcmd == '\t')
asmcmd++; // Skip leading spaces
if (*asmcmd == '\0' || *asmcmd == ';')
{
scan = SCAN_EOL;
return;
} // Empty line
if (isalpha(*asmcmd) || *asmcmd == '_' || *asmcmd == '@')
{
sdata[0] = *asmcmd++;
i = 1; // Some keyword or identifier
while ((isalnum(*asmcmd) || *asmcmd == '_' || *asmcmd == '@') && i < sizeof(sdata))
sdata[i++] = *asmcmd++;
if (i >= sizeof(sdata))
{
asmerror = "Too long identifier";
scan = SCAN_ERR;
return;
};
sdata[i] = '\0';
while (*asmcmd == ' ' || *asmcmd == '\t')
asmcmd++; // Skip trailing spaces
strcpy(s, sdata);
strupr(s);
for (j = 0; j <= 8; j++)
{ // j==8 means "any register"
if (strcmp(s, regname[0][j]) != 0) continue;
idata = j;
scan = SCAN_REG8; // 8-bit register
return;
}
for (j = 0; j <= 8; j++)
{
if (strcmp(s, regname[1][j]) != 0) continue;
idata = j;
scan = SCAN_REG16; // 16-bit register
return;
}
for (j = 0; j <= 8; j++)
{
if (strcmp(s, regname[2][j]) != 0) continue;
idata = j;
scan = SCAN_REG32; // 32-bit register
return;
}
for (j = 0; j < 6; j++)
{
if (strcmp(s, segname[j]) != 0) continue;
idata = j;
scan = SCAN_SEG; // Segment register
while (*asmcmd == ' ' || *asmcmd == '\t')
asmcmd++; // Skip trailing spaces
return;
}
if (strcmp(s, "ST") == 0)
{
pcmd = asmcmd; Scanasm(SA_NAME); // FPU register
if (scan != SCAN_SYMB || idata != '(')
{
asmcmd = pcmd; // Undo last scan
idata = 0; scan = SCAN_FPU;
return;
}
Scanasm(SA_NAME); j = idata;
if ((scan != SCAN_ICONST && scan != SCAN_DCONST) || idata < 0 || idata>7)
{
asmerror = "FPU registers have indexes 0 to 7";
scan = SCAN_ERR;
return;
}
Scanasm(SA_NAME);
if (scan != SCAN_SYMB || idata != ')')
{
asmerror = "Closing parenthesis expected";
scan = SCAN_ERR;
return;
}
idata = j;
scan = SCAN_FPU;
return;
}
for (j = 0; j <= 8; j++)
{
if (strcmp(s, fpuname[j]) != 0) continue;
idata = j;
scan = SCAN_FPU; // FPU register (alternative coding)
return;
}
for (j = 0; j <= 8; j++)
{
if (strcmp(s, mmxname[j]) != 0) continue;
idata = j;
scan = SCAN_MMX; // MMX register
return;
}
for (j = 0; j <= 8; j++)
{
if (strcmp(s, crname[j]) != 0) continue;
idata = j;
scan = SCAN_CR; // Control register
return;
}
for (j = 0; j <= 8; j++)
{
if (strcmp(s, drname[j]) != 0) continue;
idata = j;
scan = SCAN_DR; // Debug register
return;
}
for (j = 0; j < sizeof(sizename) / sizeof(sizename[0]); j++)
{
if (strcmp(s, sizename[j]) != 0) continue;
pcmd = asmcmd; Scanasm(SA_NAME);
if (scan != SCAN_PTR) // Fetch non-functional "PTR"
asmcmd = pcmd;
idata = j;
scan = SCAN_OPSIZE; // Operand (data) size in bytes
return;
}
if (strcmp(s, "EIP") == 0)
{ // Register EIP
scan = SCAN_EIP;
idata = 0;
return;
}
if (strcmp(s, "SHORT") == 0)
{ // Relative jump has 1-byte offset
scan = SCAN_JMPSIZE;
idata = 1;
return;
}
if (strcmp(s, "LONG") == 0)
{ // Relative jump has 4-byte offset
scan = SCAN_JMPSIZE;
idata = 2;
return;
}
if (strcmp(s, "NEAR") == 0)
{ // Jump within same code segment
scan = SCAN_JMPSIZE;
idata = 4;
return;
}
if (strcmp(s, "FAR") == 0)
{ // Jump to different code segment
scan = SCAN_JMPSIZE;
idata = 8;
return;
}
if (strcmp(s, "LOCAL") == 0 && *asmcmd == '.')
{
asmcmd++;
while (*asmcmd == ' ' || *asmcmd == '\t')
asmcmd++; // Skip trailing spaces
if (!isdigit(*asmcmd))
{
asmerror = "Integer number expected";
scan = SCAN_ERR;
return;
}
while (isdigit(*asmcmd)) // LOCAL index is decimal number!
idata = idata * 10 + (*asmcmd++) - '0';
scan = SCAN_LOCAL;
return;
}
if (strcmp(s, "ARG") == 0 && *asmcmd == '.')
{
asmcmd++;
while (*asmcmd == ' ' || *asmcmd == '\t')
asmcmd++; // Skip trailing spaces
if (!isdigit(*asmcmd))
{
asmerror = "Integer number expected";
scan = SCAN_ERR;
return;
}
while (isdigit(*asmcmd)) // ARG index is decimal number!
idata = idata * 10 + (*asmcmd++) - '0';
scan = SCAN_ARG;
return;
}
if (strcmp(s, "REP") == 0)
{
scan = SCAN_REP; return;
} // REP prefix
if (strcmp(s, "REPE") == 0 || strcmp(s, "REPZ") == 0)
{
scan = SCAN_REPE; return;
} // REPE prefix
if (strcmp(s, "REPNE") == 0 || strcmp(s, "REPNZ") == 0)
{
scan = SCAN_REPNE; return;
} // REPNE prefix
if (strcmp(s, "LOCK") == 0)
{
scan = SCAN_LOCK; return;
} // LOCK prefix
if (strcmp(s, "PTR") == 0)
{
scan = SCAN_PTR; return;
} // PTR in MASM addressing statements
if (strcmp(s, "CONST") == 0 || strcmp(s, "OFFSET") == 0)
{
scan = SCAN_OFS; return;
} // Present but undefined offset/constant
if (strcmp(s, "SIGNED") == 0)
{
scan = SCAN_SIGNED; return;
} // Keyword "SIGNED" (in expressions)
if (strcmp(s, "UNSIGNED") == 0)
{
scan = SCAN_UNSIGNED; return;
}; // Keyword "UNSIGNED" (in expressions)
if (strcmp(s, "CHAR") == 0)
{
scan = SCAN_CHAR; return;
} // Keyword "CHAR" (in expressions)
if (strcmp(s, "FLOAT") == 0)
{
scan = SCAN_FLOAT; return;
} // Keyword "FLOAT" (in expressions)
if (strcmp(s, "DOUBLE") == 0)
{
scan = SCAN_DOUBLE; return;
} // Keyword "DOUBLE" (in expressions)
if (strcmp(s, "FLOAT10") == 0)
{
scan = SCAN_FLOAT10; return;
} // Keyword "FLOAT10" (in expressions)
if (strcmp(s, "STRING") == 0)
{
scan = SCAN_STRING; return;
} // Keyword "STRING" (in expressions)
if (strcmp(s, "UNICODE") == 0)
{
scan = SCAN_UNICODE; return;
} // Keyword "UNICODE" (in expressions)
if (strcmp(s, "MSG") == 0)
{
scan = SCAN_MSG; return;
} // Pseudovariable MSG (in expressions)
if (mode & SA_NAME)
{
idata = i;
scan = SCAN_NAME; // Don't try to decode symbolic label
return;
};
asmerror = "Unknown identifier";
scan = SCAN_ERR;
return;
}
else if (isdigit(*asmcmd))
{ // Constant
base = 0; maxdigit = 0; decimal = hex = 0L; floating = 0.0;
if (asmcmd[0] == '0' && toupper(asmcmd[1]) == 'X')
{
base = 16;
asmcmd += 2;
} // Force hexadecimal number
while (1)
{
if (isdigit(*asmcmd))
{
decimal = decimal * 10 + (*asmcmd) - '0';
floating = floating * 10.0 + (*asmcmd) - '0';
hex = hex * 16 + (*asmcmd) - '0';
if (maxdigit == 0) maxdigit = 9;
asmcmd++;
}
else if (isxdigit(*asmcmd))
{
hex = hex * 16 + toupper(*asmcmd++) - 'A' + 10;
maxdigit = 15;
}
else break;
}
if (maxdigit == 0)
{
asmerror = "Hexadecimal digits after 0x... expected";
scan = SCAN_ERR;
return;
}
if (toupper(*asmcmd) == 'H')
{ // Force hexadecimal number
if (base == 16)
{
asmerror = "Please don't mix 0xXXXX and XXXXh forms";
scan = SCAN_ERR;
return;
}
asmcmd++;
idata = hex; scan = SCAN_ICONST;
while (*asmcmd == ' ' || *asmcmd == '\t') asmcmd++;
return;
};
if (*asmcmd == '.')
{ // Force decimal number
if (base == 16 || maxdigit > 9)
{
asmerror = "Not a decimal number";
scan = SCAN_ERR;
return;
}
asmcmd++;
if (isdigit(*asmcmd) || toupper(*asmcmd) == 'E')
{
divisor = 1.0;
while (isdigit(*asmcmd))
{ // Floating-point number
divisor /= 10.0;
floating += divisor * (*asmcmd - '0');
asmcmd++;
}
if (toupper(*asmcmd) == 'E')
{
asmcmd++;
if (*asmcmd == '-')
{
base = -1;
asmcmd++;
}
else base = 1;
if (!isdigit(*asmcmd))
{
asmerror = "Invalid exponent";
scan = SCAN_ERR;
return;
}
decimal = 0;
while (isdigit(*asmcmd))
{
if (decimal < 65536L) decimal = decimal * 10 + (*asmcmd++) - '0';
}
floating *= pow(10, (double)decimal * base);//修复
}
fdata = floating;
scan = SCAN_FCONST;
return;
}
else
{
idata = decimal;
scan = SCAN_DCONST;
while (*asmcmd == ' ' || *asmcmd == '\t')
asmcmd++;
return;
}
}
idata = hex;
scan = SCAN_ICONST; // Default is hexadecimal
while (*asmcmd == ' ' || *asmcmd == '\t')
asmcmd++;
return;
}
else if (*asmcmd == '\'')
{ // Character constant
asmcmd++;
if (*asmcmd == '\0' || (*asmcmd == '\\' && asmcmd[1] == '\0'))
{
asmerror = "Unterminated character constant";
scan = SCAN_ERR;
return;
}
if (*asmcmd == '\'')
{
asmerror = "Empty character constant";
scan = SCAN_ERR;
return;
}
if (*asmcmd == '\\') asmcmd++;
idata = *asmcmd++;
if (*asmcmd != '\'')
{
asmerror = "Unterminated character constant";
scan = SCAN_ERR;
return;
}
asmcmd++;
while (*asmcmd == ' ' || *asmcmd == '\t') asmcmd++;
scan = SCAN_ICONST;
return;
}
else
{ // Any other character or combination
idata = sdata[0] = *asmcmd++; sdata[1] = sdata[2] = '\0';
if (idata == '|' && *asmcmd == '|')
{
idata = '||';
prio = 10; // '||'
sdata[1] = *asmcmd++;
}
else if (idata == '&' && *asmcmd == '&')
{
idata = '&&';
prio = 9; // '&&'
sdata[1] = *asmcmd++;
}
else if (idata == '=' && *asmcmd == '=')
{
idata = '==';
prio = 5; // '=='
sdata[1] = *asmcmd++;
}
else if (idata == '!' && *asmcmd == '=')
{
idata = '!=';
prio = 5; // '!='
sdata[1] = *asmcmd++;
}
else if (idata == '<' && *asmcmd == '=')
{
idata = '<=';
prio = 4; // '<='
sdata[1] = *asmcmd++;
}
else if (idata == '>' && *asmcmd == '=')
{
idata = '>=';
prio = 4; // '>='
sdata[1] = *asmcmd++;
}
else if (idata == '<' && *asmcmd == '<')
{
idata = '<<';
prio = 3; // '<<'
sdata[1] = *asmcmd++;
}
else if (idata == '>' && *asmcmd == '>')
{
idata = '>>';
prio = 3; // '>>'
sdata[1] = *asmcmd++;
}
else if (idata == '|') prio = 8; // '|'
else if (idata == '^') prio = 7; // '^'
else if (idata == '&') prio = 6; // '&'
else if (idata == '<')
{
if (*asmcmd == '&')
{ // Import pseudolabel (for internal use)
if ((mode & SA_IMPORT) == 0)
{
asmerror = "Syntax error";
scan = SCAN_ERR;
return;
}
asmcmd++; i = 0;
while (*asmcmd != '\0' && *asmcmd != '>')
{
sdata[i++] = *asmcmd++;
if (i >= sizeof(sdata))
{
asmerror = "Too long import name";
scan = SCAN_ERR;
return;
}
}
if (*asmcmd != '>')
{
asmerror = "Unterminated import name";
scan = SCAN_ERR;
return;
}
asmcmd++; sdata[i] = '\0';
scan = SCAN_IMPORT; return;
}
else prio = 4;
} // '<'
else if (idata == '>') prio = 4; // '>'
else if (idata == '+') prio = 2; // '+'
else if (idata == '-') prio = 2; // '-'
else if (idata == '*') prio = 1; // '*'
else if (idata == '/') prio = 1; // '/'
else if (idata == '%') prio = 1; // '%'
else if (idata == ']')
{
pcmd = asmcmd;
Scanasm(SA_NAME);
if (scan != SCAN_SYMB || idata != '[')
{
idata = ']';
asmcmd = pcmd;
prio = 0;
}
else
{
idata = '+';
prio = 2; // Translate '][' to '+'
}
}
else prio = 0; // Any other character
scan = SCAN_SYMB;
return;
}
}
// Fetches one complete operand from the input line and fills in structure op
// with operand's data. Expects that first token of the operand is already
// scanned. Supports operands in generalized form (for example, R32 means any
// of general-purpose 32-bit integer registers).
// 从输入行获取一个完整的操作数,并用操作数的数据填充结构op
// 期望已扫描操作数的第一个标记
// 支持通用形式的操作数(例如,R32表示任何通用32位整数寄存器)
static void Parseasmoperand(t_asmoperand* op)
{
int i = 0, j = 0, bracket = 0, sign = 0, xlataddr = 0;
int reg = 0, r[9] = { 0 };
long offset;
if (scan == SCAN_EOL || scan == SCAN_ERR)
return; // No or bad operand
// Jump or call address may begin with address size modifier(s) SHORT, LONG,
// NEAR and/or FAR. Not all combinations are allowed. After operand is
// completely parsed, this function roughly checks whether modifier is
// allowed. Exact check is done in Assemble().
if (scan == SCAN_JMPSIZE)
{
j = 0;
while (scan == SCAN_JMPSIZE)
{
j |= idata; // Fetch all size modifiers
Scanasm(0);
}
if (((j & 0x03) == 0x03) || ((j & 0x0C) == 0x0C) || ((j & 0x09) == 0x09))
{
asmerror = "Invalid combination of jump address modifiers";
scan = SCAN_ERR;
return;
}
if ((j & 0x08) == 0) j |= 0x04; // Force NEAR if not FAR
op->jmpmode = j;
}
// Simple operands are either register or constant, their processing is
// obvious and straightforward.
if (scan == SCAN_REG8 || scan == SCAN_REG16 || scan == SCAN_REG32)
{
op->type = REG;
op->index = idata; // Integer general-purpose register
if (scan == SCAN_REG8) op->size = 1;
else if (scan == SCAN_REG16) op->size = 2;
else op->size = 4;
}
else if (scan == SCAN_FPU)
{ // FPU register
op->type = RST;
op->index = idata;
}
else if (scan == SCAN_MMX)
{ // MMX or 3DNow! register
op->type = RMX;
op->index = idata;
}
else if (scan == SCAN_CR)
{ // Control register
op->type = CRX;
op->index = idata;
}
else if (scan == SCAN_DR)
{ // Debug register
op->type = DRX;
op->index = idata;
}
else if (scan == SCAN_SYMB && idata == '-')
{
Scanasm(0); // Negative constant
if (scan != SCAN_ICONST && scan != SCAN_DCONST && scan != SCAN_OFS)
{
asmerror = "Integer number expected";
scan = SCAN_ERR;
return;
}
op->type = IMM; op->offset = -idata;
if (scan == SCAN_OFS) op->anyoffset = 1;
}
else if (scan == SCAN_SYMB && idata == '+')
{
Scanasm(0); // Positive constant
if (scan != SCAN_ICONST && scan != SCAN_DCONST && scan != SCAN_OFS)
{
asmerror = "Integer number expected";
scan = SCAN_ERR;
return;
}
op->type = IMM;
op->offset = idata;
if (scan == SCAN_OFS) op->anyoffset = 1;
}
else if (scan == SCAN_ICONST || scan == SCAN_DCONST || scan == SCAN_OFS)
{
j = idata;
if (scan == SCAN_OFS) op->anyoffset = 1;
Scanasm(0);
if (scan == SCAN_SYMB && idata == ':')
{
Scanasm(0); // Absolute long address (seg:offset)
if (scan != SCAN_ICONST && scan != SCAN_DCONST && scan != SCAN_OFS)
{
asmerror = "Integer address expected";
scan = SCAN_ERR;
return;
}
op->type = JMF;
op->offset = idata;
op->segment = j;
if (scan == SCAN_OFS) op->anyoffset = 1;
}
else
{
op->type = IMM;
op->offset = j; // Constant without sign
return; // Next token already scanned
}
}
else if (scan == SCAN_FCONST)
{
asmerror = "Floating-point numbers are not allowed in command";
scan = SCAN_ERR;
return;
}
// Segment register or address.
else if (scan == SCAN_SEG || scan == SCAN_OPSIZE || (scan == SCAN_SYMB && idata == '['))
{ // Segment register or address
bracket = 0;
if (scan == SCAN_SEG)
{
j = idata; Scanasm(0);
if (scan != SCAN_SYMB || idata != ':')
{
op->type = SGM; op->index = j; // Segment register as operand
return;
} // Next token already scanned
op->segment = j; Scanasm(0);
}
// Scan 32-bit address. This parser does not support 16-bit addresses.
// First of all, get size of operand (optional), segment register (optional)
// and opening bracket (required).
while (1)
{
if (scan == SCAN_SYMB && idata == '[')
{
if (bracket)
{ // Bracket
asmerror = "Only one opening bracket allowed";
scan = SCAN_ERR;
return;
}
bracket = 1;
}
else if (scan == SCAN_OPSIZE)
{
if (op->size != 0)
{ // Size of operand
asmerror = "Duplicated size modifier";
scan = SCAN_ERR;
return;
}
op->size = idata;
}
else if (scan == SCAN_SEG)
{
if (op->segment != SEG_UNDEF)
{ // Segment register
asmerror = "Duplicated segment register";
scan = SCAN_ERR;
return;
};
op->segment = idata; Scanasm(0);
if (scan != SCAN_SYMB || idata != ':')
{
asmerror = "Semicolon expected";
scan = SCAN_ERR;
return;
};
}
else if (scan == SCAN_ERR)
return;
else break; // None of expected address elements
Scanasm(0);
}
if (bracket == 0)
{
asmerror = "Address expression requires brackets";
scan = SCAN_ERR;
return;
}
// Assembling a 32-bit address may be a kind of nigthmare, due to a large
// number of allowed forms. Parser collects immediate offset in op->offset
// and count for each register in array r[]. Then it decides whether this
// combination is valid and determines scale, index and base. Assemble()
// will use these numbers to select address form (with or without SIB byte,
// 8- or 32-bit offset, use segment prefix or not). As a useful side effect
// of this technique, one may specify, for example, [EAX*5] which will
// correctly assemble to [EAX*4+EAX].
for (i = 0; i <= 8; i++) r[i] = 0;
sign = '+'; // Default sign for the first operand
xlataddr = 0;
while (1)
{ // Get SIB and offset
if (scan == SCAN_SYMB && (idata == '+' || idata == '-'))
{
sign = idata;
Scanasm(0);
}
if (scan == SCAN_ERR) return;
if (sign == '?')
{
asmerror = "Syntax error";
scan = SCAN_ERR;
return;
}
// Register AL appears as part of operand of (seldom used) command XLAT.
if (scan == SCAN_REG8 && idata == REG_EAX)
{
if (sign == '-')
{
asmerror = "Unable to subtract register";
scan = SCAN_ERR;
return;
}
if (xlataddr != 0)
{
asmerror = "Too many registers";
scan = SCAN_ERR;
return;
}
xlataddr = 1;
Scanasm(0);
}
else if (scan == SCAN_REG16)
{
asmerror = "Sorry, 16-bit addressing is not supported";
scan = SCAN_ERR;
return;
}
else if (scan == SCAN_REG32)
{
if (sign == '-')
{
asmerror = "Unable to subtract register";
scan = SCAN_ERR;
return;
}
reg = idata;
Scanasm(0);
if (scan == SCAN_SYMB && idata == '*')
{
Scanasm(0); // Try index*scale
if (scan == SCAN_ERR) return;
if (scan == SCAN_OFS)
{
asmerror = "Undefined scale is not allowed";
scan = SCAN_ERR;
return;
}
if (scan != SCAN_ICONST && scan != SCAN_DCONST)
{
asmerror = "Syntax error";
scan = SCAN_ERR;
return;
}
if (idata == 6 || idata == 7 || idata > 9)
{
asmerror = "Invalid scale";
scan = SCAN_ERR;
return;
}
r[reg] += idata;
Scanasm(0);
}
else r[reg]++;
} // Simple register
else if (scan == SCAN_LOCAL)
{
r[REG_EBP]++;
op->offset -= idata * 4;
Scanasm(0);
}
else if (scan == SCAN_ARG)
{
r[REG_EBP]++;
op->offset += (idata + 1) * 4;
Scanasm(0);
}
else if (scan == SCAN_ICONST || scan == SCAN_DCONST)
{
offset = idata; Scanasm(0);
if (scan == SCAN_SYMB && idata == '*')
{
Scanasm(0); // Try scale*index
if (scan == SCAN_ERR) return;
if (sign == '-')
{
asmerror = "Unable to subtract register";
scan = SCAN_ERR;
return;
}
if (scan == SCAN_REG16)
{
asmerror = "Sorry, 16-bit addressing is not supported";
scan = SCAN_ERR;
return;
}
if (scan != SCAN_REG32)
{
asmerror = "Syntax error";
scan = SCAN_ERR;
return;
}
if (offset == 6 || offset == 7 || offset > 9)
{
asmerror = "Invalid scale";
scan = SCAN_ERR;
return;
}
r[idata] += offset;
Scanasm(0);
}
else
{
if (sign == '-')
op->offset -= offset;
else
op->offset += offset;
}
}
else if (scan == SCAN_OFS)
{
Scanasm(0);
if (scan == SCAN_SYMB && idata == '*')
{
asmerror = "Undefined scale is not allowed";
scan = SCAN_ERR;
return;
}
else
{
op->anyoffset = 1;
}
}
else break; // None of expected address elements
if (scan == SCAN_SYMB && idata == ']') break;
sign = '?';
}
if (scan == SCAN_ERR) return;
if (scan != SCAN_SYMB || idata != ']')
{
asmerror = "Syntax error";
scan = SCAN_ERR;
return;
}
// Process XLAT address separately.
if (xlataddr != 0)
{ // XLAT address in form [EBX+AX]
for (i = 0; i <= 8; i++)
{ // Check which registers used
if (i == REG_EBX) continue;
if (r[i] != 0) break;
}
if (i <= 8 || r[REG_EBX] != 1 || op->offset != 0 || op->anyoffset != 0)
{
asmerror = "Invalid address";
scan = SCAN_ERR;
return;
}
op->type = MXL;
}
// Determine scale, index and base.
else
{
j = 0; // Number of used registers
for (i = 0; i <= 8; i++)