Ignore:
Timestamp:
May 1, 2019, 9:48:46 PM (6 years ago)
Author:
chronos
Message:
  • Added: Assembler labels reference address calculation.
  • Fixed: Displaying address/data hex numbers in opcode and instruction.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/virtualcpu4/UInstructionWriter.pas

    r184 r185  
    2525    Prefix: Boolean;
    2626    procedure Init;
     27    function GetRelativeAddr(BitWidth: TBitWidth; BaseIP, TargetAddr: QWord): Int64;
    2728    procedure Write8(Value: Byte);
    2829    procedure Write16(Value: Word);
     
    132133end;
    133134
     135function TInstructionWriter.GetRelativeAddr(BitWidth: TBitWidth; BaseIP, TargetAddr: QWord): Int64;
     136begin
     137  Result := Int64(TargetAddr) - (BaseIP + 1 + BitWidthBytes[BitWidth]);
     138end;
     139
    134140procedure TInstructionWriter.JumpRel(Addr: QWord);
    135141var
    136   NextIP: QWord;
    137 begin
    138   PrefixBegin;
    139   NextIP := IP + 1 + BitWidthBytes[Cpu.AddrSize];
     142  RelAddr: Int64;
     143begin
     144  PrefixBegin;
     145  RelAddr := GetRelativeAddr(AddrSize, IP, Addr);
    140146  Write8(Byte(opJumpRel));
    141   WriteAddressSigned(Int64(Addr) - Int64(NextIP));
     147  WriteAddressSigned(RelAddr);
    142148  PrefixEnd;
    143149end;
     
    145151procedure TInstructionWriter.JumpRelNotZero(Addr: QWord);
    146152var
    147   NextIP: QWord;
    148 begin
    149   PrefixBegin;
    150   NextIP := IP + 1 + BitWidthBytes[Cpu.AddrSize];
     153  RelAddr: Int64;
     154begin
     155  PrefixBegin;
     156  RelAddr := GetRelativeAddr(AddrSize, IP, Addr);
    151157  Write8(Byte(opJumpRelNotZero));
    152   WriteAddressSigned(Int64(Addr) - Int64(NextIP));
     158  WriteAddressSigned(RelAddr);
    153159  PrefixEnd;
    154160end;
     
    156162procedure TInstructionWriter.JumpRelZero(Addr: QWord);
    157163var
    158   NextIP: QWord;
    159 begin
    160   PrefixBegin;
    161   NextIP := IP + 1 + BitWidthBytes[Cpu.AddrSize];
     164  RelAddr: Int64;
     165begin
     166  PrefixBegin;
     167  RelAddr := GetRelativeAddr(AddrSize, IP, Addr);
    162168  Write8(Byte(opJumpRelZero));
    163   WriteAddressSigned(Int64(Addr) - Int64(NextIP));
     169  WriteAddressSigned(RelAddr);
    164170  PrefixEnd;
    165171end;
     
    360366begin
    361367  case AddrSize of
    362     bw8: Write8(Value);
    363     bw16: Write16(Value);
    364     bw32: Write32(Value);
     368    bw8: Write8(Value and $ff);
     369    bw16: Write16(Value and $ffff);
     370    bw32: Write32(Value and $ffffffff);
    365371    bw64: Write64(Value);
    366372  end;
     
    370376begin
    371377  case AddrSize of
    372     bw8: Write8(Byte(Value));
    373     bw16: Write16(Word(Value));
    374     bw32: Write32(DWord(Value));
     378    bw8: Write8(Byte(Value and $ff));
     379    bw16: Write16(Word(Value and $ffff));
     380    bw32: Write32(DWord(Value and $ffffffff));
    375381    bw64: Write64(QWord(Value));
    376382  end;
     
    380386begin
    381387  case DataSize of
    382     bw8: Write8(Value);
    383     bw16: Write16(Value);
    384     bw32: Write32(Value);
     388    bw8: Write8(Value and $ff);
     389    bw16: Write16(Value and $ffff);
     390    bw32: Write32(Value and $ffffffff);
    385391    bw64: Write64(Value);
    386392  end;
Note: See TracChangeset for help on using the changeset viewer.