27
Subject Code :151001 Name Of Subject :Microcontroller & Interfacing Name of Unit :Arithmetic& logical instructions Topic :Arithmetic & logical operations Name of Faculty : Mr. Haresh Suthar Miss. Madhuri Thakkar

Arithmetic & logical operations in 8051

Embed Size (px)

Citation preview

Page 1: Arithmetic & logical operations in 8051

Subject Code :151001Name Of Subject :Microcontroller & InterfacingName of Unit :Arithmetic& logical instructions Topic :Arithmetic & logical operationsName of Faculty : Mr. Haresh Suthar Miss. Madhuri ThakkarName of Students : (i) Savalia Avani(100870111020) (ii) Patel Jay (100870111021)

Page 2: Arithmetic & logical operations in 8051

Arithmetic Instruction :Arithmetic Instruction :

ADD and ADDCSUBBMULDIVINCDECDA

There are 24 arithmetic opcodes which are grouped into the following types:

Sub: MC Topic: Arithmetic & Logical operations

Page 3: Arithmetic & logical operations in 8051

Flag: It is a 1-bit register that indicates the status of the result from an operation

Flags are either at a flag-state of value 0 or 1

Arithmetic flags indicate the status of the results from mathematical operations ( +, , *, / )

Arithmetic FlagsArithmetic Flags

Sub: MC Topic: Arithmetic & Logical operations

Page 4: Arithmetic & logical operations in 8051

There are 4 arithmetic flags in the 8051Carry (C)Auxiliary Carry (AC)Overflow (OV)Parity (P)

All the above flags are stored in the Program Status Word (PSW)

Arithmetic Flags (Conditional Flags)Arithmetic Flags (Conditional Flags)

CY AC -- RS1 RS0 0V -- PPSW.

7PSW.

6PSW.

5PSW.

4PSW.

3PSW.

2PSW.

1PSW.0

Sub: MC Topic: Arithmetic & Logical operations

Page 5: Arithmetic & logical operations in 8051

The C flag is keeping track in unsigned operations

The OV flag is keeping track in signed operations

Arithmetic Flags (Conditional Flags)Arithmetic Flags (Conditional Flags) CY PSW.7 Carry flag AC PSW.6 Auxiliary carry flag -- PSW.5 Available to the user for general purpose RS1 PSW.4 Register Bank selector bit 1 RS0 PSW.3 Register Bank selector bit 0 0V PSW.2 Overflow flag -- PSW.1 User definable flag P PSW.0 Parity flag

Sub: MC Topic: Arithmetic & Logical operations

Page 6: Arithmetic & logical operations in 8051

Instructions that Affecting Flags Instructions that Affecting Flags (1/2)(1/2)

Instruction Mnemonic

Flags Affected

ADD C AC OV

ADDC C AC OV

SUBB C AC OV

MUL C = 0 OV

DIV C = 0 OV

DA A C

SETB C C = 1

MOV C, bit CSub: MC Topic: Arithmetic & Logical operations

Page 7: Arithmetic & logical operations in 8051

Instructions that Affecting Flags Instructions that Affecting Flags (2/2)(2/2)

Instruction Mnemonic

Flags Affected

ORL C, bit C

ANL C, bit C

RLC C

RRC C

CLR C C = 0

CPL C C = /C

CJNE CSub: MC Topic: Arithmetic & Logical operations

Page 8: Arithmetic & logical operations in 8051

ADDA, source ; A = A + sourceADDC A, source ; A = A + source + C

A register must be involved in additions

The C flag is set to 1 if there is a carry out of bit 7

The AC flag is set to 1 if there is a carry out of bit 3

ADD is used for ordinary addition

ADDC is used to add a carry after the LSB addition in a multi-byte process

The ADD and ADDC InstructionsThe ADD and ADDC Instructions

Sub: MC Topic: Arithmetic & Logical operations

Page 9: Arithmetic & logical operations in 8051

Solution F5h 1111 0101

+ 0Bh + 0000 1011

100h 0000 0000 After the addition, register A (destination)

contains 00 and the flags are as follows: CY = 1 since there is a carry out from D7 P = 0 because the number of 1s is zeroAC = 1 since there is a carry from D3 to D4

