[comp.lang.pascal] Re Turbo 3 Read Screen Function

kirsch@braggvax.arpa (David Kirschbaum) (05/01/88)

Subject: Turbo 3, read screen function

Russell asks:
>Is there a procedure/function in Turbo Pascal 3.0 to read a character
>displayed at a specific location on the screen?

I don't believe Turbo has such a function, but it's relatively easy to
do with BIOS calls.  Here's a little demo: (released to Public Domain:
go ahead and make your fortunes with it...)

PROGRAM ToadChar;

(* Simple demo of a function to return a screen character.
   For PC-DOS Turbo 3.0. (Donno what else it'll work with!)

   David Kirschbaum
   Toad Hall
   kirsch@braggvax.ARPA
*)

CONST
  STR_LEN = 21;
  S : STRING[STR_LEN] = 'Ain''t snarfing great?';

VAR
  i : INTEGER;
  Ch : CHAR;


FUNCTION Get_Char(col,row : INTEGER) : CHAR;
  {Return the screen character at col,row (X,Y) }
  BEGIN
    Inline(
  $B4/$15        {  mov  ah,$15       ;Svc 15H, get screen mode}
  /$CD/$10       {  int  $10          ;BH = current display page}
  /$B4/$03       {  mov  ah,3         ;get current cursor position}
  /$CD/$10       {  int  $10}
                 {;CX = cursor size, but we don't need that}
                 {; (don't wanna lose it either!)}
                 {;DH = row, DL = col}
  /$89/$D7       {  mov  di,dx        ;save old cursor psn here a sec}
  /$8A/$B6/>ROW  {  mov  dh,>row[bp]  ;user's specified row (assume integer)}
  /$8A/$96/>COL  {  mov  dl,>col[bp]  ;user's specified col (assume integer)}
  /$FE/$CE       {  dec  dh           ;adjust from 0..24 to 1..25 for humans}
  /$FE/$CA       {  dec  dl           ;and from 0..79 to 1..80}
  /$B4/$02       {  mov  ah,2         ;Svc 2, set cursor position}
  /$CD/$10       {  int  $10}
  /$B4/$08       {  mov  ah,8         ;Svc 8, read char and attrib}
  /$CD/$10       {  int  $10}
                 {;Turbo's stack looks like this:}
                 {;[bp+0] : return address (a word)}
                 {;[bp+4] : the row parameter (a word)}
                 {;[bp+6] : the col parameter (a word)}
                 {;[bp+8] ; where Turbo plans to find the CHAR we'll return}
  /$88/$46/$08   {  mov  [bp+8],al    ;put the char where Turbo'll return it}
  /$89/$FA       {  mov  dx,di        ;restore the original cursor position}
  /$B4/$02       {  mov  ah,2         ;Svc 2, set cursor position}
  /$CD/$10       {  int  $10          ;put the cursor back, size unchanged}
);
  END;  {of Get_Char}


BEGIN  {main demo}
  ClrScr;
  GotoXY(30,2);                       {a likely place}
  Writeln(S);                         {an unlikely string}
  GotoXY(29,4);                       {go somewhere else}

(* A Turbo idiosyncrasy .. you gotta do a write after a GotoXY,
   or you ain't actually there!
*)
  Write('[');                         {just to make it neat}

  FOR i := PRED(30+STR_LEN) DOWNTO 30 DO
    Write( Get_Char(i,2) );           {snarf, display backwards}
  Writeln(']');                       {just to make it neat}

END.