The whole flag thing is confusing the heck out of me. The definitions on the web seem really plain. I can't seem to get a really good applicable explanation to all this.
According to their definitions, - carry: indicates an unsigned integer overflow - overflow: indicates a signed integer overflow - zero: an operation produced zero - sign: operation produced a negative number
So how in the world are the following sentences true? - The following instructions will set the Sign flag: (The answer here is 252 not a negative number. So why is the sign flag set?)
mov al,0FEh
sub al,2
Adding 7Fh and 05h in an 8-bit register sets the Overflow flag.(The answer here is 132. It’s not above 255 so why is there an overflow?)
Adding 0FFh and 05h in an 8-bit register does not set the Overflow flag.(the answer is 300 so how is there not an overflow flag on? It’s above 256)
Adding 5 to 0FBh in an 8-bit register sets the Zero flag (The answer here is 256, not 0. I understand the 8 bit can only hold 255 but where does the “0” come from? I just don’t get it.)
Can someone please let me know what I'm doing wrong here and what the correct way to approach this is? Thanks.
1. If the sum of two numbers with the sign bits off yields a result number with the sign bit on, the "overflow" flag is turned on. 2. If the sum of two numbers with the sign bits on yields a result number with the sign bit off, the "overflow" flag is turned on.
Carry Flag (CY) – Carry is generated when performing n bit operations and the result is more than n bits, then this flag becomes set i.e. 1, otherwise, it becomes reset i.e. 0.
The overflow flag is thus set when the most significant bit (here considered the sign bit) is changed by adding two numbers with the same sign (or subtracting two numbers with opposite signs).
The answer here is 252 not a negative number. So why is the sign flag set?
As unsigned it is 252, but unsigned can't have a sign, so the sign flag is only related to number handled as signed. It doesn't matter how you handle it, the processor always handles it as signed with the sign flag. So 252 is over 127, therefore it is negative in 2's complement and the sign bit is set.
Adding 7Fh and 05h in an 8-bit register sets the Overflow flag.(The answer here is 132. It’s not above 255 so why is there an overflow?)
As you said, overflow is set when a signed number would overflow. A signed 8bit variable can go from -128 to 127. Therefore going from 127 to 132 is an overflow.
Adding 0FFh and 05h in an 8-bit register does not set the Overflow flag.(the answer is 300 so how is there not an overflow flag on? It’s above 256)
Again, overflow is signed overflow. This causes an unsigned overflow, so the carry bit will be set.
Adding 5 to 0FBh in an 8-bit register sets the Zero flag (The answer here is 256, not 0. I understand the 8 bit can only hold 255 but where does the “0” come from? I just don’t get it.)
As you said, 8 bits can go up to 255. After that it overflows and the lowest 8 bits are 0. So the result is zero and the zero bit is set.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With