Programs for 16 bit arithmetic operations for 8086 (using various addressing modes)

Topic Content.

1. Addition of two 16-bit numbers

Aim:

To write an assembly language program to perform addition of two 16-bit signed  and unsigned numbers.

Tools:

PC installed with TASM.

Program:

ASSUME CS : CODE, DS : DATA
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV AX, OPR1
ADD AX, OPR2
MOV RES, AX
HLT
CODE ENDS
DATA SEGMENT
OPR1 DW 4269H
OPR2 DW 1000H
RES DW ?
DATA ENDS
END

List file:

ADDRESS OPCODE OPERATION COMMENTS
0000 B88E4E MOV AX,4E8E INITIALIZATION OF
 DATA SEGMENT
 
0003 8ED8 MOV DS,AX
0005 A10000 MOV AX,[0000] THE VALUE IN [0000] IS MOVED
 TO AX
0008 03060200 ADD AX,[0002] THE VALUE IN [0002] IS ADDED
   TO AX
000C A30400 MOV [0004],AX THE VALUE IN AX IS MOVED
 TO [0004]
000F F4 HLT END OF PROGRAM

Flow Chart:

Result:

UNSIGNED NUMBERS

Flags:

Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=0, s=0, z=0, o=0, p=1, a=0, i=1, d=0.

Input:

OPR1 = 4269H
OPR2 = 1000H

Output:

RES = 5269H

SIGNED NUMBERS

Flags:

Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=1, s=0, z=0, o=1, p=0, a=0, i=1, d=0.

Input:

OPR1 = 9763H
OPR2 = A973H

Output:

RES = 40D6H

 

2. Subtraction of two 16-bit numbers

Aim:

To write an assembly language program to perform subtraction of two 16-bit  signed and unsigned numbers.

Tools:

PC installed with TASM.

Program:

ASSUME CS : CODE, DS : DATA
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV AX, OPR1
MOV BX, OPR2
SUB AX, BX
MOV RES, AX
HLT
CODE ENDS
DATA SEGMENT
OPR1 DW 4269H
OPR2 DW 1000H
RES DW ?
DATA ENDS
END

List file:

ADDRESS OPCODE OPERATION COMMENTS
0000 B88E4E MOV AX,4E8E INITIALIZATION OF
 DATA SEGMENT
0003 8ED8 MOV DS,AX
0005 A10000 MOV AX,[0000] THE VALUE IN [0000] IS MOVED
 TO AX
0008 2B060200 ADD AX,[0002] THE VALUE IN [0002] IS SUBTRACTED 
 FROM AX
000C A30400 MOV [0004],AX THE VALUE IN AX IS MOVED
 TO [0004]
000F F4 HLT END OF PROGRAM
 

Flow Chart:

Result:

UNSIGNED NUMBERS

 Flags: 

Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=0, s=0, z=0, o=0, p=1, a=0, i=1, d=0.

Input:

OPR1 = 4269H
OPR2 = 1000H

Output:

RES = 3269H

SIGNED NUMBERS

Flags:

Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=0, s=0, z=0, o=0, p=1, a=0, i=1, d=0.

Input:

OPR1 = 9763H
OPR2 = 8973H

Output:

RES = 0DF0H

 

3. Multiplication of two 16-bit unsigned numbers

Aim:

To write an assembly language program to perform multiplication of two 16-bit  unsigned numbers.

Tools:

PC installed with TASM.

Program:

ASSUME CS : CODE, DS : DATA
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV AX, OPR1
MUL OPR2
MOV RESLW, AX
MOV RESHW, DX
HLT
CODE ENDS
DATA SEGMENT
OPR1 DW 2000H
OPR2  DW 4000H
RESLW  DW ?
RESHW  DW ?
DATA ENDS
END

List file:

ADDRESS OPCODE OPERATION COMMENTS
0000 B88E4E MOV AX,4E8E INITIALIZATION OF
 DATA SEGMENT
 
0003 8ED8 MOV DS,AX
0005 A10000 MOV AX,[0000] THE VALUE IN [0000] IS MOVED
 TO AX
0008 F7260200 IMULWORD PTR [0002] THE VALUE IN [0002] IS MULTIPLIED
 BY AX
000C A30400 MOV [0004],AX THE VALUE IN AX IS MOVED
 TO [0004]
