Disassembly of old Turbo Pascal (V3) code - how to create data segment in IDA

401 Views Asked by At

I would like to disassemble the final version of a self-written Turbo Pascal V3 program, i.e. a simple .COM file, and to that effect I've dug out my old (AD 2004) registered copy of IDA Pro (V4.7.0.831). Not having used it for more than 10 years, and no longer having access to their forum, I'm now stuck. The .COM file loads, IDA happily disassembles it, but it just creates one single segment, and I have no (longer) a clue on how to create the data segment. There's a bit of info in the TP3 Manual, and using David Lindauer's GRDB in DOXBox-X allows me to single-step through the RTL initialisation code and that shows me it sets up up DS and SS, but it doesn't help me in setting up these segments in IDA.

I've tried the "Create Segment" option, but I'm lost entering the required values for start address, end address and base, "class" is probably "DATA", the once for the single "seg000" that IDA creates are CODE, start @ 0x0100, end @ 0xD623, which leads me to assume that a to-be-created "seg001" should start at 0x0000, end at 0xffff, and have a base of 0xd63 (paragraphs), but that results in a "Bad segment base: segment would have bytes with a negative offset" pop-up.

Trying start @ 0xd630, end @ 0x1d630, with a base 0x0000 creates a segment, but it looks like

seg000:D622
seg001:C8C00 ; ---------------------------------------------------------------------------
seg001:C8C00
seg001:C8C00 ; Segment type: Regular
seg001:C8C00 seg001          segment byte public '' use16
seg001:C8C00                 assume cs:seg001
seg001:C8C00                 ;org 0C8C00h 
seg001:C8C00                 assume es:nothing, ss:nothing, ds:nothing, fs:nothing, gs:nothing

Which may be correct, but the "org 0c8c00" makes absolutely no sense to me.

If you can help me I would be grateful, and to help you, I've uploaded a RAR archive with the full sources, the resulting "lift.com" executable and the input file to my Google drive @ https://drive.google.com/drive/folders/0B0oygbfs7DsVVWNBZWpqaHRHX3c?usp=sharing, look for lift16bit.rar Please note that the code will not compile with anything more advanced than Turbo Pascal 3, and in my case it was compiled with TP 3.01a.

1

There are 1 best solutions below

0
On

The following IDA snippet of IDC code will set up the segment registers for programs compiled with Turbo Pascal V3.01a:

//-------------------------------------------------------------------
// This code sets-up the Turbo Pascal segment registers
//-------------------------------------------------------------------
  auto _rds;
  auto _lds;

  _rds = word(word(0x101) + 0x103 +  9);
  _lds = word(word(0x101) + 0x103 + 11);

  add_segm_ex(0X100, _rds * 16, 0, 0, 1, 2, ADDSEG_NOSREG);
  SegRename(0X100, "cseg");
  SegClass (0X100, "CODE");

  SegDefReg(0x100, "ds", _rds);
  SegDefReg(0x100, "es", 0xFFFF);
  SegDefReg(0x100, "ss", 0xFFFF);
  SegDefReg(0x100, "fs", 0xFFFF);
  SegDefReg(0x100, "gs", 0xFFFF);

  set_segm_type(0X100, 2);

  add_segm_ex(_rds * 16, (_rds + _lds) * 16, _rds, 0, 3, 2, ADDSEG_NOSREG);
  SegRename(_rds * 16, "dseg");
  SegClass (_rds * 16, "DATA");

  SegDefReg(_rds * 16, "ds", _rds);
  SegDefReg(_rds * 16, "es", 0xFFFF);
  SegDefReg(_rds * 16, "ss", 0xFFFF);
  SegDefReg(_rds * 16, "fs", 0xFFFF);
  SegDefReg(_rds * 16, "gs", 0xFFFF);

  set_segm_type(_rds * 16, 3);

  set_inf_attr(INF_LOW_OFF, 0xffff);
  set_inf_attr(INF_HIGH_OFF, 0xffff);

It will (quite likely) work for other versions of TP3, maybe and even for TP1/2, but no guarantees!

The IDC code relies on the disassembled TP3 code from http://www.pcengines.ch/tp3.htm and single-stepping through it using a debugger, I use David Lindauer's GRDB @ https://ladsoft.tripod.com/grdb_debugger.html