Board index » delphi » Flags

Flags

Hello, all

I've been working a lot with interrupts and now I need to know the state of
some of the flags (namely zero flag). I know the flags are stored in a 16
bit register but I don't know where. I suppose there is an absolute adress
in memory to access them directly, but I don't know where is that adress. I
don't want to use assembly because I'm very weak with it. If my supposition
is not correct, how can I access the state of the flags?

Thanks in advance

 

Re:Flags


Quote
Peter Pan wrote:
> I've been working a lot with interrupts and now I need to know the state of
> some of the flags (namely zero flag). I know the flags are stored in a 16
> bit register but I don't know where. I suppose there is an absolute adress
> in memory to access them directly, but I don't know where is that adress. I
> don't want to use assembly because I'm very weak with it. If my supposition
> is not correct, how can I access the state of the flags?

The flags are not stored in memory, so forget about this possibility.

Use the following function instead:

function Zero: boolean;
inline($9F/             { lahf }
       $80/$E4/$40/     { and ah, 64 }
       $88/$E0);        ( mov al, ah }

It will return TRUE if the flag is set, FALSE otherwise.

There is something you should note, however: If the zero flag is NOT
set when the function is being called, the instruction "and  ah, 64"
will set it implicitly. It should suit your needs, though.

--
Downloading signature, please wait...

Re:Flags


Quote
Peter Pan wrote:
> I've been working a lot with interrupts and now I need to know the state of
> some of the flags (namely zero flag). I know the flags are stored in a 16
> bit register but I don't know where. I suppose there is an absolute adress
> in memory to access them directly, but I don't know where is that adress. I
> don't want to use assembly because I'm very weak with it. If my supposition
> is not correct, how can I access the state of the flags?

There is a little problem with your question. I simply cannot imagine
what you could do with the flags within a program or even within
an interrupt procedure in pure Pascal code. But anyway...

The flags are stored in the CPU in a register, and there is one
instruction to move them into a word variable (talking about standard
16-bit programming): PUSHF which pushes the flags on the stack.

With TP 6 and above you can
  .... pascal code
ASM
  PUSHF
  POP Flags
End;
  if (Flags and 1) <> 1 then .... etc.

But this is of very little use if you do not consider the way how
the previous Pascal instructions have been converted to machine
code. This is why I wrote above: I do not see the use of it.

I do not remember (but of course there could be anyway) a Pascal
instruction to do that.

If you are using the Registers record as defined in the DOS unit,
you have the Registers.Flags word. This is also the context where
you can find more information in the F1 help about the flag bits.

But the record is a special thing, it is used for DOS system calls
and in some cases for interrupt programming. You will find more
in the F1 help.
--
Franz Glaser, Glasau 3, A-4191 Vorderweissenbach Austria +43-7219-7035-0
Muehlviertler Elektronik Glaser.  Industrial control and instrumentation
http://members.eunet.at/meg-glaser/           mailto:meg-gla...@eunet.at
http://www.geocities.com/~franzglaser/ http://members.xoom.com/f_glaser/

Re:Flags


In article <7ohme5$5o...@duke.telepac.pt>,
Quote
Peter Pan <deneb.kai...@mail.telepac.pt> wrote:

:I've been working a lot with interrupts and now I need to know the state of
:some of the flags (namely zero flag). I know the flags are stored in a 16
:bit register but I don't know where. I suppose there is an absolute adress
:in memory to access them directly, but I don't know where is that adress. I

I would suggest a different approach. For flags define a variable of
the type registers, e.g.

  var regs : registers;

Testing is done as e.g.

  if regs.flags and FCarry <> 0 then exit;  { Is the carry flag set? }

That's the carry flag. The zero flag is FZero.

See

 164988 Apr 25 1999 ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip
 tsfaqp.zip Common Turbo Pascal Questions and Timo's answers, linked

for some further examples.

   All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance  ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/>  ; FIN-65101,  Finland
Digital photos collection at http://www.uwasa.fi/ktt/lasktoim/photo/

Re:Flags


Quote
Timo Salmi wrote:
> I would suggest a different approach. For flags define a variable of
> the type registers, e.g.

>   var regs : registers;

