You are on page 1of 3

Middle Technical University Digital Controller Lab.

Electrical Engineering Technical College Third Stage


Electrical Power Technical Engineering Dept. Mohammed D. Altamemi

Experiment - 5
Subtract of two (8-bit, and 16-bit) Numbers Using 8086 Microprocessor
5-1 Object:
Write an assembly language program to perform 8 bit, 16-bit subtraction, by using
different addressing modes and understand the method of subtraction in 8086
5-2 Theory:

SUB – SUB Destination, Source


SBB – SBB Destination, Source
These instructions subtract the number in some source from the number in some
destination and put the result in the destination. The SBB instruction also subtracts the
content of carry flag from the destination. The source may be an immediate number, a
register or memory location. The destination can also be a register or a memory location.
However, the source and the destination cannot both be memory location. The source and
the destination must both be of the same type (bytes or words). If you want to subtract a
byte from a word, you must first move the byte to a word location such as a 16-bit register
and fill the upper byte of the word with 0’s. Flags affected: AF, CF, OF, PF, SF, ZF.
Example:
SUB CX, BX [CX – BX; Result in CX]
SBB CH, AL [CH – AL – CF; Result in CH]

DEC – DEC Destination


This instruction subtracts 1 from the destination word or byte. The destination can be
a register or a memory location. AF, OF, SF, PF, and ZF are updated, but CF is not affected.
This means that if an 8-bit destination containing 00H or a 16-bit destination containing
0000H is decremented, the result will be FFH or FFFFH with no carry (borrow).
Example:
DEC CL [Subtract 1 from content of CL register]
DEC BYTE PTR [BX] [Subtract 1 from byte at offset [BX] in DS].
DEC WORD PTR [BP] [Subtract 1 from a word at offset [BP] in SS].
DEC COUNT [Subtract 1 from byte or word named COUNT in DS].
Decrement a byte if COUNT is declared with a DB;
Decrement a word if COUNT is declared with a DW.
5-3 Procedures:
Part I: subtract two 8-bit numbers
1. Start the program by loading the first data into AL.
Middle Technical University Digital Controller Lab.
Electrical Engineering Technical College Third Stage
Electrical Power Technical Engineering Dept. Mohammed D. Altamemi

2. Move the second data to a register BL.


3. Subtract the two register contents.
4. Check for carry.
5. If carry is present take 2’s complement of AL.
6. Store the difference value (present in AL) to [4152h]
7. Store the value of borrow in memory location [4153h].
8. Decrement AL by 1
9. Store AL to [4154h]
10. Terminate the program.
MOV CL, 00H ;Initialize C to 00
MOV AL, 50H ;Load 50h to AL .
MOV BL, 40H ;load 40H to BL .
SUB AL, BL ;Subtract the value of BL from AL
JNC LOOP ;Jump on no carry.
NEG AL ;Convert AL contents to 2's comp. .
INC CL ;Increment value in CL
LOOP: MOV [4152H], AL ;Store AL to memory address.
MOV [4153H], CL ;Move CL to memory address.
DEC AL ;decrement AL by 1.
MOV [4154H], AL ; move AL to memory address.
HLT ;Terminate the program.
PART II: subtract two 16-bit numbers
1. Start the program by loading the first data into AX.
2. Move the second data to a register BX.
3. Subtract the two register contents.
4. Check for carry.
5. If carry is present take 2’s complement of AX.
6. Store the difference value (present in Ax) to [5900h]
7. Store the value of borrow in memory location [5902h].
8. Decrement AL by 1
9. Store AL to [5903h]
10. Terminate the program.
MOV CL,00H ;Move immediate 00 value to Cl.
MOV AX, 0510H ;Load AX with immediate value.
Middle Technical University Digital Controller Lab.
Electrical Engineering Technical College Third Stage
Electrical Power Technical Engineering Dept. Mohammed D. Altamemi

MOV BX, 7264H ;Load BX with immediate value.


SUB AX, BX ;Subtract BX from AX
JNC LOOP ;jump on no carry
NEG AX ; convert AX to 2’s complement
INC CL ;increment CL by 1
LOOP: MOV [5900H], AX ;Store AX in address
MOV [5902H],CL ;Store CL in address
DEC AX ;decrement AX by 1
MOV [5903H], AX ;Store AX in address
HLT ;End program
5-4 Discussions:
1. Write a program execute the following subtraction operations, test CF, [if CF = 1 convert
the result to 2’s complement], and then storage the result and CF in memory location.
A. 65h - 82h
B. A150h - 2000h
C. C4h + 8812h
2. For A, B and C in step one each time after pressing the “single step” button, check
and record down the contents of AX, BX, and CF.
3. For A, B, and C in step one, use a calculator, calculate the answer for the above
arithmetic operations ………………………, Is it the same as the final answer in
the AX register? ……………………… .
4. Please express what you have learned with this laboratory work, by no more than
one paragraph.