View Course Path

Arithmetic Instructions in 8085 – With example codes

How are arithmetic operations carried out in a microprocessor? In this post, we will look at the arithmetic instructions possible in 8085. We’ll try to get an idea about how an elementary processor carries out its arithmetic instructions. Towards the end, we will look at an example to understand the practical application of these instructions. The objective of this post is to introduce you to different types of arithmetic instructions, their mnemonics, and what goes on in the background when they execute.

What type of arithmetic instructions does the 8085 perform?

The 8085 performs four types of arithmetic instructions, as shown in the table below.

Here r -> register; M -> Memory (HL register pair); data -> direct data; rp -> any of the three register pairs.

Operation Operand size Instruction Description
Addition
8-bit
ADD r
[A] <- [A] + [R]
ADD M
[A] <- [A] + [[HL]]
ADI data
[A] <- [A] + data
8-bit with carry
ADC r
[A] <- [A] + [R] + [carry]
ADC M
[A] <- [A] + [[HL]] + [carry]
ACI data
[A] <- [A] + data + [carry]
8-bit decimal DAA
[A]+60 if Carry = 1
[A]+06 if AC = 1
16-bit DAD rp
[HL] <- [HL] + [rp]
Subtraction
8-bit
SUB r
[A] <- [A] – [R]
SUB M
[A] <- [A] – [[HL]]
SUI data
[A] <- [A] – data
8-bit with borrow
SBB r
[A] <- [A] – [R] – [carry]
SBB M
[A] <- [A] – [[HL]] – [carry]
SBI data
[A] <- [A] – data – [carry]
Increment
8-bit
INR r [R] <- [R] + 1
INR M
[[HL]] <- [[HL]] + 1
16-bit INX rp [rp] <- [rp] + 1
Decrement
8-bit
DCR r [R] <- [R] – 1
DCR M
[[HL]] <- [[HL]] – 1
16-bit DCX rp
[rp] <- [rp] – 1

Addition in 8085

  • Any 8-bit value, or the contents of any register, or even the contents of a memory location pointed by HL pair, can be summed up with the contents of the accumulator register.
  • The added value, i.e., the result is then stored in the accumulator register itself.
  • This function cannot be be used for two other registers together – the addition has to be performed with the accumulator being one of the addends.
  • Thus, for example, the contents of, say, register C cannot be summed up directly to the contents of register B.

ADD

The Opcode

The Operand

Description

ADD

R

Add register to the accumulator

[A] <- [A] + [R]

M

Add contents from the location pointed by the HL pair (M) to the contents of the accumulator

[A] <- [A] + [[HL]]

As discussed earlier, the contents of any register or memory location are added to the contents of the accumulator register. The corresponding sum is then saved in the accumulator. If the second addend is a memory, its address is specified by the H-L pair. All flags linked with the addition are updated with the result of the addition performed.

Size of instruction

1 byte

Addressing mode

ADD R – Register; ADD M – Indirect

Flags affected

All

Example

  • ADD B
  • ADD M

ADC

The Opcode

The Operand

Description

ADC

R

Add register to the accumulator with carry

[A] <- [A] + [R] + [carry]

M

Add the content of the location pointed by the value in the HL pair (M) to the accumulator with carry.

[A] <- [A] + [[HL]] + [carry]

The only difference between the ADD and the ADC operations is that the ADC condition considers the carry value as well. Thus, the Carry flag plays an important role and becomes the third addend.

Size of instruction

1 byte

Addressing mode

ADC R – Register; ADC M – Indirect

Flags affected

All

Example

  • ADC B
  • ADC M

ADI

The Opcode

The Operand

Description

ADI

8-bit immediate data

Add immediate value to the accumulator

[A] <- [A] + data

The contents of the accumulator register are added with immediate 8-bit data using the ADI instruction. The sum is thus stored in the accumulator. All flags are correspondingly modified.

Size of instruction

2 bytes

Addressing mode

Immediate

Flags affected

All

Example

  • ADI 10H

ACI

The Opcode

The Operand

Description

ACI

8-bit immediate data

Add immediate value to the accumulator with carry

