Vous êtes sur la page 1sur 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 Instruction :
There are 24 arithmetic opcodes which are
grouped into the following
ADDtypes:
and ADDC
SUBB
MUL
DIV
INC
DEC
DA
Sub: MC
Logical operations

Topic: Arithmetic &

Arithmetic Flags
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 ( +, , *, / )
Sub: MC
Logical operations

Topic: Arithmetic &

Arithmetic Flags (Conditional Flags)


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

All the above flags are stored in the


Program Status Word (PSW)
CY

AC

--

RS1

RS0

0V

PSW.7

PSW.6

PSW.5

PSW.4

PSW.3

PSW.2

Sub: MC
Logical operations

--

PSW.1 PSW.0

Topic: Arithmetic &

Arithmetic Flags (Conditional Flags)


CY
AC

- RS1
RS0
0V

-
P

PSW.7
PSW.6
PSW.5
PSW.4
PSW.3
PSW.2
PSW.1
PSW.0

Carry flag
Auxiliary carry flag
Available to the user for general p
Register Bank selector bit 1
Register Bank selector bit 0
Overflow flag
User definable flag
Parity flag

The C flag is keeping track in unsigned


operations
The OV flag is keepingTopic:
trackArithmetic
in signed
Sub: MC
&
operations
Logical operations

Instructions that Affecting Flags


Instruction Mnemonic
ADD
ADDC
SUBB
MUL
DIV
DA A
SETB C
MOV C, bit
Sub: MC
Logical operations

(1/2)

Flags Affected
C
C
C
C=0
C=0
C
C=1
C

AC
AC
AC

Topic: Arithmetic &

OV
OV
OV
OV
OV

Instructions that Affecting Flags


Instruction Mnemonic
ORL C, bit
ANL C, bit
RLC
RRC
CLR C
CPL C
CJNE
Sub: MC
Logical operations

(2/2)

Flags Affected
C
C
C
C
C=0
C = /C
C
Topic: Arithmetic &

The ADD and ADDC Instructions


ADD
ADDC
C

A, source
A, source

; A = A + source
; A = A + source +

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 ordinaryTopic:
addition
Arithmetic &

Sub: MC
Logical
operations
ADDC
is used

to add a carry after the LSB

Example -1
Show how the flag register is affected by the following instructions.
MOV A, #0F5h
; A = F5h
ADD A, #0Bh
; A = F5 + 0B = 00

Solution

F5h
+ 0Bh

1111 0101
+ 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 zero
AC = 1 since there is a carry from D3 to D4
Sub: MC
Logical operations

Topic: Arithmetic &

Example -2
Assume 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:

NEXT:
NEXT1:

MOV
MOV
ADD
JNC
INC
ADD
JNC
INC

END
Sub: MC
Logical operations

A, 40h
R7, #0
A, 41h
NEXT
R7
A, 42h
NEXT1
R7

; set A = RAM location 40h


; set R7 = 0
; add A with RAM location 41h
; if CY = 0 dont accumulate carry
; keep track of carry
; add A with RAM location 42h
; if CY = 0 dont accumulate carry
; keep track of carry
Topic: Arithmetic &

Example -3
Write 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
MOV
ADD
C=1
MOV
MOV
ADDC

C
; make C=0
A, #0E7h ; load the low byte now A=E7h
A, #8Dh ; add the low byte now A=74h and
R6, A
A, #3Ch
A, #3Bh

Sub:MOV
MC
Logical operations

;
;
;
;
R7, A ;

save the low byte of the sum in R6


load the high byte
add with the carry
3B + 3C + 1 = 78 (all in hex)
Arithmetic
& sum
save theTopic:
high byte
of the

The DA Instruction
DA

ADDC ..
DA
A

ADD ..
DA
A

The action is to decimal adjust the register A


Used after the addition of two BCD numbers

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

; adjust for BCD addition (A=72h)

Sub: MC
Logical operations

Topic: Arithmetic &

Example 4 of DA Instruction
Hex
47
+ 25
6C
+ 6
72

BCD
0100 0111
+ 0010 0101
0110 1100
+
0110
0111 0010

Offset decimal 6 !
Sub: MC
Logical operations

Topic: Arithmetic &

The SUBB Instruction


SUBB

A, source

SUBB
SUBB
SUBB
SUBB

A,
A,
A,
A,

#data
direct
@Ri , where i =0 or 1
Rn, where n =0,1,,7

No borrow: A = A source
With borrow:
A = A source carry (i.e. borrow)
Note that the 8051 uses the 2s complement
method to do subtraction
After execution:
The C flag is set to 1 if a borrow is needed into bit
7
The AC flag is set to 1 if a borrow is needed into bit
Sub: MC3
Topic: Arithmetic &
Logical operations

The MUL Instruction


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
Sub: MC
Logical operations

Topic: Arithmetic &

The DIV Instruction


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

Sub: MC
Logical operations

Topic: Arithmetic &

The INC and DEC Instructions


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
increased by 1INC
DEC R1INC
INC
by 1
INC
Sub: MC
Logical operations

; content in 7Fh
A
direct
; content
@Ri where i=0,or 1
Rn
where n=0,,7

in R1 decreased

Topic: Arithmetic &

Logic Operation in 8051


Logical operations
Rotate and swap operations
Comparison operations

Sub: MC
Logical operations

Topic: Arithmetic &

Logical Instructions
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
Examples

Instruction

ANL A,R0

ORL A,R0

XRL A,R0

A before:
R0 before:
A afterwards:

10010111
11110010
10010010

10010111
11110010
11110111

10010111
11110010
01100101

Sub: MC
Logical operations

Topic: Arithmetic &

The CLR and CPL Instructions


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

Sub: MC
Logical operations

Topic: Arithmetic &

The Rotate Instructions

RL A
RR A

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
Sub:Also
MC useful
Logical operations

Topic: and
Arithmetic
&
for multiplication
division
in
powers of 2

The Rotate Instructions


RL A
Rotates A one bit position to the left
RLC
A
Rotates A and the carry flag one bit position to
the left
RR A
Rotates A one bit position to the right
RRC
A
Rotates A and the carry flag one bit position to
the right
Note that for RLC and RRC,
you have to know
Sub: MC
Topic: Arithmetic &
the
C flag first
Logical
operations

The Rotate Instructions


7

0
Before: 10011100
After: 00111001

RL A
C

Carry Flag

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

RLC A
7

Before: 10011100
After: 01001110

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

RR A
7

Sub: MC
Logical operations

RRC A

Carry
Flag
Topic:

Arithmetic &

The SWAP Instruction


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

High Nibble

Low Nibble

SWAP A

Register A = 5Eh (original value) after SWAP


Register A = E5h

Sub: MC
Logical operations

Topic: Arithmetic &

Comparison Operation
CJNE
destination, source, relative
address
Compare the source and destination
operands first
Jump to the relative address (subroutine) if
they are not equal
Carry flag = 1, if destination-byte is less than the source-byte,
Otherwise, the carry flag is cleared.
Sub: MC
Logical operations

Topic: Arithmetic &

Example -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 port
HERE: MOV A, P1
; get P1
CJNE A, #63h, HERE
; keep
monitoring unless
;
P1=63h

Sub: MC
Logical operations

Topic: Arithmetic &

Vous aimerez peut-être aussi