8086 Integer Arithmetic Instructions – Assembly Language Programming

In this article, we will discuss integer arithmetic instructions of 8086 and we will see assembly language examples of 8086 arithmetic instructions.  Arithmetic instructions are those instructions that perform the arithmetic operations of addition, subtraction, multiplication, and division.

Prerequisites: 8086 Microprocessor Addressing , 8086 Data Transfer Instructions

8086 Integer Addition Instructions

8086 microprocessor supports following types of addition instructions.

  1. ADD
  2. ADC
  3. INC
  4. AAA
  5. DAA

Now lets discuss the details of Addition instructions with assembly code examples.

8086 ADD Instruction

This instruction adds the data of destination and source operand and stores the result in destination. Both operands should be of same type i.e. words or bytes otherwise assembler will generate an error. It supports following operands:

SourceDestinationExample
RegisterRegisterADD AX, BX
RegisterImmediateADD CL, 2DH
RegisterMemory AddressADD [204CH], AL ADD [BP][SI]+23, DX
Memory AddressRegisterADD CL, [BX+15]

If the sum of two operands exceeds the size of destination operand, then it would set the carry flag to 1.  The ADD instruction can affect AF, CF, OF, PF, SF, ZF flags depending upon the result. If the result is zero, the ZF=1. Negative result sets SF to 1.

Example Assembly Code

ORG 100h   
.MODEL SMALL
.CODE           
MOV AL, 10H     ;Sets AL to 10H
MOV BH, 75H     ;Sets BX to 23H
ADD AL, BH      ;Store the sum of AL and BX in AL
MOV CX, 0F21Ah  ;Set CX to 0F21Ah
ADD [0154H], CX ;Store sum of CX data and data at memory address DS:0154 into the same memory address
MOV AH, 9FH     ;Sets AH to 9FH
ADD AH,BH       ;Store sum of AH and BH into BH
RET             ;stops the program
                                         

Output:

8086 airthmetic addition instruction without carry

8086 ADC Instruction

The ADC and ADD instruction perform the same operation of addition. The only difference is that ADC instruction also adds the carry flag bit to the sum of two operands.

Example Assembly Code

ORG 100h   
.MODEL SMALL
.CODE           
MOV AL, 7DH     ;Sets AL to 7DH
MOV BH, 0F5H     ;Sets BH to F5H
ADC AL, BH      ;Store the sum of AL and BH in AL
MOV CX, 0F21Ah  ;Set CX to 0F21Ah
MOV [0154H], CX ;CX=CX+ DS:0154H 
ADC AL, [0154H] ;AL=AL+ DS:0154H
RET             ;stops the program
                                          

OUTPUT

The instruction ADC AL, BH sets AL to 7D + F5= 172. The value 72H goes to AL register and the overflow bit produced sets carry flag to 1. Now when the next ADC instruction executes, it will generate sum of data from memory location 07154H, data from AL register and carry flag bit and stores the result in AL.

8086 airthmetic addition instruction with carry input

Increment Instruction

It is an increment instruction which takes only one operand. The INC instruction adds 1 to the contents of destination operand. It can affect AF, OF, PF, SF and ZF flags.

Example Assembly Code

ORG 100h   
.MODEL SMALL
.CODE           
MOV AL, 7DH     ;Sets AL to 7DH
INC AL          ;AL=AL+1
RET             ;stops the program
                                        

Output

In this example, AL is loaded with hexadecimal value of 7DH. The increment instruction adds 1 to the value inside AL and then store final result in the same AL register.

8086 airthmetic increment instruction

8086 AAA (Adjust after addition) Instruction

Suppose you want to add two decimal digits represented in ASCII code. Before the addition, you need to mask upper nibble (3) from the code. The AAA (Adjust after addition) instruction allows the addition operation without masking off the “3” in the upper nibble of each digit. It makes sure whether the final result stored is in the form of unpacked BCD or not.  The AAA instruction does not require any operand and operates on the AL register that’s why the final sum of two ASCII codes should be in the AL register.

It checks the AL register and then take following actions:

1. If lower nibble of AL is between 0 to 9:

  • AF =0 and CF= 0
  • Four higher order bits of AL sets to 0
  • AH is cleared to 0

2. If lower nibble of AL is greater than 9:

  • AF=1, CF=1
  • Add 6 to AL
  • Clears four higher order bits of AL
  • Add 1 to contents of AH

Example Assembly Code

ORG 100h   
.MODEL SMALL
.CODE           
MOV AL, 032H     ;Sets AL to 32H
AAA          
RET              ;stops the program
                                          

OUTPUT

In this case, the lower nibble of AL is 3 which is less than 9. Therefore, AF and CF are set to 0. The first four bits of AL are also 0.

8086 airthmetic adjust after addition instruction

Example Assembly Code

ORG 100h   
.MODEL SMALL
.CODE           
MOV AL, 5DH     ;Sets AL to 32H
AAA          
RET              ;stops the program
                                         

Output

8086 airthmetic adjust after addition instruction example 2

Now, AS the lower nibble of AL is D which is greater than 9. Therefore, the first four bits of AL are cleared to 0. 6 is added to AL and 1 is added to AH. Flags AF and CF are set to 1.

DAA (Decimal Adjust Accumulator) Instruction

The DAA (Decimal Adjust Accumulator) instruction also works on the data of AL register. This instruction does not need any operand.  This instruction is used to convert the sum of two packed BCD numbers into a valid BCD number.

