Board index » delphi » Some pascal questions, please help.

Some pascal questions, please help.

I got a couple of question here:
1) How do you empty the keypress buffer? I read the "Keypressed" thread
above but it didn't seem to help.
2) Is it possible to read something that is already on the screen? e.g.
to read what character is at certain x,y?
3) Is it consider bad to use inc() (just like goto, halt etc) instead of
x:=x+1?
Thank you.
 

Re:Some pascal questions, please help.


Quote
x-factorial <x!@x!.com> wrote:
>I got a couple of question here:
>1) How do you empty the keypress buffer? I read the "Keypressed" thread
>above but it didn't seem to help.

There is a direct way by calling an interrupt, but I don't know
what it is.  The following may do it (untested)

vac ch : char;

while keypressed do ch := readkey;

Quote
>2) Is it possible to read something that is already on the screen? e.g.
>to read what character is at certain x,y?

In DOS you can.  Again, I don't have the details, but there are
two bytes in memory corresponding to each position on the text
screen - one for color, the other for the character.

Quote
>3) Is it consider bad to use inc() (just like goto, halt etc) instead of
>x:=x+1?

I don't think so.

Jud McCranie

Re:Some pascal questions, please help.


Quote
Sacha Roscoe <sa...@caesia.maths.uwa.edu.au> wrote:
>I don't think this is particularly encouraged; if you've put something on
>the screen you should be able to keep track of what and where, and if the
>user has done it you should be able to trap it when it's entered. But if you
>need to use the video buffer, it's there...

Sometimes you need to read what is on the screen.  In ProComm
for DOS (probably written in TP7, since it has the CRT bug) when
you start to upload a program, the BBS writes the name of the
file to be uploaded to the screen, ProComm reads that and starts
the upload of that file for you.  The Windows version can't do
that!  (You have to enter it yourself).

Jud McCranie

Re:Some pascal questions, please help.


Quote
Jud McCranie <jud.mccra...@mindspring.com> wrote:
> x-factorial <x!@x!.com> wrote:
>>2) Is it possible to read something that is already on the screen? e.g.
>>to read what character is at certain x,y?
> In DOS you can.  Again, I don't have the details, but there are
> two bytes in memory corresponding to each position on the text
> screen - one for color, the other for the character.

IIRC, there are SegNNNN variables, one of which should contain the starting
address for the video buffer (it depends on which video mode you're in;
you'll need to test this). From there you should be able to calculate the
correct offset.

I don't think this is particularly encouraged; if you've put something on
the screen you should be able to keep track of what and where, and if the
user has done it you should be able to trap it when it's entered. But if you
need to use the video buffer, it's there...

Quote
>>3) Is it consider bad to use inc() (just like goto, halt etc) instead of
>>x:=x+1?
> I don't think so.

In the manuals it's encouraged, provided you keep a tight rein on it.
Specifically, it is faster in execution, but *does not* report overflow
errors. If a byte variable is on 255 and you Inc() it, it will simply become
0. Similar cautions apply to Dec(). Also remember the Inc(x, n) and
Dec(x, n) syntax - it can be quite useful.

--
______________________________________________________________________
     The Scarlet Manuka,      |        Nitpickers' Party motto:
  Pratchett Quoter At Large,  |  "He who guards his lips guards his
 First Prophet of Bonni, is:  |  soul, but he who speaks rashly will
   sa...@maths.uwa.edu.au     |    come to ruin." -- Proverbs 13:3
______________________________|_______________________________________
*All opinions in this article are fictional.  Any resemblance to real
opinions, whether living or dead, is purely coincidental.*

Re:Some pascal questions, please help.


Quote
>1) How do you empty the keypress buffer? I read the "Keypressed" thread
>above but it didn't seem to help.

To empty the beybuffer use something like:

    procedure ClearBuffer;
    var c: Char;
    begin
      while (keypressed) do
        c := ReadKey;
    end;

Quote
>2) Is it possible to read something that is already on the screen? e.g.
>to read what character is at certain x,y?