000F 89160600 MOV [0006],DX THE VALUE IN DX IS MOVED 
TO [0006]
0013 F4 HLT END OF PROGRAM

Flow Chart:

Result:

Flags:

Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=1, s=0, z=0, o=1, p=0, a=0, i=1, d=0.

Input:

OPR1 = 2000H
OPR2 = 4000H

Output:

RESLW = 0000H (AX)
RESHW = 0800H (DX)

 

4. Multiplication of two 16-bit signed numbers.

Aim:

To write an assembly language program to perform multiplication of two 16-bit  signed numbers.

Tools:

PC installed with TASM.

Program:

ASSUME CS : CODE, DS : DATA
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV AX, OPR1
IMUL OPR2
MOV RESLW, AX
MOV RESHW, DX
HLT
CODE ENDS
DATA SEGMENT
OPR1 DW 7593H
OPR2 DW 6845H
RESLW DW ?
RESHW DW ?
DATA ENDS
END

List File:

ADDRESS OPCODE OPERATION COMMENTS
0000 B88E4E MOV AX,4E8E INITIALIZATION OF
 DATA SEGMENT
 
0003 8ED8 MOV DS,AX
0005 A10000 MOV AX,[0000] THE VALUE IN [0000] IS MOVED
 TO AX
0008 F72E0200 IMULWORD PTR [0002] THE VALUE IN [0002] IS ADDED
 TO AX
000C A30400 MOV [0004],AX THE VALUE IN AX IS MOVED
 TO [0004]
000F 89160600 MOV [0006],DX THE VALUE IN DX IS MOVED 
TO [0006]
0013 F4 HLT END OF PROGRAM
 

Flow Chart:

Result:

Case 1: Two positive numbers

Flags:

Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=1, s=0, z=0, o=1, p=0, a=0, i=1, d=0.

Input:

OPR1 = 7593H
OPR2 = 6845H

Output:

RESLW = 689FH (AX)
RESHW = 2FE3H (DX)

Case 2: one positive number & one negative number

Flags:

Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=1, s=0, z=0, o=1, p=0, a=0, i=1, d=0.

Input:

OPR1 = 8A6DH <- 2’s Complement of (-7593H)
OPR2 = 6845H

Output:

RESLW = 9761H (AX) <- 2’s Complement 
RESHW = D01CH (DX) <- of (-2FE3689FH)

Case 3: two negative numbers

Flags:

Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=1, s=0, z=0, o=1, p=0, a=0, i=1, d=0.

Input:

OPR1 =  8A6DH <- 2’s Complement of (-7593H)
OPR2 = 97BBH  <- 2’s Complement of (-6845H)

Output:

RESLW = 689FH (AX)
RESHW = 2FE3H (DX)

 

5. Division of unsigned numbers

Aim:

To write an assembly language program to perform division of 16-bit unsigned number by 8-bit unsigned number.

Tools:

PC installed with TASM.

Program:

ASSUME CS : CODE, DS : DATA
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV AX, OPR1
DIV OPR2
MOV RESQ, AL
MOV RESR, AH
HLT
CODE ENDS
DATA SEGMENT
OPR1 DW 2C58H
OPR2 DB 56H
RESQ DB ?
RESR DB ?
DATA ENDS
END

List file:

ADDRESS OPCODE OPERATION COMMENTS
0000 B88F4E MOV AX,4E8F INITIALIZATION OF
 DATA SEGMENT
 
0003 8ED8 MOV DS,AX
0005 A10000 MOV AX,[0000] THE VALUE IN [0000] IS MOVED
 TO AX
0008 F6360200 DIV BYTE PTR [0002] THE VALUE IN [0002] IS ADDED
 TO AX
000C A20300 MOV [0003],AX THE VALUE IN AL IS MOVED
 TO [0003]
000F 88260400 MOV [0004],DX THE VALUE IN AH IS MOVED 
TO [0004]
0013 F4 HLT END OF PROGRAM
 

Flow Chart:

Result:

Flags:

Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=0, s=0, z=0, o=0, p=0, a=1, i=1, d=0.

Input:

OPR1 = 2C58H (DIVIDEND)
OPR2 = 56H (DIVISOR)

Output:

RESQ = 84H (AL)
RESR = 00H (AH)

 

6. Division of signed numbers