Example -1Example -1Show how the flag register is affected by the following instructions.

MOV A, #0F5h ; A = F5hADD A, #0Bh ; A = F5 + 0B = 00

Sub: MC Topic: Arithmetic & Logical operations

Page 10: Arithmetic & logical operations in 8051

Example -2Example -2Assume that RAM locations 40h – 42h have the following values. Write a program to find the sum of the values in these locations. At the end of the program, register A should contain the low byte and R7 contain the high byte.

RAM locations: 40h = (7Dh), 41h = (EBh), 42h = (C5h)Solution: MOV A, 40h ; set A = RAM location 40hMOV R7, #0 ; set R7 = 0

ADD A, 41h ; add A with RAM location 41h JNC NEXT ; if CY = 0 don’t accumulate carry

INC R7 ; keep track of carryNEXT: ADD A, 42h ; add A with RAM location 42h JNC NEXT1 ; if CY = 0 don’t accumulate carry

INC R7 ; keep track of carryNEXT1: END

Sub: MC Topic: Arithmetic & Logical operations

Page 11: Arithmetic & logical operations in 8051

Example -3Example -3Write a program segment to add two 16-bit numbers. The numbers are 3CE7h and 3B8Dh. Place the sum in R7 and R6; R6 should store the lower byte.

CLR C ; make C=0

MOV A, #0E7h ; load the low byte now A=E7h ADD A, #8Dh ; add the low byte now A=74h and C=1 MOV R6, A ; save the low byte of the sum in R6 MOV A, #3Ch ; load the high byte ADDC A, #3Bh ; add with the carry

; 3B + 3C + 1 = 78 (all in hex) MOV R7, A ; save the high byte of the sum

Sub: MC Topic: Arithmetic & Logical operations

Page 12: Arithmetic & logical operations in 8051

DA AThe action is to “decimal adjust” the register

AUsed after the addition of two BCD numbers

The DA InstructionThe DA Instruction

Example 4 : MOV A, #47h ; A=47h first BCD operand

MOV B, #25h ; B=25h second BCD operand

ADD A, B ; hex (binary) addition (A=6Ch)

DA A ; adjust for BCD addition (A=72h)

ADDC …..DA A

ADD …..DA A

Sub: MC Topic: Arithmetic & Logical operations

Page 13: Arithmetic & logical operations in 8051

Example 4 of DA InstructionExample 4 of DA Instruction

Hex BCD47 0100 0111

+ 25 + 0010 01016C 0110 1100

+ 6 + 011072 0111 0010

Offset decimal 6 !

Sub: MC Topic: Arithmetic & Logical operations

Page 14: Arithmetic & logical operations in 8051

SUBB A, source

No borrow: A = A – sourceWith borrow: A = A – source – carry (i.e. borrow)Note that the 8051 uses the 2’s complement

method to do subtractionAfter execution:The C flag is set to 1 if a borrow is needed into bit

7The AC flag is set to 1 if a borrow is needed into

bit 3

The SUBB InstructionThe SUBB Instruction SUBB A, #dataSUBB A, directSUBB A, @Ri , where i =0 or 1SUBB A, Rn, where n =0,1,,7

Sub: MC Topic: Arithmetic & Logical operations

Page 15: Arithmetic & logical operations in 8051

MUL AB

Uses registers A and B as both source and destination registers

Numbers in A and B are multiplied, then put the lower-order byte of the product in A and the high-order byte in B

The OV flag is set to 1 if the product > FFh

Note that the C flag is 0 at all times

The MUL InstructionThe MUL Instruction

Sub: MC Topic: Arithmetic & Logical operations

Page 16: Arithmetic & logical operations in 8051

DIV AB

Similarly, it uses registers A and B as both source and destination registers

The number in A is divided by B. The quotient is put in A and the remainder (if any) is put in B

The OV flag is set to 1 if B has the number 00h (divide-by-zero error)

Note that the C flag is 0 at all times

The DIV InstructionThe DIV Instruction

Sub: MC Topic: Arithmetic & Logical operations

Page 17: Arithmetic & logical operations in 8051

