Microcontroller 8051
Microcontroller 8051
: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
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
AC
--
RS1
RS0
0V
PSW.7
PSW.6
PSW.5
PSW.4
PSW.3
PSW.2
Sub: MC
Logical operations
--
PSW.1 PSW.0
- 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
(1/2)
Flags Affected
C
C
C
C=0
C=0
C
C=1
C
AC
AC
AC
OV
OV
OV
OV
OV
(2/2)
Flags Affected
C
C
C
C
C=0
C = /C
C
Topic: Arithmetic &
A, source
A, source
; A = A + source
; A = A + source +
Sub: MC
Logical
operations
ADDC
is used
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
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
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 ;
The DA Instruction
DA
ADDC ..
DA
A
ADD ..
DA
A
Example 4 :
MOV
A, #47h
MOV
B, #25h
ADD
A, B
DA
Sub: MC
Logical operations
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
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
AB
AB
Sub: MC
Logical operations
; content in 7Fh
A
direct
; content
@Ri where i=0,or 1
Rn
where n=0,,7
in R1 decreased
Sub: MC
Logical operations
Logical Instructions
The source operand can be any of the 4 addressing
modes (i.e. immediate/register/ direct/indirect)
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
Sub: MC
Logical operations
RL A
RR A
Topic: and
Arithmetic
&
for multiplication
division
in
powers of 2
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 &
High Nibble
Low Nibble
SWAP A
Sub: MC
Logical operations
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
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