37
Presented by HEMANTH 12MT06PED011 ARITHMETIC AND LOGICAL OPERATIONS

Hemanth143

Embed Size (px)

Citation preview

Page 1: Hemanth143

Presented byHEMANTH 12MT06PED011

ARITHMETIC AND

LOGICALOPERATIONS

Page 2: Hemanth143

INSTRUCTION TYPES:

The 8051 instructions are divided into

1. Data transfer

2. Program branching

3. Logical

4. Arithmetic

Page 3: Hemanth143

LOGICAL INSTRUCTIONS:Introduction

Application in machine control

Byte operators: for manipulating all 8051 RAM

Bit operators: For bit addressable internal RAM area some

SFRs

Bit operators yield compact program code and enhances

execution speed

TYPES:

1. Byte-Level logical operation

2. Bit-Level logical operations

3. Rotate and Swap operations

Page 4: Hemanth143

Bit and Byte level Instructions:

Boolean operator

• AND

• OR

• XOR

• NOT

8051 Mnemonic

• ANL(AND logical)

• ORL(OR logical)

• XRL(exclusive ORlogical)

• CPL(complement)

Entire bits of destination are effected.

Destination: register A or direct address in internal RAM

No flags are affected unless the direct address is PSW

Only internal RAM or SFRs may be logically manipulated

Byte Level Operations:

Page 5: Hemanth143

BYTE LEVEL INSTRUCTIONS

Page 6: Hemanth143

CPL A is called 1’s complement

Page 7: Hemanth143

EXAMPLE OF LOGIC OPERATIONS

MOV A, #OFFh ;A= FFh =1111 1111

MOV R0, #77h ;R0=77h =0111 0111

ANL A,R0 ;A =77h =0111 0111

MOV 15h,A ;15h contains 77h

CPL A ;A=88h=1000 1000

ORL 15h,#88h ;15h Contains FFh

XRL A,15h ;A=77h

XAL A,R0 ;A=00h

ANL A,15h ;A=00h

ORL A,R0 ;A=77h

CLR A ;A=00h

XRL 15h,A ;15h= FFh

XRL A,R0 ;A=77h

Page 8: Hemanth143

Bit Level Operation:

• All I/O ports and registers A, B, PSW, IP,

IE,SCON, and TCON are bit-addressable

• Bit-addressable SFRs:

• No flags, other than C flag, are affected, unless the

flag bit is an addressed bit.

Page 9: Hemanth143

Bit-level Boolean operations:

If the destination bit is a port bit, the SFR latch bit is

affected, not the pin.

ANL C,/b and ORL C,/b do not alter the addressed bit b.

Page 10: Hemanth143

Example: Write a program to save the accumulator in

R7 of bank 2.

Solution:

CLR PSW.3

SETB PSW.4 ;RS1=1& RS0=0 ;BANK 2 IS SELECTED

MOV R7,A

Page 11: Hemanth143

ROTATE & SWAP INSTRUCTIONS:

RL rotate a byte to left; MSB becomes LSB

RLC Rotate a byte and the carry bit left; the carry

bit becomes LSB, MSB becomes the carry.

RR Rotate a byte to right; LSB becomes MSB

RRC Rotate a byte and the carry bit right; LSB

becomes the carry, the carry the MSB

SWAP Exchange the low and high nibbles in a byte

Only The register A can be rotated one bit position to the

left or right with or without including the C flag

RRC and RLC affects only carry flag.

Page 12: Hemanth143

RR A: Rotate right:

MOV A , #36H ;A= 0011 0110

RR A ; 0011 0110 ,so A= 0001 1011

RR A ;A=1000 1101

RL A: Rotate Left

MOV A,#72H ;A =0111 0010

RL A ; 0111 0010 ,so A = 1110 0100

RL A ;A = 1100 1001

Page 13: Hemanth143

RRC A: Rotate Right through Carry

MSB-LSB CCLR C ;CY = 0

MOV A,#A5H ;A = 1010 0101

RRC A ;A = 0101 0010;CY = 1

RRC A ;A = 1010 1001;CY = 0

RLC A: Rotate Left through Carry

CLR C ;CY=0

MOV A,#6A ;A=0110 1010

RLC A ; A=1101 0100;CY=0

RLC A ;A=1010 1000;CY=1

MSB-LSB C

Page 14: Hemanth143

SWAP A

It swaps the lower nibble and the higher nibble

SWAP works only on the accumulator(A)

E.g. MOV A,#72H ;A = 72H

SWAP A ;A = 27H

Page 15: Hemanth143

Write a program that finds the number of 1’s in a given

byte(097h).

MOV R1,#00

MOV R7,#08 ;count=08

MOV A,#097H

CLR C

AGAIN: RLC A

JNC NEXT ;check for CY

INC R1 ;if CY=1 add to count

NEXT: DJNZ R7,AGAIN

Page 16: Hemanth143

Assume that bit P2.2 is used to control an outdoor light

and bit P2.5 a light inside a building. Show how to turn

on the outside light and turn off the inside one.

• Solution:

SETB C ;CY = 1

