| 1 | unit Disassembler;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Classes, SysUtils, Cpu, Instructions, StrUtils, Memory;
|
|---|
| 7 |
|
|---|
| 8 | type
|
|---|
| 9 |
|
|---|
| 10 | { TDisassembler }
|
|---|
| 11 |
|
|---|
| 12 | TDisassembler = class
|
|---|
| 13 | InstructionSet: TInstructionSet;
|
|---|
| 14 | Memory: TMemory;
|
|---|
| 15 | procedure Disassemble(Lines: TStrings);
|
|---|
| 16 | procedure SaveToFile(FileName: string);
|
|---|
| 17 | constructor Create;
|
|---|
| 18 | destructor Destroy; override;
|
|---|
| 19 | end;
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 | implementation
|
|---|
| 23 |
|
|---|
| 24 | procedure TDisassembler.Disassemble(Lines: TStrings);
|
|---|
| 25 | var
|
|---|
| 26 | I: Integer;
|
|---|
| 27 | J: Integer;
|
|---|
| 28 | Value: TInteger;
|
|---|
| 29 | Instruction: TInstruction;
|
|---|
| 30 | InstructionInfo: TInstructionInfo;
|
|---|
| 31 | Line: string;
|
|---|
| 32 | InstText: string;
|
|---|
| 33 | InstBytes: string;
|
|---|
| 34 | begin
|
|---|
| 35 | Memory.Position := 0;
|
|---|
| 36 | while Memory.Position < Memory.Size do begin
|
|---|
| 37 | Line := IntToHex(Memory.Position, 8) + ' ';
|
|---|
| 38 | Value := Memory.Read;
|
|---|
| 39 | InstBytes := IntToHex(Value, 2) + ' ';
|
|---|
| 40 | InstText := '';
|
|---|
| 41 | if (Value >= 0) and (Value <= Integer(High(TInstruction))) then begin
|
|---|
| 42 | Instruction := TInstruction(Value);
|
|---|
| 43 | InstructionInfo := InstructionSet.SearchInstruction(Instruction);
|
|---|
| 44 | if Assigned(InstructionInfo) then begin
|
|---|
| 45 | InstText := InstructionInfo.Name;
|
|---|
| 46 | for J := 0 to Length(InstructionInfo.Params) - 1 do begin
|
|---|
| 47 | Value := Memory.Read;
|
|---|
| 48 | InstBytes := InstBytes + IntToHex(Value, 2) + ' ';
|
|---|
| 49 | if J > 0 then
|
|---|
| 50 | InstText := InstText + ', ' else
|
|---|
| 51 | InstText := InstText + ' ';
|
|---|
| 52 | if InstructionInfo.Params[J] = ptNumber then begin
|
|---|
| 53 | InstText := InstText + IntToHex(Value, 8);
|
|---|
| 54 | end else
|
|---|
| 55 | if InstructionInfo.Params[J] = ptReg then begin
|
|---|
| 56 | InstText := InstText + 'R' + IntToStr(Value);
|
|---|
| 57 | end else
|
|---|
| 58 | if InstructionInfo.Params[J] = ptRegIndirect then begin
|
|---|
| 59 | InstText := InstText + '(R' + IntToStr(Value) + ')';
|
|---|
| 60 | end else
|
|---|
| 61 | if InstructionInfo.Params[J] = ptRegIndirectIndex then begin
|
|---|
| 62 | InstText := InstText + '(R' + IntToStr(Value);
|
|---|
| 63 | Value := Memory.Read;
|
|---|
| 64 | InstBytes := InstBytes + IntToHex(Value, 2) + ' ';
|
|---|
| 65 | InstText := InstText + ' + ' + IntToStr(Value) + ')';
|
|---|
| 66 | end else
|
|---|
| 67 | if InstructionInfo.Params[J] = ptRegIndirectGroup then begin
|
|---|
| 68 | InstText := InstText + '(R' + IntToStr(Value);
|
|---|
| 69 | Value := Memory.Read;
|
|---|
| 70 | InstBytes := InstBytes + IntToHex(Value, 2) + ' ';
|
|---|
| 71 | InstText := InstText + ': R' + IntToStr(Value) + ')';
|
|---|
| 72 | end;
|
|---|
| 73 | end;
|
|---|
| 74 | InstBytes := InstBytes + DupeString(' ', 13 - Length(InstBytes));
|
|---|
| 75 | end;
|
|---|
| 76 | end;
|
|---|
| 77 | Line := Line + InstBytes + InstText;
|
|---|
| 78 | Lines.Add(Line);
|
|---|
| 79 | end;
|
|---|
| 80 | end;
|
|---|
| 81 |
|
|---|
| 82 | procedure TDisassembler.SaveToFile(FileName: string);
|
|---|
| 83 | var
|
|---|
| 84 | Lines: TStringList;
|
|---|
| 85 | begin
|
|---|
| 86 | Lines := TStringList.Create;
|
|---|
| 87 | Disassemble(Lines);
|
|---|
| 88 | Lines.SaveToFile(FileName);
|
|---|
| 89 | Lines.Free;
|
|---|
| 90 | end;
|
|---|
| 91 |
|
|---|
| 92 | constructor TDisassembler.Create;
|
|---|
| 93 | begin
|
|---|
| 94 | InstructionSet := TInstructionSet.Create;
|
|---|
| 95 | end;
|
|---|
| 96 |
|
|---|
| 97 | destructor TDisassembler.Destroy;
|
|---|
| 98 | begin
|
|---|
| 99 | FreeAndNil(InstructionSet);
|
|---|
| 100 | inherited;
|
|---|
| 101 | end;
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 | end.
|
|---|
| 105 |
|
|---|