[A] <- [A] + data + [carry]

ACI is similar to the ADC instruction. The immediate value of length 8-bit is added to the contents of the accumulator register along with the carry value. Thus, the Carry flag plays an important role and becomes the third addend.

Size of instruction

2 bytes

Addressing mode

Immediate

Flags affected

All

Example

  • ACI 70H

DAD

The Opcode

The Operand

Description

DAD

Register pair

Add contents of register pair to contents of H-L pair. The result is stored in the H-L pair.

[HL] <- [HL] + [rp]

The 16-bit contents of any 8085 register pair are added to the contents of the H-L register pair. The result is also stored in the H-L pair. The carry flag is also updated with the result of the addition operation. No other flag bits are affected.

Size of instruction

1 byte

Addressing mode

Register

Flags affected

Only carry flag is affected

Example

  • DAD B

Subtraction in 8085

  • Any 8-bit value, or the contents of any register, or even the contents of a memory location can be subtracted from the contents of the accumulator register.
  • The difference of this operation is stored in the accumulator.
  • The subtraction is performed in 2’s complement form. (You can revisit the concept of 2’s complement over here.)
  • Also, if the result of the subtraction is negative, it is stored in its 2’s complement form.
  • Similar to the addition operation, no two 8-bit registers can be subtracted directly, unless it is subtracted from the accumulator.

SUB

The Opcode

The Operand

Description

SUB

R

Subtract register from the accumulator

[A] <- [A] – [R]

([A] – [R] = [A] + 2’s complement of [R])

M

Subtract contents of the location pointed by HL pair (M) from the accumulator

[A] <- [A] – [[HL]]

The contents of any register or memory location are subtracted from the contents of the accumulator register. The corresponding difference is then saved in the accumulator register. If the operand is a memory, its address is specified by the H-L pair. All flags linked with the subtraction operation are updated with the result of the operation performed.

Size of instruction

1 byte

Addressing mode

SUB R <- Register; SUB M <- Indirect

Flags affected

All

Example

  • SUB B
  • SUB M

SBB

The Opcode

The Operand

Description

SBB

R

Subtract register from the accumulator with borrow considered

[A] <- [A] – [R] – [carry]

M

Subtract the contents of the location pointed by the HL pair (M) from the accumulator with borrow considered

[A] <- [A] – [[HL]] – [carry]

The contents of any register or memory location AND the borrow value (the CY flag in this case) are subtracted from the contents of the accumulator register. The corresponding difference is saved in the accumulator register. If the operand is a memory, its address is specified by the H-L pair. All flags linked with the subtraction operation are updated with the result of the operation performed.

Size of instruction

1 byte

Addressing mode

SBB R <- Register; SBB M <- Indirect

Flags affected

All

Example

  • SBB B
  • SBB M

SUI

The Opcode

The Operand

Description

SUI

8-bit immediate data

Subtract an immediate value from the accumulator

[A] <- [A] – data

The contents of the accumulator register are subtracted with immediate 8-bit data using the SUI instruction. The answer is thus stored in the accumulator. All flags are correspondingly modified to reflect the difference.

Size of instruction

2 byte

Addressing mode

Immediate

Flags affected

All

Example

  • SUI 45H

SBI

The Opcode

The Operand

Description

SBI

8-bit immediate data

Subtract an immediate value from the accumulator with Borrow considered

[A] <- [A] – data – [carry]

The contents of the accumulator register are subtracted with immediate 8-bit data AND the borrow value (specified by the CY flag) using the SBI instruction. The answer is thus stored in the accumulator. All flags are correspondingly modified to reflect the difference.

Size of instruction

2 byte

Addressing mode

Immediate

Flags affected

All

Example

  • SBI 76H

Increment or Decrement in 8085

  • Every register or memory location holds contents of size 8-bits. These contents can be incremented and/or decremented by a value of 1.
  • If we consider register pairs, then the 16-bit contents can also be incremented or decremented by a value of 1.

INR

The Opcode

The Operand

Description

INR

R

Increment Register contents by one

[R] <- [R] + 1

M

