flat assembler
Message board for the users of flat assembler.

Index > Tutorials and Examples > Newbie - Overflow Flag

Author
Thread Post new topic Reply to topic
SC0U7



Joined: 20 Feb 2018
Posts: 23
SC0U7 02 Nov 2019, 18:26
Hello guys i am learning assembly language im currently on Flags.
I thinking about Overflow Flag

when i have somethink like this:

Code:
mov al,7fh
mov cl,1h
add al,cl
 
;al = 0x80
;Overflow flag is set    


But my question is how cpu know how is number negative or positive?
It may be both i am right?
But why is Overflow flag set after this?
Image
Thanks for any reply


Embarassed
Post 02 Nov 2019, 18:26
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8357
Location: Kraków, Poland
Tomasz Grysztar 02 Nov 2019, 18:51
As a fortunate coincidence, you can find this topic discussed in two of my recent videos.

I explained the basic ideas of the signed encoding and SF/OF in the part 6 of my ongoing x86 tutorial.

And then I also started a new semi-advanced mini-series about using 2-adic numbers to explain these things in detail. There I derive some actual formulas for the value of OF after addition/subtraction operation.
Post 02 Nov 2019, 18:51
View user's profile Send private message Visit poster's website Reply with quote
SC0U7



Joined: 20 Feb 2018
Posts: 23
SC0U7 02 Nov 2019, 19:50
Thank you Tomasz but if i am right then the reason why is Overflow flag set in 8bit register is fact the 128 in complement method not exist ? the last bit make a error for computer 8bit register then set OF. because for make OF on Zero it must be in range −128 and 127 right? Cuz my AL register cannot hold 128 then it only change it on EAX last byte 000DFF80 like 80 iam right? Thanks
Post 02 Nov 2019, 19:50
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8357
Location: Kraków, Poland
Tomasz Grysztar 02 Nov 2019, 19:58
In signed interpretation the highest bit is the sign bit. In this case you add two numbers that have the sign bit 0, so they are positive numbers, but the result has the highest bit 1 and therefore would be interpreted as negative number, even though the result of adding two positive numbers should also be positive. This discrepancy is an indication that an overflow occurred - the result of addition was too large to be correctly encoded as a positive number in 8 bits with sign bit.
Post 02 Nov 2019, 19:58
View user's profile Send private message Visit poster's website Reply with quote
SC0U7



Joined: 20 Feb 2018
Posts: 23
SC0U7 02 Nov 2019, 20:10
so when i try this

mov al,7fh
mov cl,-1h
add al,cl

The Overflow Flag will be not set
Post 02 Nov 2019, 20:10
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8357
Location: Kraków, Poland
Tomasz Grysztar 02 Nov 2019, 20:14
Yes. You can also see a similar example (-1 + 3) analyzed near the end of my aforementioned 2-adic video.
Post 02 Nov 2019, 20:14
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.