# 8085 program to subtract two consecutive bytes of an array

MicroprocessorMicrocontroller8085

Here we will see how we can subtract two consecutive elements in an array using 8085.

## Problem Statement

Write 8085 program to subtract two consecutive elements of an array and store them in the same location. The results will be placed at the same location from where they are taken. The numbers are stored from location 8001. The size of array is stored at 8000.

## Discussion

Here we will solve this problem by using one subroutine. That will subtract two consecutive numbers and stored them into correct position. That subroutine will be called multiple times to subtract all consecutive pairs. The task will be followed half of the number of elements. So we are taking the count, then rotate it to the right to make it half. We are rotating through carry. if the carry is already high it may store different result, so we are clearing the carry by using STC and CMC instructions.

Data

8000
0A
8001
E9
8002
D3
8003
8004
61
8005
2A
8006
1F
8007
5D
8008
26
8009
A9
800A
35

## Program

HEX Codes
Labels
Mnemonics
F000
21, 00, 80

LXI H,8000
Point to get array size
F003
7E

MOV A,M
F004
37

STC
set carry flag
F005
3F

CMC
complement carry flag
F006
0F

RRC
rotate right through carry
F007
47

MOV B,A
Store A into B
F008
23
LOOP
INX H
point to next location
F009
CD, 11, F0

F00C
05

DCR B
Decrease B by 1
F00D
C2, 08, F0

JNZ LOOP
F010
76

HLT
Terminate the program
F011
0E, 00
MVI C,00H
clear C flag
F013
7E

MOV A,M
load first number from memory to A
F014
23

INX H
point to next location
F015
96

SUB M
sub A and memory element
F016
D2, 1A, F0

JNC STORE
F019
0C

INR C
else increase C by 1
F01A
71
STORE
MOV M,C
store C into memory
F01B
2B

DCX H
point to previous location
F01C
77

MOV M,A
store A into memory
F01D
23

INX H
point to next location
F01E
C9

RET
return from subroutine

## Output

Data

8001
16
8002
00
8003
4C
8004
00
8005
0B
8006
00
8007
37
8008
00
8009
74
800A
00

Updated on 30-Jul-2019 22:30:26