Aim:

To write an assembly language program to perform division of 16-bit signed number by 8-bit signed number.

Tools:

PC installed with TASM.

Program:

ASSUME CS : CODE, DS : DATA
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV AX, OPR1
IDIV OPR2
MOV RESQ, AL
MOV RESR, AH
HLT
CODE ENDS
DATA SEGMENT
OPR1 DW 26F8H
OPR2 DB 0AAH
RESQ DW ?
RESR DW ?
DATA ENDS
END

List file:

ADDRESS OPCODE OPERATION COMMENTS
0000 B88F4E MOV AX,4E8F INITIALIZATION OF
 DATA SEGMENT
 
0003 8ED8 MOV DS,AX
0005 A10000 MOV AX,[0000] THE VALUE IN [0000] IS MOVED
 TO AX
0008 F63E0200 DIV BYTE PTR [0002] THE VALUE IN [0002] IS ADDED
 TO AX
000C A20300 MOV [0003],AX THE VALUE IN AL IS MOVED
 TO [0003]
000F 88260400 MOV [0004],DX THE VALUE IN AH IS MOVED 
TO [0004]
0013 F4 HLT END OF PROGRAM
 

Flow Chart:

Result:

Case 1:  two positive numbers

Flags:

Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=0, s=0, z=0, o=0, p=0, a=1, i=1, d=0.

Input:

OPR1 = 26F8H (DIVIDEND)
OPR2 = 56H (DIVISOR)

Output:

RESQ = 74H (AL)
RESR = 00H (AH)

Case 2: one positive number & one negative number

Flags:

Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=0, s=0, z=0, o=0, p=0, a=1, i=1, d=0.

Input:

OPR1 = D908H <- 2’s Complement of (-26F8H)
OPR2 = 56H

Output:

RESQ = 8CH (AL) <- 2’s Complement of (- 74H)
RESR = 00H (AH)

Case 3: one positive number & one negative number

Flags:

Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=0, s=0, z=0, o=0, p=0, a=1, i=1, d=0.

Input:

OPR1 =  26F8H
OPR2 = AAH <- 2’s Complement of (-56H)

Output:

RESQ = 8CH (AL)  <- 2’s Complement of (- 74H)
RESR = 00H (AH)

 

7. ASCII addition

Aim:

To write an ALP to perform the addition of two ASCII bytes.

Tools:

PC installed with TASM.

Program:

ASSUME CS : CODE, DS : DATA
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV AH, 00H
MOV AL, CHAR
ADD AL, CHAR1
AAA
MOV RES, AX
HLT
CODE ENDS
DATA SEGMENT
CHAR DB '8'
CHAR1 DB '6'
RES DW ?
DATA ENDS
END

List file:

ADDRESS OPCODE OPERATION COMMENTS
0000 B88E4E MOV AX,4E8E INITIALIZATION OF
 DATA SEGMENT
 
0003 8ED8 MOV DS,AX
0005 B400 MOV AH,00 00H IS STORED IN AH
0007 A00000 MOV AL,[0000] THE VALUE IN [0000] IS MOVED
 TO AL
000A 02060100 ADD AL,[0001] THE VALUE IN [0001] IS ADDED 
TO AL
000E 37 AAA ASCII ADJUST AFTER
 ADDITION
000F A30200 MOV [0002],AX THE VALUE IN AX IS
 MOVED TO [0002]
0012 F4 HLT END OF PROGRAM
 

Flow Chart:

Result:

Flags:

Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=1, s=0, z=0, o=0, p=1, a=1, i=1, d=0.

Input:

CHAR = '8'
CHAR1 = '6'

Output:

RES = 0104 (AX)  <- unpacked BCD of 14

 

8. ASCII substraction

Aim:

To write an ALP to perform the subtraction of two ASCII bytes.

Tools:

PC installed with TASM.

Program:

ASSUME CS : CODE, DS : DATA
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV AH, 00H
MOV AL, CHAR
SUB AL, CHAR1
AAS
MOV RES, AX
HLT
CODE ENDS
DATA SEGMENT
CHAR DB '9'
CHAR1 DB '5'
RES DW ?
DATA ENDS
END

List file:

ADDRESS OPCODE OPERATION COMMENTS
0000 B88E4E MOV AX,4E8F INITIALIZATION OF
 DATA SEGMENT
 
