-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAssemblerU.~pa
3204 lines (3061 loc) · 121 KB
/
AssemblerU.~pa
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
unit AssemblerU;
{
This is the main unit for assembling code.
It loads instruction set information from textfiles into SymbolConvertions and Opcode.
This information is then used during the assembling process to determine what
machine code corresponds to assembly code.
The textfiles contain binary strings. The machine codes are generated by adding
these strings together. There are examples in the instruction set file to help
illustrate this.
The instruction set files contain symbols which also include registers. These
are used for operands. There are a few "symbols" that are defined in this program.
for example, i8, i16, mem8, mem16
future changes
- add support for EQU operator.(similar to #DEFINE in c or const in Delphi)
ie. CR EQU 0Dh
DOLLAR EQU '$'
- support for includes
ie. include SE.ASM
- proc declarations
ie. Textln PROC
Addressing modes still need to be supported.
Here are some sites with information on this.
http://www.cs.tut.fi/~siponen/upros/intel/intel_addr.html
}
{$define WeAreDebugging}
// leave out this directive to prevent certain error finding code from being used
interface
uses classes,windows;
type
// ---------------------------------------------------------------------
SymbolConvertion = record
Symbol: string;
// contains a symbol ie. 'ax', 'cx', 'cl'...
Binary: string; // contains the binary number the symbol is converted to
end;
InstructionSetID = record
FileName: string;
Colour: integer;
end;
MyOperand = record
operandDef: integer;
NoMachineCode: boolean; // true only when the operand does not add any binary to the machine code
{ some instructions only accept 1 register. for example "xor ax, 0030h"
for that instruction the opcode and the immediate value are the only
things affecting the machine code and the register is already known
since no other registers can use them
}
Name: string; // ie. 'reg8', 'reg16', 'stck'(floating stack)...
end;
MyAttribute = record
name: string; // name of the property
offset: integer; // number of bytes offset into the struct
end;
MyStruct = record
name: string; // the name of the structure
attribute: array of MyAttribute; // attributes of the structure
end;
MyOpcode = record
Processor: string;
// stores the version of processor, the instruction was first used in
// ie. '80186' for push i8 and push i16
opcode: string; // ie. 'mov', 'lea', 'pop'...
Operand1: MyOperand;
operand2: MyOperand; // ie. 'i8', 'reg8', reg16'...
operand3: MyOperand; // rarely used
OpcodeInBinary: string; // ie. '10110'
Colour: integer;
typeID: byte; // identifies the type of instruction it is.
{
optDefault is NormOpcode
optPref is for prefixes
}
ModeDefined: boolean; // true when the location of the addressing mode bits is defined
MachAfterMode: string; // machine code bits placed to the right
// of the addressing mode in the instruction
DIASMOnly: boolean; // true when the instruction is only to be used for disassembling purposes.
// This means there is another instruction that is more efficient.
asmonly: boolean; // for instructions that are not to be disassembled(this means the identical machine code is used for another assembly statement)
RevMachOperand: boolean; // true if the machine code for the operands is reversed.
// if true, the machine code for the first operand that you see in the the assembly statement, goes after the machine code of the second operand.
branch: boolean; // indicates that the instruction does or can change the IP value
// jumps and calls would be such instructions.
// This is used when disassembling programs so the disassembler can branch just like the processor does
StopDiasm: boolean;
{ when an instruction with this set to true is found, the
disassembler will stop. Data after this instruction will be made into a hex dump}
end;
MyDefine = record
name: string; // ie. 'reg8'
values: array of string; // ie. ('al','bl','cl','dl','ah','bh','ch','dh')
end;
MyCodeLabel = record
name: string; // name of the label
StaticAddress16: integer;
// The address of a label relative to the start of the program.
// With COM programs this is the lower 16 bit value of the RAM address
end;
// ---------------------------------------------------------------------
tDebugOutput = class
private
pTextFile: Textfile;
pFileAssigned: boolean;
pOutputToGUI: boolean;
pFileName: string;
function GetOutputToGUI: boolean;
procedure SetOutputToGUI(NewVal: boolean);
function GetFileSet: boolean;
function GetFileName: string;
procedure SetFileName(NewFN: string);
public
procedure Output(s: string);
procedure AssignF(fn: string); overload;
procedure AssignF; overload;
procedure CloseF;
constructor Create;
property FileName: string read GetFileName write SetFileName;
property FileSet: boolean read GetFileSet;
property OutputToGUI: boolean read GetOutputToGUI write SetOutputToGUI;
end;
// -------------------------------------------
procedure FreeAssemblerU;
procedure LoadInstructionSet(FileName: string);
function AssembleTStringsToFile(code: TStrings; COMFileName: string): string;
function AssembleSourceFileToProgramFile(SourceFileName,COMFileName: string): string;
function ExecuteFile(FileName, DefaultDir: string): HWND;
function MakeHTMLOpcodeTable(fn: string): string;
function MakeSourceCodeHTMLDocument(code: TStrings;fn,Title: string): string;
procedure DisassembleCOMProgram(Code: TStrings; COMFN: string);
procedure InitializeAssemblerU;
function ReferenceToBinary(OpDefine: string; MemRef: string;ShowDebug: boolean;var mode: byte; var error: boolean;var prefix: string): string;
procedure DisassembleEXEProgram(Code: TStrings; EXEFN: string);
procedure MakeOpcodeHeader;
var
OutputDebug: tDebugOutput;
OutputDebugMessages: boolean;
MakeAssembleProcessDoc: boolean;
CaseSensative: boolean; // set to false when things are to be converted to lower case before comparing to find a match
CurrentLine: integer; // the current line being assembled
CurrentByteCompiling: integer;
implementation
uses SysUtils,Dialogs,StringManiU,ShellAPI,MainU;
type ByteArray = array of byte;
Equate = record
identifier, NewValue: string;
end;
var
CompiledProgram: ByteArray;
MyCritError: boolean; // true only when an error occurs that would cause
SetFiles: array of InstructionSetID; // used to list all instruction set files that have already been loaded to prevent eternal recursive loops
Symbols: array of SymbolConvertion; // registers and things that need converted
Opcode: array of MyOpcode;
Defines: array of MyDefine;
Structs: array of MyStruct; // structures defined in the assembly code
Labels: array of MyCodeLabel; // labels used to address different areas in the program
OpcodeTypes: array of string; // values loaded from instruction set files
radix: word; { defined the default number base when there are no trailing
letters to define the number base of an immediate value }
const
// defines
MemReference = -2;
ImmediateValueDefine = -1;
// opcode types
optDefault = 0; // the default value
optPref = 1; // prefixes
Prefixes: array[0..5] of string = ('00101110','00111110','00100110','01100100','01100101','00110110');
// Segment Register Overrides used for memory references (indexes into the prefix array)
CSoverride = 0;
DSoverride = 1;
ESoverride = 2;
FSoverride = 3;
GSoverride = 4;
SSoverride = 5;
function ExecuteFile(FileName, DefaultDir: string): HWND;
begin
Result := ShellExecute(MainFrm.handle,nil,PChar(FileName), '',
PChar(DefaultDir), SW_SHOWNORMAL);
end;
// --------------------- tDebugOutput Class -------------------------
constructor tDebugOutput.Create;
begin
// inherited create;
pFileAssigned:=false;
pOutputToGUI:=true;
pFileName:='DebugData.txt';
end;
procedure tDebugOutput.AssignF(fn: string);
begin
pFileName:=fn;
AssignFile(pTextFile,fn);
Reset(pTextFile);
pFileAssigned:=true;
end;
procedure tDebugOutput.AssignF;
begin
if pFileName<>'' then
begin
AssignFile(pTextFile,pfilename);
Rewrite(pTextFile);
pFileAssigned:=true;
end
else
ShowMessage('error assigning a file to a blank file name');
end;
procedure tDebugOutput.CloseF;
begin
if pFileAssigned then
CloseFile(pTextFile);
pFileAssigned:=false;
end;
procedure tDebugOutput.Output(s: string);
begin
if OutputDebugMessages then
begin
s:=IntToStr(CurrentLine)+': '+s;
if pFileAssigned and not pOutputToGUI then
WriteLn(pTextFile,s)
else if pOutputToGUI then
MainFrm.DebugOutput.lines.add(s)
else
ShowMessage('Error writing to a file that hasn''t been assigned.');
end;
end;
procedure tDebugOutput.SetOutputToGUI(NewVal: boolean);
begin
pOutputToGUI:=NewVal;
if NewVal then
Self.CloseF;
end;
function tDebugOutput.GetFileSet: boolean;
begin
result:=(pFileName<>''); // return false if the filename is blank
end;
function tDebugOutput.GetFileName: string;
begin
result:=pFileName;
end;
procedure tDebugOutput.SetFileName(NewFN: string);
begin
if pFileName='' then
OutputToGUI:=true
else
pFileName:=NewFN;
end;
function tDebugOutput.GetOutputToGUI: boolean;
begin
result:=pOutputToGUI;
end;
// ---------------------------------------------------------
procedure FreeInstructionSet;
var
x: integer;
begin
Symbols:=nil;
Opcode:=nil;
for x:=high(Defines) downto 0 do
Defines[x].Values:=nil;
Defines:=nil;
end;
procedure FreeAssemblerU; // free memory used in the unit
begin
FreeInstructionSet;
OutputDebug.Destroy;
end;
procedure InitializeAssemblerU;
begin
MakeAssembleProcessDoc:=true;
OutputDebug:=tDebugOutput.Create;
end;
function MyLowerCase(s: string): string;
begin
if CaseSensative then
result:=s
else
result:=LowerCase(s);
end;
function Opcode1MoreEfficientThan2(var op1,op2: MyOpcode): boolean;
var
c2,c1: byte;
ResultSet: boolean;
begin
result:=false;
if (op1.Operand1.Name='')and(op2.Operand1.Name<>'') then
result:=true
else if op2.Operand1.Name='' then
result:=false
else
begin
ResultSet:=false;
c2:=0;
if op2.Operand1.NoMachineCode then
inc(c2);
if op2.Operand2.NoMachineCode or (op2.Operand2.Name='') then
inc(c2);
c1:=0;
if op1.Operand1.NoMachineCode then
inc(c1);
if op1.Operand2.NoMachineCode or (op1.Operand2.Name='') then
inc(c1);
if c1=c2 then
begin
if ((op1.Operand1.Name='i8')or(op1.Operand1.Name='i16'))
and((op2.Operand1.Name='i8')or(op2.Operand1.Name='i16')) then
begin
if op1.Operand1.Name<op2.Operand1.Name then // if op1.Operand1.Name='i16' and op2.Operand1.Name='i8'
begin
result:=false; // op1 is less efficient than op2
ResultSet:=true;
end;
end;
if not ResultSet then
begin
if ((op1.Operand2.Name='i8')or(op1.Operand2.Name='i16'))
and((op2.Operand2.Name='i8')or(op2.Operand2.Name='i16')) then
begin
if op1.Operand1.Name<op2.Operand1.Name then // if op1.Operand1.Name='i16' and op2.Operand1.Name='i8'
begin
result:=false;
ResultSet:=true;
end;
if not ResultSet then
if Length(op1.OpcodeInBinary)>Length(op2.OpcodeInBinary) then
begin
result:=false;// op1 is less efficient than op2
ResultSet:=true;
end;
end;
if ((op2.Operand2.Name='i8')or(op2.Operand2.Name='i16'))
and not ((op1.Operand2.Name='i8')or(op1.Operand2.Name='i16')or(op1.Operand2.Name='')) then
begin
result:=true;
ResultSet:=true;
end;
end;
end
else
result:=(c1>c2);
end;
end;
function MachineGreater(i1,i2: integer): boolean;
// used by a sort procedure
var
c1,c2: integer;
begin
c1:=Length(Opcode[i1].OpcodeInBinary);
c2:=Length(Opcode[i2].OpcodeInBinary);
if Opcode[i1].ModeDefined then
inc(c1,5+Length(Opcode[i1].MachAfterMode));
if Opcode[i2].ModeDefined then
inc(c2,5+Length(Opcode[i2].MachAfterMode));
if c1>c2 then
result:=true
else
begin
{ if Opcode[i1].OpcodeInBinary>Opcode[i2].OpcodeInBinary then
result:=true
else }
result:=false;
end;
end;
procedure SwapOpcodes(i1,i2: integer);
var
buf: MyOpcode; // a buffer
begin
buf:=Opcode[i1];
Opcode[i1]:=Opcode[i2];
Opcode[i2]:=buf;
end;
procedure SortOpcodesAlphabetically;
// use selection sort to sort the opcodes alphabetically
var
x: integer;
Sorted: boolean;
begin
Sorted:=false;
while not Sorted do
begin
Sorted:=true;
for x:=high(Opcode)-1 downto 0 do
begin
if Opcode[x].Opcode>Opcode[x+1].Opcode then
begin
SwapOpcodes(x,x+1);
Sorted:=false;
end;
end;
end;
end;
procedure SortOpcodesEfficiency;
// use selection sort to sort the opcodes by efficiency
var
x: integer;
Sorted: boolean;
begin
Sorted:=false;
while not Sorted do
begin
Sorted:=true;
for x:=high(Opcode)-1 downto 0 do
begin
if Opcode1MoreEfficientThan2(Opcode[x],Opcode[x+1]) then
begin
SwapOpcodes(x,x+1);
Sorted:=false;
end;
end;
end;
end;
procedure SortOpcodesMachine;
// use selection sort to sort the opcodes alphabetically
var
x: integer;
Sorted: boolean;
begin
Sorted:=false;
while not Sorted do
begin
Sorted:=true;
for x:=high(Opcode)-1 downto 0 do
begin
if MachineGreater(x,x+1) then
begin
SwapOpcodes(x,x+1);
Sorted:=false;
end;
end;
end;
end;
function GetDefine(operand: string): integer;
// returns the index of the define containing the operand
var
x,y: integer;
b: byte;
pref: string;
error: boolean;
begin
operand:=LowerCase(operand);
result:=ImmediateValueDefine; // immediate values
for x:=high(Defines) downto 0 do
if Defines[x].name=Operand then
begin
result:=x;
exit;
end;
if result<0 then
for x:=high(Defines) downto 0 do
with Defines[x] do
for y:=high(Values) downto 0 do
if Values[y]=operand then
begin
result:=x;
exit;
end;
if (result<0)and(operand<>'') then
begin
if ReferenceToBinary('mem',operand,false,b,error,pref)<>'' then
begin
if not error then
result:=MemReference;
end
else if (operand='mem'){any memory reference}or(operand='mem8'{reference to 8-bit value})
or(operand='mem16'{reference to 16-bit value})or(operand='mem32'{reference to 32-bit value})
or(operand='mem64'{reference to 64-bit value})or(operand='mem80'{reference to 80-bit value})
or(operand='mem128'{reference to 128-bit value}) then
result:=MemReference;
end;
end;
function GetSymbol(s: string): integer;
var
x: integer;
begin
result:=-1;
s:=LowerCase(s);
for x:=high(Symbols) downto 0 do
if Symbols[x].Symbol=s then
begin
result:=x;
break;
end;
end;
function GetMyLabel(LBLName: string; ShowDebug: boolean): MyCodeLabel;
var
x: integer;
begin
result.name:='';
result.StaticAddress16:=$100; // default values if the label is not found
for x:=high(Labels) downto 0 do
if LBLName = Labels[x].name then
begin
result:=Labels[x];
break; // break the for loop
end;
if ShowDebug then
if result.name='' then
OutputDebug.Output('Undefined label: "'+LBLName+'"');
end;
// ------------------- number base convertions ------------------------------
function InCPPHex(HexStr: string): boolean;
begin
result:=false;
HexStr:=LowerCase(HexStr);
if copy(HexStr,1,2)='0x' then
result:=true
else if copy(HexStr,1,3)='-0x' then
result:=true;
end;
function DecToInt(DecStr: string;var error: boolean): integer;
var
c: char;
pos1: integer;
begin
result:=0;
if DecStr='' then
error:=true
else
begin
c:=DecStr[Length(DecStr)];
if (c='d')or(c='D') then
DecStr:=Copy(DecStr,1,Length(DecStr)-1);
val(DecStr,result,pos1);
if pos1<>0 then
error:=true;
end;
end;
function HexToInt(HexStr: string;var error: boolean): integer;
// HexStr must be in lowercase
// HexStr = ie. '- 2424ac45h'
var
signed: boolean;
len1: integer;
c: byte; // stores the ascii value of a hex digit
d: byte; // stores the binary digit
begin
result:=0;
ClearAllSpaces(HexStr);
if HexStr='' then Exit;
Signed:=(HexStr[1]='-');
if HexStr[length(HexStr)]<>'h' then
HexStr:=HexStr+'h';
if not signed then
HexStr:=' '+HexStr;
len1:=length(HexStr)-1;
for d:=len1 downto 2 do
begin
c:=ord(HexStr[d]);
if (c>=97)and(c<=102) then
result:=result or ((c-87) shl ((len1-d) shl 2))
else if (c>=48)and(c<=57) then
result:=result or ((c-48) shl ((len1-d) shl 2))
else
begin
OutputDebug.Output('Invalid hex digit "'+HexStr[d]+'" in '+HexStr);
error:=true;
break;
end;
end;
if signed then
result:=-result;
end;
function CPPHexToInt(HexStr: string;var error: boolean): integer;
// HexStr must be in lower case
// HexStr = ie. '-0x2424ac45'
var
signed: boolean;
len1: integer;
c: byte; // stores the ascii value of a hex digit
d: byte; // stores the binary digit
begin
result:=0;
error:=false;
ClearAllSpaces(HexStr);
if HexStr='' then Exit;
Signed:=(HexStr[1]='-');
if not signed then
HexStr:=' '+HexStr;
len1:=length(HexStr);
// ie. HexStr = ' 0x02FF'
for d:=len1 downto 4 do
begin
c:=ord(HexStr[d]);
if (c>=97)and(c<=102) then
result:=result or ((c-87) shl ((len1-d) shl 2))
else if (c>=48)and(c<=57) then
result:=result or ((c-48) shl ((len1-d) shl 2))
else
begin
OutputDebug.Output('Invalid hex digit: '+HexStr);
error:=true;
break;
end;
end;
if signed then
result:=-result;
end;
function BinToInt(binStr: string;var error: boolean): integer;
var
signed: boolean;
c: byte; // stores the ascii value of a hex digit
d: byte; // stores the binary digit
begin
error:=false;
result:=0;
ClearAllSpaces(binStr);
if binStr='' then Exit;
Signed:=(binStr[1]='-');
if binStr[length(binStr)]<>'b' then
binStr:=binStr+'b';
if not signed then
binStr:=' '+BinStr;
for d:=length(BinStr)-1 downto 2 do
begin
c:=ord(BinStr[d]);
if (c>=48)and(c<=49) then
result:=result or (ord(c=49) shl (length(BinStr)-1-d))
else
begin
OutputDebug.Output('Invalid binary digit: '+BinStr);
error:=true;
break;
end;
end;
if signed then
result:=-result;
end;
function OctToInt(binStr: string;var error: boolean): integer;
var
signed: boolean;
c: byte; // stores the ascii value of a hex digit
d: byte; // stores the binary digit
begin
result:=0;
error:=false;
ClearAllSpaces(binStr);
if binStr='' then Exit;
Signed:=(binStr[1]='-');
if binStr[length(binStr)]<>'o' then
binStr:=binStr+'o';
if not signed then
binStr:=' '+BinStr;
for d:=length(BinStr)-1 downto 2 do
begin
c:=ord(BinStr[d]);
if (c>=48)and(c<=57) then
result:=result or ((c-48) shl (3*(length(BinStr)-1-d)))
else
begin
OutputDebug.Output('Invalid octal digit: '+BinStr);
error:=true;
break;
end;
end;
if signed then
result:=-result;
end;
function IntToBinary(i: integer;Bytes: byte): string;
var
signed: boolean;
d: byte; // stores the binary digit
begin
result:='';
if bytes=0 then
exit;
if (bytes=1)and(i>255) then
OutputDebug.Output('Value out of 8 bit range: '+inttostr(i));
signed:=(i<0);
if signed then
inc(i);
i:=abs(i);
for d:=7 downto 0 do // byte, word, or dword
result:=result+chr(48{ord('0')}+i shr d and 1);
if Bytes>1 then // word or dword
for d:=15 downto 8 do
result:=result+chr(48{ord('0')}+i shr d and 1);
if Bytes=4 then // dword
result:=result+IntToBinary(i shr 16,2);
if signed then // do a logical NOT operation on the digits of a negative number
for d:=length(result) downto 1 do
result[d]:=chr(48+ord(result[d]='0'));
end;
// ----------------------------------------------------------------------------
function ImmediateValueToInt(opa: string;ConvertLabels: boolean; Bytes: byte; var error: boolean): integer;
{
opa = the immediate value ie. opa = '-3423h'
error = set to true if an error occurs
}
var
lbl: MyCodeLabel;
ConvertSuccess: boolean;
begin
ClearFirstSpaces(opa);
result:=0;
error:=false;
if opa='' then
begin
OutputDebug.Output('Error getting an immediate value from a blank');
error:=true;
exit;
end;
if InCPPHex(opa) then // ie. opa='0xFF'
begin
result:=CPPHexToInt(LowerCase(opa),error);
end
else
begin
ConvertSuccess:=true;
case opa[length(opa)] of
'h','H': if OnlyChars(LowerCase(opa),'-0123456789abcdefh') then
result:=HexToInt(LowerCase(opa),error) // hex
else
ConvertSuccess:=false;
'd','D': if OnlyChars(opa,'-0123456789dD') then
result:=DecToInt(opa,error) // decimal(base 10)
else
ConvertSuccess:=false;
'b','B': if OnlyChars(opa,'-01bB') then
result:=BinToInt(opa,error) // binary
else
ConvertSuccess:=false;
'o','O': if OnlyChars(opa,'-01234567oO') then
result:=OctToInt(opa,error) // octal
else
ConvertSuccess:=false;
'''': result:=ord(opa[2]); // ascii characters
else
ConvertSuccess:=false;
end;
if not ConvertSuccess then
begin
error:=false;
if OnlyChars(LowerCase(opa),'-0123456789abcdef') then
begin
case radix of
2: if OnlyChars(opa,'-01') then
result:=BinToInt(opa+'b',error) // binary
else
error:=true;
8: if OnlyChars(opa,'-01234567') then
result:=OctToInt(opa+'o',error) // octal
else
error:=true;
10:
if ValidInt(opa) then
begin
result:=StrToInt(opa); // decimal
end
else
error:=true;
16: result:=HexToInt(LowerCase(opa)+'h',error); // hex
else
error:=true;
end;
end
else
error:=true;
if error then
error:=false
else
exit; // if it was successfully converted to an integer, then exit
if ConvertLabels then
begin
lbl:=GetMyLabel(MyLowerCase(opa),false);
if lbl.name='' then
begin
if (LowerCase(ClearFirstWord(opa))='offset')and(bytes>1) then
begin
lbl:=GetMyLabel(LowerCase(opa),false);
if lbl.name='' then
error:=true
else
result:=lbl.StaticAddress16; // the offset value
end
else
error:=true; // indicate that an error has occured
end
else
begin
if bytes=1 then
result:=lbl.StaticAddress16-CurrentByteCompiling
// address relative to the current instruction
else if bytes=2 then
result:=lbl.StaticAddress16;
// address relative to the beginning of the segment
// label
end;
end
else
// if it can't be converted using all those options, there is an error because it couldn't be converted to an integer.
error:=true;
end;
end;
end;
function GetImmediateValueInBinary(opa: string;Bytes: byte): string;
var
error: boolean;
begin
result:=IntToBinary(ImmediateValueToInt(opa,true,bytes,error),Bytes);
if error then
MyCritError:=true;
end;
procedure CheckForDuplicateInstructions;
var
instr,instr2: integer;
MatchFound: boolean;
s: string;
begin
MatchFound:=false;
for instr:=high(Opcode) downto 0 do
begin
for instr2:=high(Opcode) downto 0 do
if instr2<>instr then
if (Opcode[instr].Opcode=Opcode[instr2].Opcode)and // names match for example, both are 'mov'
(Opcode[instr].OpcodeInBinary=Opcode[instr2].OpcodeInBinary)and // machine code for opcode match
(Opcode[instr].Operand1.Name=Opcode[instr2].Operand1.Name)and // first operands match
(Opcode[instr].Operand2.Name=Opcode[instr2].Operand2.Name)and
(Opcode[instr].DiasmOnly=Opcode[instr2].DiasmOnly) then // second operands match
begin
s:=Opcode[instr].Opcode;
if Opcode[instr].Operand1.Name<>'' then
begin
s:=s+' '+Opcode[instr].Operand1.Name;
if Opcode[instr].Operand2.Name<>'' then
s:=s+', '+Opcode[instr].Operand2.Name;
end;
ShowMessage('An instruction was loaded twice from the instruction set files. The instruction is...'
+#13+s+#13+'There may be more repeated instructions.');
MatchFound:=true;
break;
end;
if MatchFound then
break;
end;
end;
procedure RecLoadInstructionSet(FileName: string);
// loads data for assembling code from a file
// this is recursive because of the include option
procedure FormatOperandName(var Operand: MyOperand);
begin
Operand.NoMachineCode:=false;
if Operand.Name<>'' then
begin
if (Operand.Name[1]='"')and(Operand.Name[Length(Operand.Name)]='"') then
begin
// ie. "al", "ax"...
Operand.NoMachineCode:=true; // no binary code used to define this operand
Operand.Name:=copy(Operand.Name,2,length(Operand.Name)-2);
// remove the quote signs
Operand.OperandDef:=GetDefine(Operand.Name);
end;
end;
end;
function SubStrFound(SubStr: string;var MainString: string): boolean;
var
pos1: integer;
begin
pos1:=pos(SubStr,MainString);
if pos1=0 then
result:=false
else
begin
MainString:=Copy(MainString,1,pos1-1)+Copy(MainString,pos1+Length(SubStr),999);
result:=true;
end;
end;
var
tf: Textfile;
s,s2: string;
x,i: integer; // used to store temperary integer values
InstructionSetFileIndex: integer; // stores the index of this file in the array of instruction set file names
mode: byte;
error: boolean;
SetColour: integer; // a colour for the instructions
const
mdSymbol = 0; // define symbols like registers
mdDefine = 1; // define operand types like reg8, reg16, seg...
mdOpcode = 2; // define opcodes
mdExample = 3; // do nothing, this section is just for
mdOpcodeTypes = 4; // listing and assigning identifiers for the types of opcodes, ie. jcc, call, string...
begin
FileName:=LowerCase(FileName);
if pos(':\',FileName)=0 then
FileName:=MainU.GetProgDir+'\'+FileName;
if FileExists(FileName) then
begin
for x:=high(SetFiles) downto 0 do
begin
if FileName=SetFiles[x].FileName then
exit;
// exit the procedure because this file is already getting
// loaded or has been loaded
end;
SetLength(SetFiles,high(SetFiles)+2);
SetFiles[high(SetFiles)].FileName:=LowerCase(FileName);
InstructionSetFileIndex:=high(SetFiles);
Mode:=mdExample;
AssignFile(tf,FileName);
Reset(tf);
Setcolour:=$FFFFFF; // default colour for the instructions
while not eof(tf) do
begin
Readln(tf,s);
s:=LowerCase(CleanString(s));
if s<>'' then
begin
s2:=ClearFirstWordSpecial(s,' ');
if s2<>'' then
begin
if s2[1]='#' then
begin
if (pos('define',s2)=2) then
mode:=mdDefine
else if (pos('symbol',s2)=2) then
mode:=mdSymbol
else if (pos('opcode',s2)=2) then
mode:=mdOpcode
else if (pos('example',s2)=2) then
mode:=mdExample
else if (pos('colour',s2)=2) then
begin
Setcolour:=ImmediateValueToInt(s,false,4,error);
SetFiles[InstructionSetFileIndex].Colour:=SetColour;
if error then
ShowMessage('Error getting a colour for the instructions in: '+FileName);
end
else if (pos('include',s2)=2) then
begin
x:=pos('<',s);
if x>0 then
begin
s:=copy(s,x+1,999);
x:=pos('>',s);
if x>0 then
begin
s:=copy(s,1,x-1);
s:=CleanString(s);
// s now is the filename of an included
if pos('\',s)=0 then