In everyday life, numbers are used that could be positive or negative. Many applications require signed data.
Signed 8-bit operands:
In signed byte operands, the most significant bit (MSB) is set aside for the sign (+ or -), while the rest of the bits are used for the magnitude. The sign is represented by 0 for positive (+) numbers and 1 for negative (-) numbers.
D7 (MSB) is the sign and D0 to D6 are set aside for the magnitude of the number. If D7 = 0, the operand is positive, and if D7 = 1, it is negative.
For positive numbers, D7 is 0 and the range is 0 to 127. For negative numbers, D7 is 1 and the range is -1 to -128. The magnitude of negative number is represented in 2’s complement. The range of 8-bit signed number is -128 to +127.
When using signed numbers, overflow problem may occur. If the result of an operation on signed numbers is too large for the register, an overflow has occurred and the programmer is notified by raising the OV (overflow) flag.
(+96) + (+70) = +166 which is out of range and results in overflow. The CPU sets OV = 1 to indicate the overflow.
In 8-bit signed number operations, OV is set to 1 if either of the following two conditions occurs:
1. There is a carry from D6 to D7 but no carry out of D7 (CY = 0).
2. There is a carry from D7 out (CY = 1) but no carry from D6 to D7.
In other words, the overflow flag is set to 1 if there is a carry from D6 to D7 or from D7 out, but not both. This means that if there is a carry both from D6 to D7 and from D7 out, OV = 0.
(-128) + (-2) = -130 which is out of range and results in overflow. The CPU sets OV = 1 to indicate the overflow.
OV indicates whether the result is valid or not. If OV = 1, the result is erroneous; if OV = 0, the result is valid. In unsigned number addition we must monitor the status of CY (carry flag), and in signed number addition, the OV (overflow) flag must be monitored by the programmer.
Signed 8-bit operands:
In signed byte operands, the most significant bit (MSB) is set aside for the sign (+ or -), while the rest of the bits are used for the magnitude. The sign is represented by 0 for positive (+) numbers and 1 for negative (-) numbers.
D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
Sign | magnitude |
D7 (MSB) is the sign and D0 to D6 are set aside for the magnitude of the number. If D7 = 0, the operand is positive, and if D7 = 1, it is negative.
For positive numbers, D7 is 0 and the range is 0 to 127. For negative numbers, D7 is 1 and the range is -1 to -128. The magnitude of negative number is represented in 2’s complement. The range of 8-bit signed number is -128 to +127.
Decimal | Binary | Hex |
---|---|---|
-128 | 1000 0000 | 80 |
-127 | 1000 0001 | 81 |
… | ||
-1 | 1111 1111 | FF |
0 | 0000 0000 | 00 |
+1 | 0000 0001 | 01 |
… | ||
+127 | 0111 1111 | 7F |
When using signed numbers, overflow problem may occur. If the result of an operation on signed numbers is too large for the register, an overflow has occurred and the programmer is notified by raising the OV (overflow) flag.
ORG 0H
MOV A, #+96
ADD A, #+70
END
(+96) + (+70) = +166 which is out of range and results in overflow. The CPU sets OV = 1 to indicate the overflow.
In 8-bit signed number operations, OV is set to 1 if either of the following two conditions occurs:
1. There is a carry from D6 to D7 but no carry out of D7 (CY = 0).
2. There is a carry from D7 out (CY = 1) but no carry from D6 to D7.
In other words, the overflow flag is set to 1 if there is a carry from D6 to D7 or from D7 out, but not both. This means that if there is a carry both from D6 to D7 and from D7 out, OV = 0.
ORG 0H
MOV A, #-128
ADD A, #-2
END
(-128) + (-2) = -130 which is out of range and results in overflow. The CPU sets OV = 1 to indicate the overflow.
OV indicates whether the result is valid or not. If OV = 1, the result is erroneous; if OV = 0, the result is valid. In unsigned number addition we must monitor the status of CY (carry flag), and in signed number addition, the OV (overflow) flag must be monitored by the programmer.
Related topics:
8051 Addition | 8051 Addition of Unsigned Numbers | 8051 Subtraction of Unsigned Numbers | 8051 Multiplication Unsigned 8-bit | 8051 Division Unsigned 8-bit | 8051 Binary Coded Decimal
List of topics: 8051
No comments:
Post a Comment