home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / PASCAL / 30TURUTL / TURBOBUG.TXT < prev    next >
Text File  |  1985-02-16  |  6KB  |  142 lines

  1.                     ***** BUG REPORT *****
  2.          ***** MAJOR BUG IN TURBO PASCAL V2.00 *****
  3.                          July 1,1984
  4.  
  5.  
  6.      The runtime routines  do  not  handle  a  floating-point 
  7. (real)  subtraction  correctly.  For  some subtractions,  the 
  8. correct  difference  is  returned;  for  others,  a  zero  is 
  9. returned. The following program demonstrates the bug:
  10.  
  11. program test;
  12. begin
  13.      writeln(9.+(-6.0));        { Wrong value returned }
  14.      writeln(-1.0+(-1.0));      { Correct value returned }
  15.      writeln(1-2);              { Correct value returned }
  16.      writeln(1.-2:10:2);        { Wrong value returned }
  17.      writeln(1.-2.0);           { Wrong value returned }
  18.      writeln(1-2.0);            { Wrong value returned }
  19.      writeln(456 - 123.0);      { Correct value returned }
  20. end.
  21.  
  22. A value of zero is returned on those lines marked as 'wrong'. 
  23. The other lines return the correct difference.
  24.  
  25.  
  26. THE CAUSE
  27.  
  28.     After disassembling and tracing through a sample compiled 
  29. program, the error was located in the following code:
  30.  
  31. XXXX:12FA E84FFF        CALL    124C            ; Subtract
  32. XXXX:12FD 7306          JNB     1305
  33. XXXX:12FF 80F780        XOR     BH,80           ; Handle negative
  34. XXXX:1302 E863FF        CALL    1268            ;   numbers
  35. XXXX:1305 8B4504        MOV     AX,[DI+04]      ; Is mantissa zero?
  36. XXXX:1308 0B4502        OR      AX,[DI+02]
  37. XXXX:130B 0A4501        OR      AL,[DI+01]      ; ***** ERROR *****
  38. XXXX:130E 740D          JZ      131D            ; Yes
  39. XXXX:1310 F6450580      TEST    BYTE PTR [DI+05],80  ; Normalize
  40. XXXX:1314 750C          JNZ     1322
  41.  
  42. (Comments  have  been  added for clarity).  This disassembled 
  43. code is located in the  routines  that  handle  addition  and 
  44. subtraction  (only the subtraction part is shown).  The error 
  45. occurs when the routine tests to see if  the  result  of  the 
  46. subtraction is zero. It tests for a zero by "OR-ing" together 
  47. the five bytes of the mantissa (the instructions that do this 
  48. are at offsets 1305H to 130BH). If the mantissa is zero, then 
  49. the result of this "multiple-or" will set the zero flag.  The 
  50. first  of  these instructions is a move of the word at [DI+4] 
  51. to AX.  The next instruction takes the logical OR of  AX  and 
  52. [DI+2].  No problem so far.  However, the last instruction is 
  53. "OR AL,[DI+1]",  an instruction that operates on a  byte  and 
  54. not  on  a word.  If the OR of the words at [DI+4] and [DI+2] 
  55. results in a word whose UPPER byte is NONZERO but whose LOWER 
  56. byte is ZERO,  then the next instruction, the "byte-wise" OR, 
  57. will SET the zero flag if the byte at [DI+1] is zero.  Notice 
  58. that the instruction at 130BH totally ignores the contents of 
  59. the upper byte of AX;  the flags are  set  according  to  the 
  60. lower byte of AX only.  This is what causes the error.
  61.  
  62.  
  63. THE FIX
  64.  
  65.      The fix to this bug is simple: simply exchange the order 
  66. of the two "OR" instructions.  This way, the zero flag is set 
  67. according  to  a  full  16-bit  OR  and  not  an  8-bit  one. 
  68. IMPORTANT:  Only persons familiar with the operation of DEBUG 
  69. should  attempt  the  following.  Using DEBUG,  the following 
  70. sequence of commands can be used to fix the bug:
  71.  
  72.      Assumptions and notes in the following:
  73.           1) ONLY WORK ON A COPY OF TURBO!  DO  NOT  USE 
  74.              YOUR MASTER COPY!
  75.           2) The sequence of commands shown below writes 
  76.              out  the   fixed   version   to   the  file 
  77.              'turbo.com'
  78.           3) Make sure that you have version 2.00
  79.           4) 'turbo.com'   must   be   in   the  current 
  80.              directory on drive B.
  81.           5) DOS  2.00  or  above  is  being  used  (the 
  82.              debugger in DOS 1.00 and 1.10 does not have 
  83.              the 'assemble' command).                           
  84.           6) Be  sure  to  verify  that  the code in the 
  85.              compiler is the same as  that  shown  below 
  86.              initially.  After the changes are made,  be 
  87.              sure to verify that the changes  have  been 
  88.              made  correctly  before  writing  the fixed 
  89.              version out to disk.
  90.  
  91.  
  92. B>debug turbo.com
  93. -u 12fa             <- Verify the following locations
  94. XXXX:12FA E84FFF        CALL    124C
  95. XXXX:12FD 7306          JNB     1305
  96. XXXX:12FF 80F780        XOR     BH,80
  97. XXXX:1302 E863FF        CALL    1268
  98. XXXX:1305 8B4504        MOV     AX,[DI+04]
  99. XXXX:1308 0B4502        OR      AX,[DI+02]
  100. XXXX:130B 0A4501        OR      AL,[DI+01]
  101. XXXX:130E 740D          JZ      131D
  102. XXXX:1310 F6450580      TEST    BYTE PTR [DI+05],80
  103. XXXX:1314 750C          JNZ     1322
  104. XXXX:1316 E8F9FE        CALL    1212
  105. XXXX:1319 FE0D          DEC     BYTE PTR [DI]
  106. -a 1308             <- Make changes
  107. XXXX:1308 or al,[di+1]
  108. XXXX:130E or ax,[di+2]
  109. XXXX:1311                       <- Press 'enter'
  110. -u 12fa             <- Verify that the changes are correct
  111. XXXX:12FA E84FFF        CALL    124C
  112. XXXX:12FD 7306          JNB     1305
  113. XXXX:12FF 80F780        XOR     BH,80
  114. XXXX:1302 E863FF        CALL    1268
  115. XXXX:1305 8B4504        MOV     AX,[DI+04]
  116. XXXX:1308 0A4501        OR      AL,[DI+01]
  117. XXXX:130B 0B4502        OR      AX,[DI+02]
  118. XXXX:130E 740D          JZ      131D
  119. XXXX:1310 F6450580      TEST    BYTE PTR [DI+05],80
  120. XXXX:1314 750C          JNZ     1322
  121. XXXX:1316 E8F9FE        CALL    1212
  122. XXXX:1319 FE0D          DEC     BYTE PTR [DI]
  123. -w                  <- Save fixed version of turbo
  124. Writing 8E80 bytes
  125. -q
  126.  
  127. B>
  128.  
  129. <end of fix>
  130.  
  131.  
  132. OTHER NOTES ABOUT TURBO
  133.  
  134.      One  cannot  set  breakpoints  in TURBO using DEBUG.  It 
  135. seems that TURBO  uses  the  breakpoint  interrupt  for  some 
  136. hideous  reason.  If  one  attempts  to set breakpoints,  the 
  137. system will probably crash.  Tracing,  however,  does seem to 
  138. work.  The  above  bug  was  tracked down only after hours of 
  139. tracing (by maching and by hand) and disassembling.
  140.  
  141.