Lab3 Simple Assignment and Arithmetic Operations Addition, Subtraction
Lab3 Simple Assignment and Arithmetic Operations Addition, Subtraction
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:
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.
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.
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.
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.
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:
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:
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
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