The answer is related only to DOS applications. There are several methods
to do such things: interrupt $10, function $8  such things (gives one sign
at a specific position). It might be more flexible to build a structure
representing the organisation of the screen and to adress it to the screen
memory:

    Unit Screen;

    Interface

    Procedure ReadScreen(x, y, len : Byte; Wrap : Boolean; var s: String);

    Implementation

    Type
      Pixel = Record
                sign : Char;
                attr : Byte;
      end;
      ScreenMem = Array[1..25, 1..80] of Pixel;

    Var
      VideoBuffer : ^ScreenMem;

    Procedure ReadScreen(x, y, len : Byte; Wrap : Boolean; var s: String);
    begin
      s := '';
      while (len > 0) do
      begin
        s := s + VideoBuffer^[y, x].sign;
        if (x = 80) and (not Wrap) then
          break;
        if (x = 80) then
        begin
          x := 1;
          inc(y);
        end
        else
          inc(x);
        dec(len);
      end;
    end;

    begin
      if (mem[$40:$49] = 7) then
        VideoBuffer := Ptr($B000, $0000)
      else
        VideoBuffer := Ptr($B800, $0000);
    end.

This small Unit allows you to read Strings from screen. But only if you are
in standard video mode (80 x 25). In similiar way you can write to screen.

Quote
>3) Is it consider bad to use inc() (just like goto, halt etc) instead of
>x:=x+1?

I prefer the inc() and dec() procedures. I think the time thay were
implemented I've read that they are much faster because they use one CPU
command. I don't know ho this is in newer compilers.

--
Merry Christmas
Georg Pohl

~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
1st Email:   g...@rp-plus.de
2nd Email:   pohl....@klosterfrau.de
Website:     http://www.online-club.de/~Eulenspiegel
             Download: freeware for DOS, C-libraries

Re:Some pascal questions, please help.


In article <83mqok$mc...@enyo.uwa.edu.au>,
Sacha Roscoe  <sa...@caesia.maths.uwa.edu.au> wrote:

Quote
>In the manuals it's encouraged, provided you keep a tight rein on it.
>Specifically, it is faster in execution, but *does not* report overflow
>errors.

Neither does other integer arithmetic.

Quote
> If a byte variable is on 255 and you Inc() it, it will simply become
>0.

So will with b:=b+1; unless you have range checking on.

Osmo

Re:Some pascal questions, please help.


Quote
ronka...@cc.helsinki.fi (Osmo Ronkanen) wrote:
>>Specifically, it is faster in execution, but *does not* report overflow
>>errors.

>Neither does other integer arithmetic.

>> If a byte variable is on 255 and you Inc() it, it will simply become
>>0.

>So will with b:=b+1; unless you have range checking on.

But the runtime errors can be caught with n:=n+1, but not with
inc(n) in TP.
Jud McCranie

Re:Some pascal questions, please help.


Quote
"G.Pohl" <pohl....@klosterfrau.de> wrote:
>I prefer the inc() and dec() procedures. I think the time thay were
>implemented I've read that they are much faster because they use one CPU
>command. I don't know ho this is in newer compilers.

In at least some other compilers, they are the same speed.

Jud McCranie

Re:Some pascal questions, please help.


Jud McCranie <jud.mccra...@mindspring.com> skrev i
diskussionsgruppsmeddelandet:kcdt5ssop7n6ksma6kbk60aqndehfgo...@4ax.com...

Quote
> There is a direct way by calling an interrupt, but I don't know
> what it is.

This is really old, I haven't used it in years, but it should still work:

PROCEDURE FlushKeyBuffer;
Var
  Regs : Registers;
Begin
  Regs.AX := ($0C shl 8) or 6;
  Regs.DX := $00FF;
  Intr($21,Regs);
End;

/HM

Re:Some pascal questions, please help.


There is a nice way of emptying the keypress buffer.

You can make use of the fact that the buffer is circular: the end byte is
"glued" to the start byte, you might say.
There are two pointers: one points at the first byte that is entered into
the buffer; the other to the last byte.
If anyone enters bytes through the keyboard, the end pointer increases; if
the end of the buffer is reached, it start from the beginning of the buffer
(this is why the buffer is "circular").
If the end pointer is one less than the start pointer, the buffer is "full".
If the program reads from the buffer and the buffer is emptied byte by byte,
the start pointers increases.
If the two pointers are equal, the buffer is considered as empty, whatever
bytes are in it. So, you can empty the buffer by making the pointers equal.