ORL C,P2.2 ;CY = P2.2 ‘OR’ed with CY

MOV P2.2,C ;turn it on if not on

CLR C ;CY = 0

ANL C,P2.5 ;CY = P2.5 ‘AND’ed with CY

MOV P2.5,C ;turn it off if not off

Page 17: Hemanth143

ARITHMETIC OPERATIONS:MNEMONIC OPERATION

INC destination Increment destination by 1

DEC destination Decrement destination by 1

ADD/ADDC destination, source Add source to destination

without/with carry( C ) flag

SUBB destination, source Subtract, with carry,

source from destination

MUL AB Multiply the contents of

registers A & B

DIV AB Divide the contents of register A by

contents of register B ( A/B )

DA A Decimal adjust the register A

Page 18: Hemanth143
Page 19: Hemanth143

INCREMENTING &

DECREMENTING

Page 20: Hemanth143
Page 21: Hemanth143

ADD

ADD A, Source ;A = A + source The instruction ADD is used to add two operands

Destination operand is always in register A

Source operand can be a register, immediate data, or in

memory

Memory-to-memory arithmetic operations are never

allowed in 8051 Assembly language

Page 22: Hemanth143

FLAGS AFFECTED:

C,AC,OV

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

position 7; it is cleared to 0 otherwise.

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

position 3; it is cleared to 0 otherwise.

• OV flag is set to 1 if there is a carry out of bit

position 7,not bit position6 or if there is a carry out

of bit position6 not bit position7.

Page 23: Hemanth143

Show how the flag register is affected by the

following instruction.

MOV A,#0F5H ;A=F5 hex

ADD A,#0BH ;A=F5+0B=00

Solution:

F5H + 1111 0101+

0BH = 0000 1011=

100H 0000 0000

CY =1, since there is a carry out from D7

PF =1, because the number of 1s is zero (an even number),

PF is set to 1.

AC =1, since there is a carry from D3 to D4

Page 24: Hemanth143

SIGNED & UNSIGNED ADDITIION

• Unsigned numbers:8 bit magnitude

• Signed numbers: use bit 7 as a sign bit

Bit 0-6 magnitude of no.

Bit 7=1 means no. is negative

=0 : positive

In signed form, a single byte number range:-128d (1000

0000) to +127d(0111 1111)

In unsigned form:00h(0000 0000) to FFh(1111 1111)

• Adding or subtracting unsigned numbers may create

CY flag when exceeds FFh or a borrow when minuend

is less than subtrahend.

• In the case of signed numbers along with CY,OV is

used for sign bit actions.

Page 25: Hemanth143

• Unsigned addition:

There is a Carry out from sum, so

Carry flag is set to 1

Signed addition:. addition of Unlike signed numbers

Here there is a carry from bit 7,so CY=1. Also a carry from

bit 6 and OV=0. So no action is needed to correct the sum

Page 26: Hemanth143

Addition of like signed numbers:

• +100d(0110 0100=64h)

+050d(0011 0010=32h)

= +150d(1001 0110=96h) CY=0,OV=1

Page 27: Hemanth143

• -030d(1110 0010, 2’s cmpl of 0001 1110)+

-050d(1100 1110, 2’s cmpl of 0011 0010)

=-080d(1011 0000 ) CY=1.OV=1

SIGNED NO. 1011 0000=-48d,so we have to take

2’s complement of this answer.

Page 28: Hemanth143

ADDC: Add with carry:

ADDC IS NORMALLY USED TO ADD A CARRY AFTER

THE LSB ADDITION IN A MULTI BYTE PROCESS.

Page 29: Hemanth143

• ADD A,R5

1Ch= 0001 1100+

A1h= 1010 0001

BDh=1011 1101

• ADDC A,#10h:

• 5Eh=0101 1110+

10h=0001 0000=(6Eh=0110 1110)

6Eh+1(CY)=6Fh

Example:

Page 30: Hemanth143

SUBTRACTION:SUBB

Page 31: Hemanth143

MULTIPLICATION:MUL AB

• MUL AB ;multiply A by B; put the lower-order byte

of product in A and higher order byte in B

• Only registers A and B can be used.

• Use A and B as both as source and destination

address for operation

• Both numbers are unsigned

• OV will be set, If A*B>FFh, it means the product is

>8bit and Reg B should be checked for higher byte.

• CY is always cleared to 0.

Page 32: Hemanth143

Example :Multiply FFh with FFh

• A=FFh & B=FFh will give largest possible

product(FE01h).

• 01h will be stored in A and FEh in B.

• OV=1;CY=0

Example :

Page 33: Hemanth143

DIV AB :DIVISION

• DIV AB ;divide A by B; put the integer part of

quotient in Reg A and the integer part of the reminder in

B

• Only registers A and B can be used.

• Use A and B as both as source and destination address

for operation

• Both numbers are unsigned

Page 34: Hemanth143

Example:

*divide FFh by 2Ch:

255/44=5.8d

35d=23h

Page 35: Hemanth143
Page 36: Hemanth143

Byte cycle

Page 37: Hemanth143