To increment (INC) or decrement (DEC) the internal memory location specified by the operand

No change with all the arithmetic flags in this operation

e.g. INC 7Fh ; content in 7Fh increased by 1 DEC R1 ; content in R1 decreased by 1

The INC and DEC InstructionsThe INC and DEC Instructions

INC AINC directINC @Ri where i=0,or 1INC Rn where n=0,,7

Sub: MC Topic: Arithmetic & Logical operations

Page 18: Arithmetic & logical operations in 8051

Logical operations

Rotate and swap operations

Comparison operations

Logic Operation in 8051Logic Operation in 8051

Sub: MC Topic: Arithmetic & Logical operations

Page 19: Arithmetic & logical operations in 8051

The source operand can be any of the 4 addressing modes (i.e. immediate/register/ direct/indirect)

ANL can be used to clear (0) certain bits

ORL can be used to set (1) certain bits

Logical InstructionsLogical Instructions

Instruction ANL A,R0 ORL A,R0 XRL A,R0

A before: 10010111 10010111 10010111R0 before: 11110010 11110010 11110010A afterwards: 10010010 11110111 01100101

ExamplesExamples

Sub: MC Topic: Arithmetic & Logical operations

Page 20: Arithmetic & logical operations in 8051

CLR AAll bits in register A are clearedCPL AAll bits in register A are complemented (inverted)Note that CLR and CPL instructions operate on register A only

The CLR and CPL InstructionsThe CLR and CPL Instructions

Sub: MC Topic: Arithmetic & Logical operations

Page 21: Arithmetic & logical operations in 8051

Contents in register A is rotated one bit position to the left or to the right (operated in A only)

The bit shifted out is used as the new bit shifted in

May include the C flag in the operation

Useful in inspecting the bits in a byte one by one

Also useful for multiplication and division in powers of 2

The Rotate InstructionsThe Rotate InstructionsRL ARR A

Sub: MC Topic: Arithmetic & Logical operations

Page 22: Arithmetic & logical operations in 8051

RL ARotates A one bit position to the leftRLC ARotates A and the carry flag one bit position to the leftRR ARotates A one bit position to the rightRRC ARotates A and the carry flag one bit position to the rightNote that for RLC and RRC, you have to know the C flag first

The Rotate InstructionsThe Rotate Instructions

Sub: MC Topic: Arithmetic & Logical operations

Page 23: Arithmetic & logical operations in 8051

01234567

RL A

01234567C

RLC ACarry Flag

01234567

RR A

Before: 10011100 After: 00111001

C01234567

Carry FlagRRC A

Before: 10011100 CY = 0 After: 00111000 CY = 1

Before: 10011100 After: 01001110

Before: 10011100 CY = 1 After: 11001110 CY = 0

The Rotate InstructionsThe Rotate Instructions

Sub: MC Topic: Arithmetic & Logical operations

Page 24: Arithmetic & logical operations in 8051

Swapping the lower-nibble (lower 4 bits) and the higher-nibble (upper 4 bits) of register A.

017 6 5 4 3 2

High Nibble Low Nibble

SWAP A

The SWAP InstructionThe SWAP Instruction

Register A = 5Eh (original value) after SWAP Register A = E5h

Sub: MC Topic: Arithmetic & Logical operations

Page 25: Arithmetic & logical operations in 8051

CJNE destination, source, relative address

Compare the source and destination operands first

Jump to the relative address (subroutine) if they are not equal

Comparison OperationComparison Operation

Carry flag = 1, if destination-byte is less than the source-byte,Otherwise, the carry flag is cleared.

Sub: MC Topic: Arithmetic & Logical operations

Page 26: Arithmetic & logical operations in 8051

Example -5Example -5

Write a program segment to monitor P1 continuously for the value of 63h. It should get out of the monitoring only if P1 = 63h.

Solution :MOV P1, #0FFh ; make P1 an

input portHERE:MOV A, P1 ; get P1

CJNE A, #63h, HERE ; keep monitoring unless ; P1=63h

Sub: MC Topic: Arithmetic & Logical operations

Page 27: Arithmetic & logical operations in 8051