The following procedure does the trick:

procedure clrkeybuffer;
var c: char;
begin
  memw[$40:$1C]:=memw[$40:$1A]
end;

x-factorial <x!@x!.com> schreef in berichtnieuws
5cw74.9270$ZT5.3810...@news1.rdc1.on.wave.home.com...

Quote
> I got a couple of question here:
> 1) How do you empty the keypress buffer? I read the "Keypressed" thread
> above but it didn't seem to help.
> 2) Is it possible to read something that is already on the screen? e.g.
> to read what character is at certain x,y?
> 3) Is it consider bad to use inc() (just like goto, halt etc) instead of
> x:=x+1?
> Thank you.

Re:Some pascal questions, please help.


In article <vj7v5skec1lsuj0ig806kmudu0obfja...@4ax.com>,
Jud McCranie  <jud.mccra...@mindspring.com> wrote:

Quote
>ronka...@cc.helsinki.fi (Osmo Ronkanen) wrote:

>>>Specifically, it is faster in execution, but *does not* report overflow
>>>errors.

>>Neither does other integer arithmetic.

>>> If a byte variable is on 255 and you Inc() it, it will simply become
>>>0.

>>So will with b:=b+1; unless you have range checking on.

>But the runtime errors can be caught with n:=n+1, but not with
>inc(n) in TP.

Actually earlier versions did not catch integer overflows. On newer ones
that can be done with {$Q+}. One should note that if n is integer or
word then it is overflow but if it is byte or shortint then it is range
check error. This is because the arithmetic is done with 16 bits.

Osmo

Re:Some pascal questions, please help.


In article <83nmiq$13...@news.netcologne.de>,

Quote
G.Pohl <rp19...@rp-plus.de> wrote:
>I prefer the inc() and dec() procedures. I think the time thay were
>implemented I've read that they are much faster because they use one CPU
>command. I don't know ho this is in newer compilers.

It is not an issue of compilers but processors.  On 486 and Pentium
there is no speed benefit in using inc().

Osmo

Re:Some pascal questions, please help.


JRS:  In article <NNO74.3887$W33.8...@nntpserver.swip.net> of Tue, 21
Dec 1999 18:30:24 in news:comp.lang.pascal.borland, HM

Quote
<h.mol...@home.se> wrote:

>Jud McCranie <jud.mccra...@mindspring.com> skrev i
>diskussionsgruppsmeddelandet:kcdt5ssop7n6ksma6kbk60aqndehfgo...@4ax.com...

>> There is a direct way by calling an interrupt, but I don't know
>> what it is.

>This is really old, I haven't used it in years, but it should still work:

>PROCEDURE FlushKeyBuffer;
>Var
>  Regs : Registers;
>Begin
>  Regs.AX := ($0C shl 8) or 6;
>  Regs.DX := $00FF;
>  Intr($21,Regs);
>End;

ISTM that it will read a single normal character, or half an extended
one.  Regs.AX := $0C06 should do as well for the AX line; bit perhaps
you meant $0C00?.

--
? John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk   Turnpike v4.00   MIME. ?
 <URL: http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
 <URL: ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ;
 <URL: http://www.merlyn.demon.co.uk/clpb-faq.txt> Pedt Scragg: c.l.p.b. mFAQ.

Re:Some pascal questions, please help.


Quote
ronka...@cc.helsinki.fi (Osmo Ronkanen) wrote:
>Actually earlier versions did not catch integer overflows. On newer ones
>that can be done with {$Q+}.

That's right.

Jud McCranie

Re:Some pascal questions, please help.


Quote
ronka...@cc.helsinki.fi (Osmo Ronkanen) wrote:
>It is not an issue of compilers but processors.  On 486 and Pentium
>there is no speed benefit in using inc().

According to my tests with TP7 on a P-II, inc(x) is about 7%
faster than x:=x+1.  

(And with runtime checking on, inc should be faster since it
doesn't do the checking).

Jud McCranie

Go to page: [1] [2]

Other Threads