Experiment-3: 8085 ALP to Subtract of two 8 bit numbers with and without borrow


Experiment-3: Write a program to perform: i. Subtraction of two 8 bit numbers without borrows. ii. Subtraction of two 8 bit numbers with borrows.

Objective:

To understand the assembly program

To draw flowchart for subtraction of two numbers

To develop the assembly program for subtraction

To analyse the program and interpret errors and result

Requirement:

Operating System – Windows, Linux

  • 8085 Simulator

Example-1. Subtraction of two 8 bit numbers without borrows.

Algorithmic Steps:

  1. Input two numbers say one in Accumulator and another in register B

  2. Subtract B from A ; A= A-B

  3. Check carry ( if A <B, carry flag will be set and difference will be in 2’s complement, Carry=’1’ means difference is -ve. Do correction (take 2’s complement to find magnitude of -ve number)

  4. Save the result in variable ‘x’ and Borrow if any in Borrow variable

Program:

Address

OP

data

label

Mnemonic

Comments/remarks

4200

C3

07 042

jmp start

;data

4203

0F (1 bytes)

A1: db 15

1st data item

4204

73 (1 bytes)

B1: db 115

2nd data item

4205

<1 bytes>

x: ds 1

Storage space for difference

4206

<1 bytes>

Borrow: ds 1

Storage space for borrow

;code

4207

00

start: nop

4208

0E

00

MVI C, 0

;initialize carry

420A

3A

04 042

LDA B1

; load number

420D

47

MOV B, A

; transfer to register B

420E

3A

03 042

LDA A1

;load number in Acc

4211

90

SUB B

; A= A – B

4212

D2

1C 42

JNC save

4215

2F

CMA

to get the magnitude of -ve number,do 2’s complement

4216

C6

01

ADI 01

; get magnitude of -ve no.

4218

0C

INR C

; Record Borrow in register C

4219

C3

1C 42

JMP save

421C

00

save:

nop

421D

32

05 042

STA x

; save difference

4220

79

MOV A, C

; copy borrow in Acc

4221

32

06 042

STA Borrow

; save borrow

4224

76

hlt

OUTPUT

Example-2: 16-bit subtraction (uses SUB and SBB )

ALGORITHM

Data variable

Variable
Address
Value
Address
Value
X:
4200
25
4201
15
Y:
4203
12
4204
05
Z:
4205
4206

Algorithm:

  • Load source (X) address in HL pair
  • Load source (Y) address in DE pair
  • Load Accumulator (A) from memory i.e [HL]
  • Swap HL and DE pair HL ßàDE
  • Load register B from memory [HL]
  • Subtract A-B
  • Save result in register C
  • Increment HL
  • Swap HL with DE
  • Increment HL
  • Load accumulator from memory address 4201 [HL]
  • Swap HL and DE
  • Load register B from memory
  • Subtract with borrow
  • Increment HL to save the result
  • Save register C (least significant result) at 4205 [HL] <-  C
  • Increment HL now 4206
  • Save most significant digit of result in memory 4206 [HL] <- A
  • stop

Program in assembly with OPCODEs

Address
OP-CODE /DATA
Label
Mnemonic
Comments
C9 09 042
JMP start
4200
19 0F
X:
DB 25, 15
4203
0C 05
Y:
DB 12, 05
4205
Z:
DB 00, 00
4209
00
Start:
nop
420A
21 03 042
LXI H,X
Initialize memory pointer in HL
420D
11 05 042
LXI D,Y
Initialize memory pointer in DE
4210
7E
MOV A, M
ACC = [HL]
4211
EB
XCHG
HL  <–>DE

 

4212
46
MOV B, M
B <- [hl]
4213
90
SUB B
A = A-B
4214
4F
MOV C, A
C=A
4215
23
INX H
HL++
4216
EB
XCHG
HL <—>DE
4217
23
INX H
HL++
4218
7E
MOV A, M
ACC = [HL]
4219
EB
XCHG
HL <–> DE
421A
46
MOV B, M
B = [HL]
421B
98
SBB B
A = A-B-1
421C
23
INX H
HL++
421D
71
MOV M,C
[HL] = C
421E
23
INX H
HL++
421F
77
MOV M,A
[HL] = A
4220
76
HLT
end

Result:

Viva Questions:

Que-1: How is subtraction performed in computer.

Ans:

Subtraction is performed by using 2’s complement arithmetic.

Que-2: What are the commands available in 8085 microprocessor for performing subtraction

Ans:

  1. SUB    –  Subtraction
  2. SBB   – subtraction with borrow

error: Content is protected !!