View Issue Details

IDProjectCategoryView StatusLast Update
0038593FPCCompilerpublic2021-03-06 15:27
ReporterMatthias Koch Assigned ToFlorian  
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionno change required 
PlatformCross 16 Bit DOS on AMD64 LinuxOSDOS 
Product Version3.3.1 
Summary0038593: Not handling dword arithmetic correctly in ppcross8086
DescriptionThe upper 16 bits of a dword variable are not calculated properly and contain zero using this function:

var memory : array[0..7] of byte;

function read32(addr : dword) : dword;
begin
  read32 := memory[addr] or
            memory[addr + 1] shl 8 or
            memory[addr + 2] shl 16 or
            memory[addr + 3] shl 24;
end;
Steps To ReproduceUsing Free Pascal Compiler version 3.0.4+dfsg-22 [2019/01/24] for x86_64
on Debian 10 gives correct result:
fpc error && ./error

12345678
43424140
44434241
45444342
46454443
47464544


When using compiler from ftp://ftp.freepascal.org/pub/fpc/snapshot/trunk/i8086-msdos/fpc-3.3.1.msdos.built.on.x86_64-linux-tiny.tar.gz
Free Pascal Compiler version 3.3.1-r20:48878 [2021/03/05] for i8086

ppcross8086 -XX -Os -CX -WmTiny -Wtcom error.pas

Running error.com in Dosbox gives:

12345678
00004140
00004241
00004342
00004443
00004544
TagsNo tags attached.
Fixed in Revision
FPCOldBugId
FPCTarget-
Attached Files

Activities

Matthias Koch

2021-03-06 02:45

reporter  

error.pas (1,959 bytes)   
// ----------------------------------------------------------------------------
//  Nice hex number printing helpers
// ----------------------------------------------------------------------------

function byte2hex(number : byte) : string;
const
    hexa : array [0..15] of char = '0123456789ABCDEF';
begin
  byte2hex := hexa[number shr 4] + hexa[number and 15];
end;

function word2hex(number : word) : string;
begin
  word2hex := Byte2Hex((number and $FF00) shr 8) + Byte2Hex(number and $00FF);
end;

function dword2hex(number : dword) : string;
begin
  dword2hex := Word2Hex((number and $FFFF0000) shr 16) + Word2Hex(number and $0000FFFF);
end;

// ----------------------------------------------------------------------------
//  Try to reprocuce the bug
// ----------------------------------------------------------------------------

var memory : array[0..7] of byte;


function read32(addr : dword) : dword;
begin
  read32 := memory[addr] or
            memory[addr + 1] shl  8  or
            memory[addr + 2] shl 16  or
            memory[addr + 3] shl 24;
end;

begin
  memory[0] := $40;
  memory[1] := $41;
  memory[2] := $42;
  memory[3] := $43;
  memory[4] := $44;
  memory[5] := $45;
  memory[6] := $46;
  memory[7] := $47;


  writeln(dword2hex($12345678));

  writeln(dword2hex(read32(0)));
  writeln(dword2hex(read32(1)));
  writeln(dword2hex(read32(2)));
  writeln(dword2hex(read32(3)));
  writeln(dword2hex(read32(4)));
end.

// Using Free Pascal Compiler version 3.0.4+dfsg-22 [2019/01/24] for x86_64
// on Debian 10 gives correct result:
// fpc error && ./error

// 12345678
// 43424140
// 44434241
// 45444342
// 46454443
// 47464544


// Now using compiler from ftp://ftp.freepascal.org/pub/fpc/snapshot/trunk/i8086-msdos/fpc-3.3.1.msdos.built.on.x86_64-linux-tiny.tar.gz
// ppcross8086 -XX -Os -CX -WmTiny -Wtcom error.pas
// Running error.com in Dosbox gives:

// 12345678
// 00004140
// 00004241
// 00004342
// 00004443
// 00004544
error.pas (1,959 bytes)   

Florian

2021-03-06 10:12

administrator   ~0129422

This is normal in Pascal: by default integer operations are carried out with the size of the natural integer size (16 Bit for 8086). As the result type of an expression depends only on its operands, the later assignment to dword does not matter. To get the desired behaviour, cast the first operand to dword.

Matthias Koch

2021-03-06 15:27

reporter   ~0129433

Thank you for the hint, my issue is solved now. I am sorry for the confusion. Many thanks for a great compiler !

Issue History

Date Modified Username Field Change
2021-03-06 02:45 Matthias Koch New Issue
2021-03-06 02:45 Matthias Koch File Added: error.pas
2021-03-06 10:12 Florian Assigned To => Florian
2021-03-06 10:12 Florian Status new => resolved
2021-03-06 10:12 Florian Resolution open => no change required
2021-03-06 10:12 Florian FPCTarget => -
2021-03-06 10:12 Florian Note Added: 0129422
2021-03-06 15:27 Matthias Koch Note Added: 0129433