INC does not set the overflow flag 
Author Message
 INC does not set the overflow flag

Hi, I'm writting a little routine in assembler with TP and I face a bizarre
behavior :
mov ax,$FFFF
inc ax   <-- this is supossed to set the Overflow Flag = 1 but it does not.

 Why ?

I've tried with Q+ and Q- directive, nothig help...

        Do you know the solution ???

                Thanks

                Jean-Philippe Robichaud

                http://www.*-*-*.com/



Wed, 18 Jun 1902 08:00:00 GMT  
 INC does not set the overflow flag

Quote:
>Hi, I'm writting a little routine in assembler with TP and I face a bizarre
>behavior :
>mov ax,$FFFF
>inc ax   <-- this is supossed to set the Overflow Flag = 1 but it does not.

Inc only sets the overflow flag when the ~signed~ number overflows. This
means Inc will only set Overflow going from MaxInt ($7FFF) to MinInt
($8000).

Quote:
>I've tried with Q+ and Q- directive, nothig help...

What has the {$Q} compiler directive got to do with how the 80x86 sets its
flags register bits?

Quote:
> Do you know the solution ???

Depends what you're trying to do.

Jay
--

Jason Burgon - author of Graphic Vision

http://www.jayman.demon.co.uk



Wed, 18 Jun 1902 08:00:00 GMT  
 INC does not set the overflow flag
On Thu, 28 Jan 1999 03:42:51 -0000, "Jason Burgon"

Quote:

> Inc only sets the overflow flag when the ~signed~ number overflows. This
> means Inc will only set Overflow going from MaxInt ($7FFF) to MinInt
> ($8000).

> >I've tried with Q+ and Q- directive, nothig help...

> What has the {$Q} compiler directive got to do with how the 80x86 sets its
> flags register bits?

This should read: Arithmetic overflow checking ($Q} etc. is only done
for operations programmed in Pascal and not for instructions in
asm-end blocks. asm-end blocks are free-style wrestling.

By the way, Pascal's INC is never checked for overflow. If you want
overflow checking you have to replace it by w:=w+1 or w:=succ(w) if
you want to increment by 1. In this case Q+ will diagnose an
arithmetic overflow if w:word was $FFFF.

For Pascal operations the checking routine _will_ consider the
processor's overflow flag , for instance if you multiply two integers.

Regards
Horst



Wed, 18 Jun 1902 08:00:00 GMT  
 INC does not set the overflow flag
Quote:
>Hi, I'm writting a little routine in assembler with TP and I face a bizarre
>behavior :
>mov ax,$FFFF
>inc ax   <-- this is supossed to set the Overflow Flag = 1 but it does not.
> Why ?

Because inc NEVER sets the overflow flag. Refer any x86 asm manual
and you'll see that. Use instead add ax,1 or instead of jump on
carry jump on zero.

Bye,
Stefan
---
please remove the P in my email-adress to answer me



Wed, 18 Jun 1902 08:00:00 GMT  
 INC does not set the overflow flag

Quote:

>Carry and overflow are different flags. Inc sets the overflow but not
>the carry. The above does not set the overflow as there is no overflow
>from -1 to 0. Overflow is for signed and carry for unsigned numbers.

Note that if one wanted to check carry after inc one could check the
zero flag.  This does not work with dec though.

Osmo



Wed, 18 Jun 1902 08:00:00 GMT  
 INC does not set the overflow flag

Quote:

>>Hi, I'm writting a little routine in assembler with TP and I face a bizarre
>>behavior :
>>mov ax,$FFFF
>>inc ax   <-- this is supossed to set the Overflow Flag = 1 but it does not.
>> Why ?
>Because inc NEVER sets the overflow flag. Refer any x86 asm manual
>and you'll see that. Use instead add ax,1 or instead of jump on
>carry jump on zero.

Carry and overflow are different flags. Inc sets the overflow but not
the carry. The above does not set the overflow as there is no overflow
from -1 to 0. Overflow is for signed and carry for unsigned numbers.

