8051 Arithmetic Instructions

Arithmetic Instructions

One of the operand in arithmetic type of instruction is accumulator. Other operand, if there, will be immediate value, direct memory, register, register indirect, except in case of MUL and DIV where register B is used to hold the value of the 2nd operand.

ADD

 

Format of Instruction Opcode/Encoding Example and Operation Flags Affected
ADD A, #imm 00100100 Immediate
8-bit
ADD A, #03h

A=A+#data

C,AC,OV,P
ADD A, @Ri 0010011i (i is Reg number) ADD A, @R1

A=A+ (Ri)

C,AC,OV,P
ADD A, direct 00100101 Direct ADD A, 20H

A=A+(20)

C,AC,OV,P
ADD A, Rn 00101nnn ADD A, R0

A=A+R0

C,AC,OV,P

 

 ADDC

 

Format of Instruction Opcode/Encoding Example and Operation Flags Affected
ADDC A, #imm 00110100 #immediate ADDC A, #43h C,AC,OV,P
ADD A, @Ri 0011011i (i is Reg number) ADDC A, @R1 C,AC,OV,P
ADD A, direct 00110101 Direct ADDC A, 20H C,AC,OV,P
ADD A, Rn 00111nnn ADDC A, R0 C,AC,OV,P

 Write a program to add multiple byte of data in assembly language of 8051

MOV R1, 55h                         ; Mov value at address 55h to R1

MOV R2, 56h             ; Mov value at address 56h to R2

MOV 57h, #34h                      ; Move immediate value 34h at address 57h

MOV A, R1                            ; MOV content of R1 to Accumulator

ADD A, @R2                         ; A = A + Value at address given in R2(i.e. address 56h)

ADDC A, 57h                                    ; A= A+(57h) +C

DA

The DA instruction adjusts the eight-bit value in the Accumulator resulting from the earlier addition of two variables (each in packed-BCD format), producing two four-bit digits. Any ADD or ADDC instruction may have been used to perform the addition.

If addition of two packed BCD no. results in a number >9 or there is a carry; The result will be corrected/adjusted by adding 6 to the result.

DA A cannot simply convert a hexadecimal number in the Accumulator to BCD notation, nor does it apply to decimal subtraction

 

format Opcode/Encoding example / Operation Flags affected
DA Acc 11010100 DA A

If(A3-0 >9)

A=A+6
If(A7-4 >9)

A=A+60h

C

Example

MOV    A, #34h          ; A= 34h

MOV    B, #46h       ; B= 46h

ADD     A, B                       ; A= 34h+46h

DA       A     ; If (A3-0 > 9) or AC=’1’ then A=A+6; if A7-4 >9 or C=’1’ then A=A+60

DEC

Decrements the operand by ‘1’.

Example :

MOV A, #34h

DEC A                           ; Answer A=34-1 = 33

MOV A, #00h

DEC A                        ; overflows to 0FFh

 

Note
Modifying PORT Value

DEC P0                       ; Reads port output data latch (not at the pins), decrements and writes back to port data latch

 

Format of Instruction Opcode/  Encoding Example/ Operation Flags affected
DEC Acc 00010100 DEC A

A=A-1

P
DEC @Ri 0001011i DEC @R1

(Ri)=(Ri)-1

None
DEC Direct 00010101, direct DEC 20h

(direct)=(direct)-1

none
DEC Rn 00011nnn DEC R7

Rn=Rn-1

none

 DIV

Devisor in Register B

Dividend in Accumulator

Usage

DIV AB          ; Divides content of A by content of B,

Quotient in Accumulator

Remainder in B

If divisor in register B is ‘00h’, then DIV A/B will result in Overflow indicating division-by-zero error

 

Format of Instruction Opcode/Encoding Example Flags affected
DIV AB 10000100 DIV AB C, OV, P

 

Write a program to divide an eight number by another 8-bit number

MOV A, @R1            ; mov value from address given in R1 to A

MOV B, 35h   ; MOV value from address 35h to B

DIV AB                      ; Divide A by B, quotient in A, remainder in B

INC Instruction

Used to increment the value of the operand

Note 0FF + 1 = 00 overflow occurs, No flag affected by this action

Modifying PORT Value

INC   P0                      ; Reads port output data latch (not the input pins of the port), increments it and writes back to output port data latch

not the input pins of the port.

Format of Instruction Op-code/Encoding Example / Operation Flags affected
INC @Ri 0000011i INC @R0

(Ri)=(Ri)+1

None
INC Acc 00000100 INC A

A=A+1

P
INC Direct 00000101 INC 34h

(direct)=

(direct)+1

none
INC data pointer 10100011 INC DPTR

DPTR=DPTR+1

none
INC reg 00001nnn INC Rn

Rn=Rn+1

none

MUL Instruction

Multiplicand in Accumulator

Multiplier in register B

Product in BA  ; with high byte in register B and low byte in A

The OV flag is set if the product is greater than 255 (0FFh)

Format of instruction Opcode/ Encoding Example /Operation Flags affected
MUL AB 10100100 MUL AB

BA=A*B

C,OV,P

 

Program to multiply two numbers

 MOV R1, 35h             ; suppose (35h) = 15h (Decimal 21)

MOV R2, #5h             ; R2= 5h                (decimal)

MOV A, R1                            ; A = 15h

MOV B, R2                            ; B= 05h

MUL A, B                              ; B=00; A=69h   (21×05=105 = 69h)

 

 SUBB Instruction

This instruction subtracts the specified byte variable and the carry flag from the accumulator.

A = A –(num+C)

It sets the carry flag if a borrow is required for bit 7 of the result.

Format Opcode example /Operation Flags affected
SUBB A, #data 10010100 #8-bit data SUBB A, #01h

A=A-C-#data

C,AC,OV,P
SUBB A, @Ri 1001011i SUBB A, @R1

A=A-C-(Ri)

C,AC,OV,P
SUBB A, direct 10010101 Direct SUBB A, 44h

A=A-C-(direct)

C,AC,OV,P
SUBB A, Rn 10011nnn SUBB A, R5

A=A-C-Rn

C,AC,OV,P

                                           

Write a program to subtract three 8bit numbers

MOV A, #56h                     ; A = 56h

MOV R1, #34h                   ; R1 = 34h

MOV R2, 56h                     ; R2 = (56h)

SUB A, R1                         ; A= A-R1

SUBB A, R2                       ; A= A-(C+R2)

Updated: June 10, 2019 — 3:18 pm

Leave a Reply

Your email address will not be published. Required fields are marked *

care4you © 2014 care4you © Revision-1: 2016 care4you © Revision-2: 2019 Connect On Facebook DMCA.com Protection Status
error: Content is protected !!