Increment the content at the memory location pointed by the HL pair by one

[[HL]] <- [[HL]] + 1

The INR command is used to increment/increase the contents of a register or of the data at the location pointed by HL pair(M) by one. The result is stored in the same location itself. In the case of the operand being M, the value at the memory location given by the HL pair is incremented by one. The value in the HL register pair does not change.

Size of instruction

1 byte

Addressing mode

INR R<- Register; INR M <- Indirect

Flags affected

All except the carry flag

Example

  • INR B
  • INR M // If M=7500H and value at 7500H =03H then after execution HL/M=7500H and value at 7500H = 04H.

INX

The Opcode

The Operand

Description

INX

R

Increment contents of register pair by one

[rp] <- [rp] + 1

The INX instruction increments the contents of a register-pair by the value of one. The result is stored in the same place itself.

Size of instruction

1 byte

Addressing mode

Register

Flags affected

None

Example

  • INX B

DCR

The Opcode

The Operand

Description

DCR

R

Decrement Register contents by one

[R] <- [R] – 1

M

Decrement the content at the memory location pointed by the HL pair by one

[[HL]] <- [[HL]] – 1

The DCR command is used to decrement/decrease the contents of a register or of a memory location by the value of one. The result after this operation is stored in the same location itself. If the operand happens to be a memory location, its address is then specified by the contents of the H-L pair.

Size of instruction

1 byte

Addressing mode

DCR R<- Register; DCR M <- Indirect

Flags affected

All except the carry flag

Example

  • DCR B
  • DCR M

DCX

The Opcode

The Operand

Description

DCX

R

Decrement register pair by one

[rp] <- [rp] – 1

The DCX instruction decrements the contents of the register-pair by the value of one. The result is stored in the same place itself.

Size of instruction

1 byte

Addressing mode

Register

Flags affected

None

  • DCX H

Simulation using the Sim8085 Emulator

All the example codes in this 8085 course have been executed in an online development environment called Sim8085. It is a simple environment that is really user-friendly for beginners. You can write codes for the Intel 8085 microprocessor, debug the assembly code, and then simulate the 8085 microprocessor. We’ve covered a tutorial on using the Sim8085 emulator in the data transfer instructions post here. You can check out the Sim8085 emulator here

Let us walk through the simulator with our code.

Sim8085 - Main Page
Sim8085 – Main Page

Assembly Language Programming Using Arithmetic Transfer Instructions

We shall now see an Assembly Language Program using these instructions and see how the arithmetic instructions are put to use. Consider the given problem statement.

Let us write a program to perform the multiplication of two numbers using the 8085 Arithmetic Instructions.

We know that multiplication is nothing but repeated addition. Thus, we shall write this ALP using the technique of repeated addition.

MVI A, 00H
MVI B, 07H
MVI C, 04H
LOOP : ADD B
DCR C
JNZ LOOP
HLT
Output of Program 1
The output of Program 1

Let us go through the step by step analysis of our program.

  1. MVI A, 00H

    We initially clear the contents of the accumulator register.

  2. MVI B, 07H

    Our aim is to multiply 7 with 4. This can be achieved by adding 7 to itself four times. Thus, we set the register B as the value 7.

  3. MVI C, 04H

    The register C holds the value of the counter, i.e., the number of times this addition is to be repeated.

  4. LOOP : ADD B

    We begin a loop with the label LOOP. As we have already seen, the result of the addition operation is stored in the Accumulator Register. Thus, after running the program each time, the register A (Accumulator) has 7 added to itself.

  5. DCR C

    Once the addition is carried out, we decrement the value of register C using the DCR condition.

  6. JNZ LOOP

    This loop goes on until the content of register C is 0 (which is checked by the JNZ condition, which jumps backs to LOOP if C is not 0).

  7. HLT

    Once all the steps of the program are complete, we halt/put an end to the program by using the HLT condition.

Finally, we see that the value of the register A (Accumulator) is 1C, which is 28 in Hexadecimal.

We hope this post explained how an arithmetic instruction executes and what happens in the background when it does. Let us know in the comments section below if you’d like us to add examples for each instruction.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.