8051 singed number concept [compatibility mode]

19

Click here to load reader

Transcript of 8051 singed number concept [compatibility mode]

Page 1: 8051 singed  number concept [compatibility mode]

Prof. Nitin Ahire 1

Singed number concept

in

8051 Microcontroller

Nitin Ahire

XIE Mahim

Page 2: 8051 singed  number concept [compatibility mode]

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.

Page 3: 8051 singed  number concept [compatibility mode]

Prof. Nitin Ahire 3

As 8051 is a 8 bit controller here we consider the 8 bit numbers

D7 D6 D1D2D3D4D5 D0

SIGN MAGNITUDE

Page 4: 8051 singed  number concept [compatibility mode]

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

Page 5: 8051 singed  number concept [compatibility mode]

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

Page 6: 8051 singed  number concept [compatibility mode]

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 )

Page 7: 8051 singed  number concept [compatibility mode]

Prof. Nitin Ahire 7

• Show how the 8051 would represent -128

• Sol:

1000 0000

0111 1111

1000 0000 ( which become 80h)

Page 8: 8051 singed  number concept [compatibility mode]

Prof. Nitin Ahire 8

Range of singed numbers

• Decimal binary Hex

-128 1000 0000 80

-127 1000 0001 81

-126 1000 0010 82

… ….. …… ….

-2 1111 1110 FE

-1 1111 1111 FF

0 0000 0000 00

+1 0000 0001 01

+2 0000 0010 02

… …… ……. ….

+127 0111 1111 7F

Page 9: 8051 singed  number concept [compatibility mode]

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

Page 10: 8051 singed  number concept [compatibility mode]

Prof. Nitin Ahire 10

Example

• Examine the following code and analyze

the result

MOV A,# +96

MOV R1, # +70

ADD A,R1

Page 11: 8051 singed  number concept [compatibility mode]

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)

Page 12: 8051 singed  number concept [compatibility mode]

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.

Page 13: 8051 singed  number concept [compatibility mode]

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

Page 14: 8051 singed  number concept [compatibility mode]

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

Page 15: 8051 singed  number concept [compatibility mode]

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)

Page 16: 8051 singed  number concept [compatibility mode]

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.

Page 17: 8051 singed  number concept [compatibility mode]

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.

Page 18: 8051 singed  number concept [compatibility mode]

Prof. Nitin Ahire 18

• Data 40=(71)

• 41=(11)

• 42=(65)

• 43=(59)

• 44=(37)

Page 19: 8051 singed  number concept [compatibility mode]

Prof. Nitin Ahire 19

Solution • MOV R0,#40H

MOV R2,05HCLR AMOV R7,A

AGAIN:ADD A,@R0DA AJNC NEXTINC R7

NEXT: INC RODJNZ R2,AGAIN

END