home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / PROG / PASCAL / MISCTI10.ZIP / TI232.ASC < prev    next >
Encoding:
Text File  |  1988-04-18  |  6.9 KB  |  228 lines

  1.  
  2. The  following example routines are public domain  programs  that 
  3. have  been uploaded to our Forum on CompuServe.  As a courtesy to 
  4. our  users  that  do not have  immediate  access  to  CompuServe, 
  5. Technical Support distributes these routines free of charge.
  6.  
  7. However,  because these routines are public domain programs,  not 
  8. developed by Borland International,  we are unable to provide any 
  9. technical support or assistance using these routines. If you need 
  10. assistance   using   these   routines,    or   are   experiencing 
  11. difficulties,  we  recommend  that you log  onto  CompuServe  and 
  12. request  assistance  from the Forum members that developed  these 
  13. routines.
  14.  
  15. Turbo  Pascal's Intr procedure cannot call MS-DOS interrupts  25H 
  16. and  26H  because they do not leave the CPU flags on  the  stack. 
  17. This set of functions allows you to call those interrupts,  which 
  18. are  Absolute  Disk Read and Absolute Disk Write,  by  using  the 
  19. following functions ReadSectors and WriteSectors, respectively. 
  20.  
  21. Both functions are called with the same parameters:
  22.          Buffer:  a buffer large enough to hold NumberSectors 512                   
  23. byte sectors.
  24.           Drive: the drive number.  0 is A:, 1 is B:, and so on.
  25.   NumberSectors: the number of sectors to transfer.
  26.   LogicalSector: the first logical sector number to transfer.
  27.  
  28. Be  sure  that  the  Buffer is large enough  to  hold  512  times 
  29. NumberSectors bytes of data.   Failure to do so will surely  lead 
  30. to bugs that are difficult to solve.
  31.  
  32. The  integer  return value is the error code returned by  MS-DOS. 
  33. "0" indicates a successful transfer, all other values are errors.  
  34. See a DOS Technical Reference Manual for translations.
  35.  
  36. A small sample program is provided.  It is a minimal disk editor 
  37. and is intended as an example only,  though it provides the basis 
  38. for a complete disk editor/utility.  The example is commented out 
  39. -- you must remove the line with the "(*" on it.
  40.  
  41. USE WITH CAUTION!
  42.  
  43. }
  44.  
  45. {$C-}
  46. {  Remove  the  leading  * for better keyboard  response  in  the 
  47.    example program }
  48.  
  49. Function __ReadWriteSectors(Var Buffer;
  50.                Drive, NumberSectors, LogicalSector: Integer;
  51.                WriteFlag: Byte): Integer;
  52. Var Result: Integer;
  53. Begin
  54.   Result:=0;
  55.   Inline($8A/$86/ Drive /      { MOV  AL,[BP+Drive]            }
  56.     $8B/$8E/ NumberSectors /   { MOV  CX,[BP+NumberSectors]    }
  57.     $8B/$96/ LogicalSector /   { MOV  DX,[BP+LogicalSector]    }
  58.     $55/                       { PUSH BP                       }
  59.     $1E/                       { PUSH DS                       }
  60.     $C5/$9E/ Buffer /          { LDS  BX,[BP+Buffer]           }
  61.     $80/$BE/ WriteFlag /$01/   { CMP  BYTE PTR [BP+WriteFlag],1 
  62. }
  63.     $74/$04/                   { JE   Write                     
  64. }
  65.     $CD/$25/                   { INT  25H                       
  66. }
  67.     $EB/$02/                   { JMP  WrapUp                    
  68. }
  69.     $CD/$26/                   {Write: INT 26H                  
  70. }
  71.     $5B/                       
  72.                         {WrapUp:POP  BX ;Dispose of flags }
  73.     $1F/                       {       POP  DS                 }
  74.     $5D/                       {       POP  BP                 }
  75.     $73/$04/                   {       JNB  Ok                 }
  76.     $89/$86/ Result            {       MOV  [BP+Result],AX     }
  77.     );                         {Ok:                            }
  78.   __ReadWriteSectors:=Result;
  79. End;
  80.  
  81. Function ReadSectors(Var Buffer; Drive, NumberSectors, 
  82.                      LogicalSector: Integer): Integer;
  83.   Begin
  84.     ReadSectors:=
  85.    __ReadWriteSectors(Buffer,Drive,NumberSectors,
  86.                       LogicalSector,0);
  87.   End;
  88.  
  89. Function WriteSectors(Var Buffer; Drive, NumberSectors, 
  90.                       LogicalSector: Integer): Integer;
  91.   Begin
  92.     WriteSectors:=
  93.       __ReadWriteSectors(Buffer,Drive,NumberSectors,
  94.                          LogicalSector,1);
  95.   End;
  96.  
  97. { Example program.  Delete next line to enable it: }
  98. (*
  99.  
  100. {$R+}
  101.  
  102. Type
  103.   Str=String[4];
  104.  
  105. Function Int2Hex2(B: Byte): Str;
  106.   Const H: Array [0..15] Of Char='0123456789ABCDEF';
  107.   Begin
  108.     Int2Hex2:=H[B Shr 4]+H[B And 15];
  109.   End;
  110.  
  111. Function Int2Hex4(I: Integer): Str;
  112.   Const H: Array [0..15] Of Char='0123456789ABCDEF';
  113.   Begin
  114.     Int2Hex4:=H[I Shr 12]+H[(I Shr 8) And 15]+
  115.               H[(I Shr 4) And 15]+H[I And 15];
  116.   End;
  117.  
  118. Function Hex2Int1(C: Char): Byte;
  119.   Begin
  120.     Case Upcase(C) Of
  121.       '0'..'9': Hex2Int1:=Ord(C) And 15;
  122.       'A'..'F': Hex2Int1:=Ord(C) And 15+9;
  123.       Else Hex2Int1:=0;
  124.      End;
  125.   End;
  126.  
  127. Function Hex2Int4(S: Str; Default: Integer): Integer;
  128.   Var I: Integer;
  129.   Begin
  130.     If S='' Then Hex2Int4:=Default
  131.     Else
  132.      Begin
  133.       I:=0;
  134.       While S<>'' Do
  135.        Begin
  136.         I:=I Shl 4 + Hex2Int1(S[1]);
  137.         Delete(S,1,1);
  138.        End;
  139.       Hex2Int4:=I;
  140.      End;
  141.   End;
  142.  
  143. Procedure ShowSector(Var S);
  144.   Var Buffer: Array [0..31,0..15] Of Byte Absolute S;
  145.       I,J: Integer;
  146.       B: Byte;
  147.   Begin
  148.     For I:=0 To 31 Do
  149.      Begin
  150.       Write(Int2Hex4(I Shl 4),' |  ');
  151.       For J:=0 To 15 Do
  152.        Begin
  153.         Write(Int2Hex2(Buffer[I,J]),' ');
  154.         If J And 3=3 Then Write(' ');
  155.        End;
  156.       Write('| ');
  157.       For J:=0 To 15 Do
  158.        Begin
  159.         B:=Buffer[I,J];
  160.         If B<127 Then NormVideo
  161.         Else LowVideo;
  162.         B:=B And $7F;
  163.         If B<32 Then B:=Ord('.');
  164.         Write(Chr(B));
  165.        End;
  166.       NormVideo;
  167.       WriteLn;
  168.      End;
  169.   End;
  170.  
  171.  
  172. Var
  173.   Buffer: Array [0..511] Of Byte;
  174.   DR,LS,Result,C: Integer;
  175.   W: Char;
  176.   V: Str;
  177.   Changed: Boolean;
  178.  
  179. Begin
  180.   FillChar(Buffer,SizeOf(Buffer),'z');
  181.   Repeat
  182.     DR:=-1;
  183.     Write('Enter the drive # to read (CR to end): ');
  184.     ReadLn(DR);
  185.     If DR In [0..25] Then
  186.      Begin
  187.       Write('Enter the logical sector # to read: ');
  188.       ReadLn(LS);
  189.       Result:=ReadSectors(Buffer,DR,1,LS);
  190.       WriteLn('Result code = ',Result);
  191.       Changed:=False;
  192.       Repeat
  193.         ShowSector(Buffer);
  194.         Write('Enter the number of the byte to change ');
  195.         Write(' (0-1FF, CR to end): ');
  196.         ReadLn(V);
  197.         C:=Hex2Int4(V,-1);
  198.         If (C>0) And (C<512) Then
  199.          Begin
  200.           Write('Enter new value [',Int2Hex2(Buffer[C]),']: ');
  201.           ReadLn(V);
  202.           If Length(V)>1 Then
  203.             If V[1] In ['''','"'] Then Buffer[C]:=Ord(V[2])
  204.             Else Buffer[C]:=Hex2Int4(V,Buffer[C])
  205.           Else Buffer[C]:=Hex2Int4(V,Buffer[C]);
  206.           Changed:=True;
  207.          End
  208.         Else C:=-1;
  209.       Until C=-1;
  210.  
  211.       If Changed Then
  212.        Begin
  213.         Write('Write changes back to drive ',DR,',');
  214.         Write ('logical sector #',LS,' (Y/N)? ');
  215.         ReadLn(W);
  216.         If Upcase(W)='Y' Then
  217.          Begin
  218.           Result:=WriteSectors(Buffer,DR,1,LS);
  219.           WriteLn('Result code = ',Result);
  220.          End;
  221.        End;
  222.      End
  223.     Else DR:=-1;
  224.   Until DR=-1;
  225. End.
  226. (**)
  227.  
  228.