Friday 17 February 2012

Status flags


Status flags in ARM processor

The most common status flags are: V (overflow), N (sign status), Z (zero or not), C (carry bit, remember it is not borrow bit of substraction).
  • V - This flag is set when the sign of result is wrong. This methodology is called "twos complement"
    • For addition, V is set if msb (s) is equal to msb (d) and msb (result) is not equal to msb (d), which means the addition two positive numbers is negative, or the addition of two negative numbers is positive.
    • For substraction, V is set if msb (s) is not equal to msb (d) and msb (result) is not equal to msb (d), which means the result of one positive number substracting negative number is negative, or the result of one negative number substracting positive number is positive.
    • Anyway, because the most significant bit represents the sign bit, so when we do some operation with large number which will make the result exceed the scope of operandV represents the wrong operation
    • Because assembly number cannot recognize signed or unsigned, so with the help of V bit we can control the calculation is signed or unsigned.
  • N - It is set is msb of the result is 1 which means the result can be treated as negative number.
  • Z - It is set if the result is zero.
  • C - It is set if the result of addition exceed the destination register and is cleared if a substraction generates a borrow bit. We will talk about this in the next part for detail.


Carry flag vs. Borrow flag[1]

"While the carry flag is well-defined for addition, there are two possible ways to use the carry flag for subtractive operations.
One uses the bit as a borrow flag, setting it if a<b when computing ab, and a borrow must be performed. A subtract with borrow (SBB) instruction will compute abC = a−(b+C), while a subtract without borrow (SUB) acts as if the borrow bit were clear. The 8080Z80x86 and 68k families (among others) use a borrow bit.[1]
The other takes advantage of the identity that −x = not(x)+1 and computes ab as a+not(b)+1. The carry flag is set according to this addition, and subtract with carry computes a+not(b)+C, while subtract without carry acts as if the carry bit were set. The 6502 and PowerPC processors use this convention. The 6502 is a particularly well-known example because it does not have a subtract without carry operation, so software must ensure that the carry flag is set before every subtract operation where a borrow is not required.

The modern convention is to refer to the first alternative as a "borrow bit", while the second is called a "carry bit". However, there are exceptions in both directions; the VAX and NS320xx architectures use the borrow bit convention, but call their abC operation "subtract with carry" (SBWC). PA-RISC uses a carry bit convention, but calls its a+not(b)+C operation "subtract with borrow" (SUBB)."


There are two types of substract with carry bit: a-b-C type and a+not(b)+C.

a-b-C type:
When borrow, it is set as 1When no borrow, set 0.
a+not(b)+C type:
When borrow, it is set as 0. When no borrow, set 1In fact, this type of substraction calculation is exactly addition calculation. Here must remember "not(b)+1" is the complement of "b".


References:

No comments:

Post a Comment

Difference between "docker stop" and "docker kill"

 To stop a running container, you can use either "docker stop" or "docker kill" command to do so. Although it seems doin...