> Testing is done as e.g.

>   if regs.flags and FCarry <> 0 then exit;  { Is the carry flag set? }

But a machine instruction that causes the zero flag to toggle does not
necessarily affect the memory area where regs.flags is situated. If a machine
instructions changes the state of the zero flag, it does however not change the
value of regs.flags and therefore testing regs.flags could return wrong results.

It's probably better to use assembly code except if one wants to inspect the
flags register after an interrupt call performed with the procedures Msdos or
Intr, which write the value of the flags register to regs.flags.

A bit long and complicated, but I hope you understand what I mean.

--
Downloading signature, please wait...

Re:Flags


I pretend to 'rewrite' a set of functions that allows me to access (in a
better way, I hope) the keyboard, without using the CRT unit. My intention
is to use interrupt 16h - which I can do with Pascal or very simple Assembly
(that I'm capable of :)))) - but, for function 11h I need to know the state
of the Zero Flag.

I think the PUSHF, POP solution is suitable and very simple.

Thank you very much.

Pedro Claro

Re:Flags


Quote
Frederic wrote:
> function Zero: boolean;
> inline($9F/             { lahf }
>        $80/$E4/$40/     { and ah, 64 }
>        $88/$E0);        ( mov al, ah }

  First of all, please do not interperet this as a flame - it is a real
question. Why do people use the inline($xx / .. / ..); statement instead
of asm .... end; ? Is it because older pascal compilers did not not
support it or something?

         - Bob

Re:Flags


Quote
Bob wrote:
>   First of all, please do not interperet this as a flame - it is a real
> question. Why do people use the inline($xx / .. / ..); statement instead
> of asm .... end; ? Is it because older pascal compilers did not not
> support it or something?

If you declare a function like this:

function Whatever: word; assembler;
asm
  ...
end;

the compiler will insert a call to this function into the main program
instead of the code of the function itself. That call makes the code
larger by a few unnecessary bytes and takes some time to execute.

If you write:

function Whatever: word;
begin
  asm
    ..
  end
end;

the compiler will again call the function and it will create a stack
frame. This is again wasted time and code.

Inline functions are not called. The code they consist of is directly
inserted into the main program. If you view the disassembly of such a
function, you don't even see that in Pascal it used to be a
sub-program as it is seamlessly integrated into the main program.

Re:Flags


Quote
Frederic wrote:

> Bob wrote:

> >   First of all, please do not interperet this as a flame - it is a real
> > question. Why do people use the inline($xx / .. / ..); statement instead
> > of asm .... end; ? Is it because older pascal compilers did not not
> > support it or something?

> If you declare a function like this:

> function Whatever: word; assembler;
> asm
>   ...
> end;

> the compiler will insert a call to this function into the main program
> instead of the code of the function itself. That call makes the code
> larger by a few unnecessary bytes and takes some time to execute.
> ...

This kind of INLINE is treated somewhat similar to an ASM - MACRO with
multiple expansion.
--
Franz Glaser, Glasau 3, A-4191 Vorderweissenbach Austria +43-7219-7035-0
Muehlviertler Elektronik Glaser.  Industrial control and instrumentation
http://members.eunet.at/meg-glaser/           mailto:meg-gla...@eunet.at
http://www.geocities.com/~franzglaser/ http://members.xoom.com/f_glaser/

Re:Flags


Quote
> > function Zero: boolean;
> > inline($9F/             { lahf }
> >        $80/$E4/$40/     { and ah, 64 }
> >        $88/$E0);        ( mov al, ah }
> question. Why do people use the inline($xx / .. / ..); statement instead
> of asm .... end; ? Is it because older pascal compilers did not not
> support it or something?

yes old compilers dit not support asm end.
but even in BP7 you must use inline ine the interface part if
you want an inline asm procedure.

Re:Flags


Bob ha escrit:

Quote

>   First of all, please do not interperet this as a flame - it is a real
> question. Why do people use the inline($xx / .. / ..); statement instead
> of asm .... end; ? Is it because older pascal compilers did not not
> support it

Yes (integrated asm comes with version 6.0 IIRC).

Also because this is the easiest way to handle some problems, like
using 386+ instructions.

Antoine

Other Threads