It checks the AL data and performs the following operations:

1. If lower nibble of AL > 9 or AF=1 then:

  • Add 6 to lower byte of AL
  • Set AF=1

2. If AL> 9Fh or CF = 1 then:

  • Add 60h to AL
  • Set CF = 1

Example Assembly Code

ORG 100h   
.MODEL SMALL
.CODE           
MOV AL, 5DH     ;Sets AL to 5DH 
ADD AL, 25H     ;AL=5DH+25H
DAA          
RET             ;stops the program
                                         

Output

8086 airthmetic DAA instruction

8086 Integer Subtraction Instructions

8086 microprocessor supports the following subtraction Instructions:

  1. SUB
  2. SBB
  3. DEC
  4. AAS
  5. DAS

Subtraction Instruction without Carry

Subtraction instruction takes two operands. Subtract the data in the source operand from the data of destination operand and then store the result back to the destination operand. Just like ADD instruction, both operands should be either in bytes or words. If one operand is in a byte and the other in words then insert two zero’s at the start of bytes. But they should be of the same type. The subtraction instruction supports the following operands:

SourceDestinationExample
RegisterRegisterSUB CH, AL
RegisterImmediateSUB AX, 16H
RegisterMemory AddressSUB [SI], AX SUB 19[BP][DI], CX
Memory AddressRegisterSUB AL, 10[CX]

Example Assembly Code

ORG 100h
MOV AX, 2506    ;Sets AX to 2506
MOV BX, 1647    ;Sets BX to 1647
SUB AX, BX      ;AX=AX-BX  
SUB [SI], AX    ;DS:SI=DS:SI-AX
RET             ;Stop the program

Output

The SUB AX, BX instruction subtracts BX from AX and store the difference in AX which is:

AX = 9CA – 66F = 35B. In the next instruction, SI is loaded with 0700H. The 6th instruction SUB [SI], AX subtracts AX from the data stored at memory location DS:SI and stores the difference at the same memory address.

8086 airthmetic subtraction instruction without carry

The SUB instruction can affect AF, CF, OF, PF, SF and ZE flags depending upon the result obtained from difference.

8086 SBB Subtraction Instruction

The SBB instruction not only subtract the data of source from destination’s data but it also subtracts the carry flag bit from their result and then store the result in Destination operand.

The flags AF, CF, OF, PF, SF and ZF can be affected from this instruction.  

Example Assembly Code

ORG 100h
MOV AX, 2506    ;Sets AX to 2506
MOV BX, 1647    ;Sets BX to 1647
SBB AX, BX      ;AX=AX-BX 
MOV SI, 0700H   ;Set SI to 0700H
SBB [SI], AX    ;DS:SI=DS:SI-AX
RET             ;Stop the program

Output

Here, the subtraction of 0047 from 2506 sets the borrow flag (CF) to 1. Therefore, the subtraction with borrow (SBB) instruction sets BX equal to the difference of:

BX = 2FH – 9CAH – 1 = F665H

8086 airthmetic subtraction instruction with carry

Decrement Instruction

The DEC instruction subtracts 1 from the destination operand and loads the result back into the same destination.

Example Assembly Code

ORG 100h 
.MODEL SMALL
.CODE
MOV AX, 25    ;Sets AX to 25
DEC           ;AX=AX-1   
RET           ;Stops the program

Output

8086 airthmetic decrement instruction

8086 AAS Instruction

In ASCII code subtraction of two decimal digits, we need to mask the “011”or 3 in upper nibbles to obtain result in a unpacked BCD form. The Adjust after Subtraction instruction (AAS) provides the correct unpacked BCD result without masking the “3”. The AAS instruction checks the conent of AL register. That’s why before calling AAS instruction, move the result of difference into AL register.

The AAS instruction checks the content of AL register and perform following operation:

If D3-D0 > 9 or AF = 1 then

  • subtract 6 from AL and 01 from AH
  • flags AF and CF are set to 1
  • clear the high-order four bits of AL.

If D3-D0 < 9 then:

  • set AF and CF to 0
  • clear the high-order four bits of AL.

Example Assembly Code

ORG 100h 
.MODEL SMALL
.CODE
MOV AX, 00F9H  ;Sets AX to 00F9H  
MOV BX, 1152H  ;Sets BX to 0022H
SUB AX, BX     ;Compute AX-BX
DAS  
RET            ;Stops the program

Output

8086 airthmetic adjust after subtraction instruction

8086 DAS Instruction

This is same as AAS. But it is used to convert the difference of two packed BCD numbers into a packed BCD result. The instruction operates on AL content and it does not require any operand.

The DAS instruction checks the low and high our order bits of the AL register and perform the following operation:

  • If D3-D0 > 9 then set AF flag to 1 and subtract 6 from these four bits.
  • If D7-D4 > 9 then set CF flag to 1 and subtracts 6 from these four bits.
  • If both D3-D0 and D7-D4 are greater than 9 then subtract 6 from both bytes and set AF=1, CF=1.

Example Assembly Code

ORG 100h 
.MODEL SMALL
.CODE
MOV AX, 02FCH  ;Sets AX to 02FCH  
MOV BX, 1152H  ;Sets BX to 1152H
SUB AX, BX     ;Compute AX-BX
DAS  
RET            ;Stops the program

Output

8086 airthmetic DAS instruction

Leave a Comment

seven − 6 =