Osmo



Wed, 18 Jun 1902 08:00:00 GMT  
 INC does not set the overflow flag

Quote:
>Hi, I'm writting a little routine in assembler with TP and I face a
>face a bizarre behavior :
>mov ax,$FFFF
>inc ax   <-- this is supossed to set the Overflow Flag = 1
>but it does not.

The asm-instruction  "inc operand" WILL affect the overflow.
It was mentioned in other postings that this will happen on:

asm
  mov ax,$7FFF
  inc  ax
end;

Quote:
>I've tried with Q+ and Q- directive, nothig help...

Pure appearance of overflow-Flag in processor-status
will result in nothing.

Pascal-Instructions

{$q+}
a:=$7FFF;
a:=a+1;

would result in something like

mov [1234],7FFF      {assuming var a is ds:1234}
mov ax,[1234]
add ax,1
jno go_on
call xxxx:xxxx               {Call Overflow-Error Routine}
go_on:
mov [1234],ax

As you see, any instruction that could cause an overflow
is coded with explicit check of overflow-flag behind it.

By the way the pascal-procedure inc(x) does NOT check
for occurence of overflow even in $q+

To "see" your overflow-flag try this:

var   Inc_me:word;  o_flag:word;
begin
  inc_me:=0;
  repeat
    asm
      mov o_flag,0
      inc inc_me

      mov o_flag,1

    end;
    if o_flag=1 then writeln('overflowed to ',inc_me);
  until keypressed;
end.

regards

   Harald



Wed, 18 Jun 1902 08:00:00 GMT  
 INC does not set the overflow flag


Quote:
>Hi, I'm writting a little routine in assembler with TP and I face a
bizarre
>behavior :
>mov ax,$FFFF
>inc ax   <-- this is supossed to set the Overflow Flag = 1 but it does
not.

> Why ?

The CARRY flag is what you need to be testing.  The OVERFLOW flag is
used only when dealing with signed (twos complement) numbers.

CARRY is set if the result cannot be expressed as an UNSIGNED
representation (a carry out of the high-order bit has occurred) while
the OVERFLOW is set if the result cannot be expressed in a SIGNED
representation (a carry into the high-order but not a carry out of the
high-order bit).

In your case (FFFF = -1) being incremented in signed arithmetic becomes
0000 which is correct and there is certainly no OVERFLOW.  If this were
an unsigned value, incrementing 65535 becomes zero which is certainly
not 65536 and hence the CARRY bit is set.  Try incrementing 32767
($7FFF) and watch how the OVERFLOW gets set when the result
becomes -32768 ($8000).

Cheers,
Lester



Wed, 18 Jun 1902 08:00:00 GMT  
 INC does not set the overflow flag

Quote:



>>Hi, I'm writting a little routine in assembler with TP and I face a
>bizarre
>>behavior :
>>mov ax,$FFFF
>>inc ax   <-- this is supossed to set the Overflow Flag = 1 but it does
>not.

>> Why ?

...

Quote:
>In your case (FFFF = -1) being incremented in signed arithmetic becomes
>0000 which is correct and there is certainly no OVERFLOW.  If this were
>an unsigned value, incrementing 65535 becomes zero which is certainly
>not 65536 and hence the CARRY bit is set.

No no no no. INC and DEC do NOT set the carry flag.

Osmo



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Flag one item in a set

2. Doing a locate on a TQuery result set on MSACCESS table in D3

3. HELP : "Invalid Paramater", when doing Tquery.Open, seems to not release resources

4. Delphi *.hlp files still not done

5. Set Alias Code Not Working - Any Ideas ?

6. Interbase - Setting table active - field not found

7. Interbase - Setting table active - field not found

8. CHARACTER SET WIN_1252 not defined with Delphi 2.0

9. Why BDECFG32 not setting LOCAL SHARE val

10. Set Range will not work? DELPHI 1

11. TTABLE routines for Setting ranges do not work.

12. "Parameter not set in query string"

 

 
Powered by phpBB® Forum Software