0003 8ED8 MOV DS,AX
0005 B400 MOV AH,00 00H IS STORED IN AH
0007 A00000 MOV AL,[0000] THE VALUE IN [0000] IS MOVED
 TO AL
000A 2A060100 SUB AL,[0001] THE VALUE IN [0001] IS 
SUBTRACTED FROM AL
000E 3F AAS ASCII ADJUST AFTER
 SUBTRACTION
000F A30200 MOV [0002],AX THE VALUE IN AX IS
 MOVED TO [0002]
0012 F4 HLT END OF PROGRAM
 

Flow Chat:

Result:

Case 1: 

Flags:

Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.

Input:

CHAR  = ‘9’
CHAR1 = ‘5’

Output:

RES = 0004 (AX)

Case 2:

Flags:

Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=1, s=1, z=0, o=0, p=1, a=1, i=1, d=0.

Input:

CHAR  = ‘5’
CHAR1 = ‘9’

Output:

RES = FF06 (AX) <- 2’s Complement of (-4)

 

9. ASCII multiplication

Aim:

To write an ALP to perform the multiplication of two ASCII bytes.

Tools:

PC installed with TASM.

Program:

ASSUME CS : CODE, DS : DATA
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV AH, 00
MOV AL, NUM1
MUL NUM2
AAM
MOV RES, AX
HLT
CODE ENDS
DATA SEGMENT
NUM1 DB 09
NUM2 DB 05
RES DW ?
DATA ENDS
END

List file:

ADDRESS OPCODE OPERATION COMMENTS
0000 B88E4E MOV AX,4E8F INITIALIZATION OF
 DATA SEGMENT
 
0003 8ED8 MOV DS,AX
0005 B400 MOV AH,00 00H IS STORED IN 
AH
0007 A00000 MOV AL,[0000] THE VALUE IN [0000] IS MOVED
 TO AL
000A F6260100 MUL BYTE PTR [0001] THE VALUE IN [0001] IS 
MULTIPLIED BY AL
000E D40A AAM ASCII ADJUST AFTER 
MULTIPLCATION
000F A30200 MOV [0002],AX THE VALUE IN AX IS
 MOVED TO [0002]
0012 F4 HLT END OF PROGRAM
 

Flow Chart:

Result:

Flags:

Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=0, s=0, z=0, o=0, p=1, a=0, i=1, d=0.

Input:

NUM1  =  09
NUM2 =  05

Output:

RES = 0405 (AX)  <- unpacked BCD of 45

 

10. ASCII division

Aim:

To write an ALP to perform the division of two ASCII numbers.

Tools:

PC installed with TASM.

Program:

ASSUME CS : CODE, DS : DATA
CODE SEGMENT
MOV AX, DATA
MOV DS, AX
MOV AX, DIVIDEND
AAD
MOV CH, DIVISOR
DIV CH
MOV RESQ, AL
MOV RESR, AH
HLT
CODE ENDS
DATA SEGMENT
DIVIDEND DW 0607H
DIVISOR DB 09H
RESQ DB ?
RESR DB ?
DATA ENDS
END

List file:

ADDRESS OPCODE OPERATION COMMENTS
0000 B88F4E MOV AX,4E8F INITIALIZATION OF
 DATA SEGMENT
 
0003 8ED8 MOV DS,AX
0005 A10000 MOV AH,[0000] THE VALUE IN [0000] IS 
MOVED TO AX
0008 D50A AAD ASCII ADJUST FOR 
DIVIDION
000A 8A2E0200 MOV CH,[0002] THE VALUE IN [0002] IS
 MOVED TO CH
000E F6F5 DIV CH AX IS DIVIDED
 BY CH
0010 A20300 MOV [0003],AL THE VALUE IN AL IS
 MOVED TO [0003]
0013 88260400 MOV [0002],AH THE VALYE IN AH IS
 MOVED TO [0004]
0017 F4 HLT END OF
 PROGRAM

Flow Chart:

Result:

Flags:

Before execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.
After execution, c=0, s=0, z=0, o=0, p=0, a=0, i=1, d=0.

Input:

DIVIDEND = 0607H <- unpacked BCD of 67
DIVISOR = 09H

Output:

RESQ = 07 (AL)
RESR = 04 (AH)