0% found this document useful (0 votes)
35 views

Lab3 Simple Assignment and Arithmetic Operations Addition, Subtraction

The ADD instruction adds the data of the destination and source operands and stores the result in the destination. It supports adding registers, registers with immediates, registers with memory addresses, and memory addresses with registers. The ADC instruction performs addition with carry, adding the carry flag bit to the sum. The INC instruction increments the destination operand by 1. The SBB instruction subtracts source from destination and subtracts the carry flag bit from the result. The AAS and DAS instructions are used to obtain the correct unpacked or packed BCD result after subtraction of two decimal digits or numbers.

Uploaded by

Olana Teressa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Lab3 Simple Assignment and Arithmetic Operations Addition, Subtraction

The ADD instruction adds the data of the destination and source operands and stores the result in the destination. It supports adding registers, registers with immediates, registers with memory addresses, and memory addresses with registers. The ADC instruction performs addition with carry, adding the carry flag bit to the sum. The INC instruction increments the destination operand by 1. The SBB instruction subtracts source from destination and subtracts the carry flag bit from the result. The AAS and DAS instructions are used to obtain the correct unpacked or packed BCD result after subtraction of two decimal digits or numbers.

Uploaded by

Olana Teressa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Arithmetic and logic instructions

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:

Source Destination Example

Register Register ADD AX, BX

Register Immediate ADD CL, 2DH

Register Memory Address ADD [204CH], AL ADD [BP][SI]+23, DX

Memory Address Register ADD 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
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.

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 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

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 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

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

You might also like