source: branches/bigint/Disassembler.pas

Last change on this file was 8, checked in by chronos, 3 months ago
  • Added: Common package.
  • Added: Memory form to show content of memory.
File size: 2.4 KB
Line 
1unit Disassembler;
2
3interface
4
5uses
6 Classes, SysUtils, Cpu, Instructions, StrUtils, IntMemory, Int;
7
8type
9
10 { TDisassembler }
11
12 TDisassembler = class
13 InstructionSet: TInstructionSet;
14 Memory: TIntMemory;
15 procedure Disassemble(Lines: TStrings);
16 procedure SaveToFile(FileName: string);
17 constructor Create;
18 destructor Destroy; override;
19 end;
20
21
22implementation
23
24procedure TDisassembler.Disassemble(Lines: TStrings);
25var
26 I: Integer;
27 Value: TInt;
28 Instruction: TInstruction;
29 InstructionInfo: TInstructionInfo;
30 Line: string;
31 InstText: string;
32 InstBytes: string;
33begin
34 Memory.Position := 0;
35 while Memory.Position < Memory.Size do begin
36 Line := IntToHex(Memory.Position, 8) + ' ';
37 Value := Memory.ReadPos;
38 InstBytes := IntToHex(Value, 2) + ' ';
39 InstText := '';
40 if (Value >= 0) and (Value <= Integer(High(TInstruction))) then begin
41 Instruction := TInstruction(Value);
42 InstructionInfo := InstructionSet.SearchInstruction(Instruction);
43 if Assigned(InstructionInfo) then begin
44 InstText := InstructionInfo.Name;
45 for I := 0 to Length(InstructionInfo.Params) - 1 do begin
46 Value := Memory.ReadPos;
47 InstBytes := InstBytes + IntToHex(Value, 2) + ' ';
48 if I > 0 then
49 InstText := InstText + ', ' else
50 InstText := InstText + ' ';
51 if InstructionInfo.Params[I] = ptNumber then begin
52 InstText := InstText + IntToHex(Value, 8);
53 end else
54 if InstructionInfo.Params[I] = ptIndirect then begin
55 InstText := InstText + '(' + IntToStr(Value) + ')';
56 end else
57 if InstructionInfo.Params[I] = ptIndirect2 then begin
58 InstText := InstText + '((' + IntToStr(Value) + '))';
59 end else raise Exception.Create('Unsupported instruction parameter type');
60 end;
61 InstBytes := InstBytes + DupeString(' ', 13 - Length(InstBytes));
62 end;
63 end;
64 Line := Line + InstBytes + InstText;
65 Lines.Add(Line);
66 end;
67end;
68
69procedure TDisassembler.SaveToFile(FileName: string);
70var
71 Lines: TStringList;
72begin
73 Lines := TStringList.Create;
74 Disassemble(Lines);
75 Lines.SaveToFile(FileName);
76 Lines.Free;
77end;
78
79constructor TDisassembler.Create;
80begin
81 InstructionSet := TInstructionSet.Create;
82end;
83
84destructor TDisassembler.Destroy;
85begin
86 FreeAndNil(InstructionSet);
87 inherited;
88end;
89
90end.
91
Note: See TracBrowser for help on using the repository browser.