1. Prof. Nitin Ahire 1
Singed number concept
in
8051 Microcontroller
Nitin Ahire
XIE Mahim
2. Prof. Nitin Ahire 2
Singed number concept
• In everyday life, numbers are used that
could be +Ve or –Ve
• In the 8051 the MSB bit is set aside for the
sign
• If MSB(D7) =1 represent negative number
• If MSB(D7) =0 represent positive numbers
• The remaining bit D0 to D6 used for
magnitude.
3. Prof. Nitin Ahire 3
As 8051 is a 8 bit controller here
we consider the 8 bit numbers
D7 D6 D1D2D3D4D5 D0
SIGN MAGNITUDE
4. Prof. Nitin Ahire 4
POSITIVE NUMBERS
• The range of positive numbers can be
represent by the format as shown in figure
• It range from 0 to +127
0 0000 0000
+1 0000 0001
+5 0000 0101
+127 0111 1111
5. Prof. Nitin Ahire 5
NEGATIVE NUMBERS
• For negative number D7=1 however the
magnitude is represented in it’s 2’s
compliment form
• Steps
1 write the magnitude of the number in 8 bit
binary
2 invert each bit
3 add one to it
6. Prof. Nitin Ahire 6
• Show how the 8051 would represent – 5
• Sol:
0000 0101 5 in 8-bit binary
1111 1010 invert bit
1111 1011 add 1 ( which is FB h )
7. Prof. Nitin Ahire 7
• Show how the 8051 would represent -128
• Sol:
1000 0000
0111 1111
1000 0000 ( which become 80h)
9. Prof. Nitin Ahire 9
Overflow problem in signed number
• What is an overflow?
• If the result of an operation on the signed
numbers is too large for the register, an
overflow has occurred
CY AC -OVRS0RS1FO P
10. Prof. Nitin Ahire 10
Example
• Examine the following code and analyze
the result
MOV A,# +96
MOV R1, # +70
ADD A,R1
11. Prof. Nitin Ahire 11
Solution
+ 96 0110 0000 60h
+ 70 0100 0110 46h
-------- ---------------
+166 1010 0110 A6 and OV =1
according to CPU the result -90h which is wrong.
( OV = 1)
+166 is not the valid signed number. ( 0 to +127)
12. Prof. Nitin Ahire 12
When is the OV flag set?
1. There is 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.
13. Prof. Nitin Ahire 13
Example 1
• MOV A, #-2 (FEh) ( 1111 1110)
• MOV R1,#-5 (FBh) ( 1111 1011)
• ADD A,R1 (F9h=-7, 1111 1001correct),
• So CPU generate OV=0
14. Prof. Nitin Ahire 14
Example 2
MOV A, #-128 ; (80H)
MOV R4,#-2 (FEH)
ADD A,R4 ( A=7EH =+127 INVALID )
So CPU generate OV =1
15. Prof. Nitin Ahire 15
Example 3
• MOV A,#+7; A=0000 0111 (A=07H)
• MOV R1,#+18; R1= 0001 0010 (R1=12H)
• ADD A,R1; A=0001 1001 (A=19H=+25)
According to CPU, this is +25, which is
correct (OV=0)
16. Prof. Nitin Ahire 16
• In any signed number addition OV
indicates whether the result is valid or not
• If OV=1, the result is erroneous
• If OV=0, the result is valid.
17. Prof. Nitin Ahire 17
BCD ADDITION
• Assume that 5 BCD data items are stored
in RAM location starting at 40H, write a
program to find the sum of all numbers.
The result must be in BCD.
18. Prof. Nitin Ahire 18
• Data 40=(71)
• 41=(11)
• 42=(65)
• 43=(59)
• 44=(37)
19. Prof. Nitin Ahire 19
Solution
• MOV R0,#40H
MOV R2,05H
CLR A
MOV R7,A
AGAIN:ADD A,@R0
DA A
JNC NEXT
INC R7
NEXT: INC RO
DJNZ R2,AGAIN
END