0% found this document useful (0 votes)
10 views71 pages

Unit 2 FMPMCCSD

Uploaded by

Sasii Thadii
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views71 pages

Unit 2 FMPMCCSD

Uploaded by

Sasii Thadii
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 71

Chapter 2 - 8086 family assembly language

programming - introduction
from Microprocessors and Interfacing by Douglas Hall
Outline
• Program Development Steps • Writing programs for the use
with assemblers
• Representing program operations
• Program format
• Finding the right instructions
• Assembler Directives
• Writing a program
• Types of numbers used in data
• Constructing the machine codes for 8086 statements
instructions
• Assembly Language Program
• Instruction templates development Tools
• MOD and R/M Bit patterns in 8086
• Tips for hand coding
Program Development Steps

• The problem that you want to solve

• Just ask yourself question that “What do I really want this program
to do?”
Representing program operations

• Formula or sequence of operations used to solve a programming


problem is called as the algorithm.
• There are two ways of representing algorithms:
• Flowchart
• Structured programming and pseudo code
Flowchart

• It uses graphical shapes to represent different types of programming


operations.
• Flow chart symbols
Structured programming and pseudo code
• Programs should be understandable to other programmers
• Two approaches top-down and bottom up approach
• Top-down: divide the program in to major modules and divide theses major
modules in to sub modules and so on.
• Bottom-up: each programmer writes the low level module code and hopes
that all pieces will eventually fit together.

• Use set of three to seven standard structures.


• Main structures are only three viz. IF-THEN-ELSE, SEQUENCE &
WHILE-DO.
• The others are IF-THEN, CASE & REPEAT-UNTIL.
Finding the right instructions

• Instructions in 8086 are mainly divided into following categories


• Data Transfer Instructions
• Arithmetic Instruction
• Bit manipulation Instruction
• String Instruction
• Program execution transfer Instruction
• Processor control Instruction
Writing a program

• You need to do the following to write the program effectively:


• INITIALIZATION INSTRUCTIONS: used to initialize various parts of the program
like segment registers, flags and programmable port devices.
• STANDARD PROGRAM FORMAT: it’s a tabular format containing ADDRESS,
DATA OR CODE, LABELS, MNEM, OPERAND(S) and COMMENTS as the
columns.
• DOCUMENTATION: you should document the program. E.g. each page of the
document contains page number and name of the program, heading block
containing the abstract about the program, comments should be added
wherever necessary.
Constructing the machine codes for 8086
instructions
• Each instruction in 8086 is associated with the binary code.
• You need to locate the codes appropriately.
• Most of the time this work will be done by assembler
• The things needed to keep in mind is:
• Instruction templates and coding formats
• MOD and R/M Bit patterns for particular instruction
Instruction template

• The Intel literature shows two different formats for coding 8086
instructions.
• Instruction templates helps you to code the instruction properly.

Coding Template for 8086 IN Instruction


MOD and R/M Bit patterns in 8086 (contd.)
MOD and R/M Bit patterns in 8086 - Use

• If the other operand in the instruction is also one of the eight register
then put in 11 for MOD bits in the instruction code.
• If the other operand is memory location, there are 24 ways of
specifying how the execution unit should compute the effective
address of the operand in the main memory.
• If the effective address specified in the instruction contains
displacement less than 256 along with the reference to the contents
of the register then put in 01 as the MOD bits.
• If the expression for the effective address contains a displacement
which is too large to fit in 8 bits then out in 10 in MOD bits.
MOV instruction coding format(contd.)

• MOV SP, BX: this instruction will copy a word from BX register to SP
register.
MOV instruction coding format(contd.)

• MOV 43H [SI], DH: copy a byte from DH register to memory location.
MOV instruction coding format

• MOV CX, [437AH]: copy the contents of the two memory locations to
the register CX.
Tips for Hand Coding 8086 Programs

• Check your algorithm very carefully to make sure that it really does
what it supposed to do.
• Initially write down assembly language statements and comments for
your program.
• Recheck each statement to make sure that you have right instructions
to implement your algorithm.
• Finally work out the binary code needed by each instruction and
execute you program.
Writing programs for use with and assembler

• Allows you to refer the data items by names rather than their offsets.
• How to write the programs so that it can be used by the assemblers.
Program Format

• There are assembler level directives available to the programmer so


that he can write programs which are compatible with the assembler.

Sample
formatted
program
Assembler Directives(contd.)

• There are different directives available for assembler each with their
unique purpose.
• SEGMENT and ENDS Directives
• Naming data and Addresses – EQU, DB, DW and DD Directives
• The ASSUME Directive
• The END Directive
Assembler Directives(contd.)

• SEGMENT and ENDS Directive:


• The SEGMENT and ENDS directives are used to identify a group of data items
or group of instructions that you want to be put together in a particular
segment.
• A logical segment is not usually given starting address when it is declared.

• EQU Directive: used to assign names to constants.


• DB, DW and DD Directives: used to assign names to variables.
Assembler Directives(contd.)

• The ASSUME Directive


• 4 physical segments: code, data, stack and extra segment
• tells which logical segment to use for each of the physical segment at a given
time.

• The END Directive


• Tells the assembler to stop reading
• instruction or statements that are written after END will be ignored.
The 8086 assembly level programming code must be
written in upper case letters.
The labels must be followed by a colon,

for example: label:


All labels and symbols must begin with a letter
All comments are typed in lower case
The last line of the program must be ended with the
END directive
Assembly Language Program Development
Tools
• There are several tools available to support the programmer of
assembly language for better experience of programming. They are
discussed in brief here:
• Editor
• Assembler
• Linker
• Locators
• Debuggers
• Emulators
Editor

• It is the program which allows you to create a file containing the


assembly language statements for your program.
• Examples are PC Write, Word stars and the editors that comes with
assemblers.
• Creates Source file to be processed by the assembler
Assembler

• It is the program which is used to transfer the assembly language


mnemonics to corresponding binary codes. It works in passes.
• In the first pass, it determines the displacement of the named data
items, the offset of labels etc. and puts this information to in a symbol
table.
• In the second pass, it produces the binary codes for each instructions
and inserts the offsets etc. that it calculated in the first pass.
• It generates two files namely object file (.OBJ) and assembler list file
(.LST).
Linker

• It is a program used to join several object files into large object files.
• While writing large programs it is good to divide them into modules
so that each modules can be written, tested, debugged independently
and then use linker to combine the modules to form the actual
program.
• It produces two files link file which contains the binary codes of all
the combined modules and a link map file which contains the address
information about the linked files.
Locators

• A locator is the program used to assign the specific addresses of


where the segments of object code are to be loaded in to main
memory.
• Examples include EXE2BIN which comes with the IBM PC DOS.
• Converts .exe to .bin files which has physical addresses
Debugger

• A debugger is the program which allows you to load your object code
program in to system memory.
• It allows you to look at the contents of the registers and memory
locations after your program runs.
• It also allows you to set breakpoints at any points in the program.
• It simply allows you to find the source of the problem into the
program.
• There are lots of debuggers available like Borland Turbo Debugger,
Microsoft’s Code view debugger etc.
Emulators

• One way to run your program


• It is mixture of hardware and software.
• Generally used to test the hardware and software of an external
system such as microprocessor based instruments.
Applied Microsystems ES 1800
16-bit emulator
UNIT -II
 Instruction Set and Assembly Language Programming
of 8086
 Instruction formats, Addressing modes,
 Instruction Set
 Assembler Directives,
 Procedures, Macros
 Simple Programs involving Logical
 Branch and Call Instructions
 Sorting Evaluating Arithmetic Expressions
 String Manipulations
UNIT-II
The instruction format contains two fields
 operation code / opcode
 Operand field
OPERATION CODE / OPCODE:
 It indicates the type of the operation to be performed by CPU
 Example : MOV , ADD …

OPERAND:
 The CPU executes the instruction using the information resides in these fields .
There are six general formats of instructions in 8086 instruction set.
The instruction of 8086 vary from 1to 6 bytes length
ONE BYTE INSTRUCTION:
 It is only one byte long and may have implied data or register operands.
 The least three significant 3 bits of the opcode are used for specifying register
operand if any otherwise all the 8 bits form an opcode and the operands are implied.
REGISTER TO REGISTER
 The format is 2 byte long
 The first byte of the code specifies the opcode and width
 The second byte of the code shows the register operand and R/M field
 The Register represented by REG is one of the operands . The R/M field specifies
another register or memory location .ie the other operand

REGISTER TO/FROM MEMORY WITH NO DISPLACEMENT


 The format is 2 byte long
 This is similar to the register to register format except for the MOD field is shown.
 The MOD field shows the mode of addressing

REGISTER TO/FROM MEMORY WITH DISPLACEMENT


 The format contains one or two additional bytes for displacement along with 2 bytes
Register to/from memory with no displacement.

IMMEDIATE OPERAND TO REGISTER


 The first byte as well as the 3 bits from the second byte which are used for REG field
in case of Register to register format or used for OPCODE.
 It also contains one are two bytes of data.
IMMEDIATE OPERAND TO MEMORY WITH 16 BIT DISPLACEMENTS
 It requires 5 to 6 bytes for coding

 The first two bytes contains the information regarding OPCODE,MOD and R/M fields
 The remaining 4 bytes contains 2 bytes of displacement and 2 bytes of data
ADDRESSING MODES OF 8086
According two the flow of instructions may be categorized as
1. Sequential Control flow instructions
2. Control transfer instructions
Sequential control flow instructions are the instructions which after execution
transfer control to the next instruction appearing immediately. The control transfer
instructions transfer control to some predefined address or the address somehow
specified in the instruction after their execution.
What is addressing mode?
The different ways in which a source operand is denoted in an instruction are known
as addressing mode the addressing modes for sequential control flow instructions
are

1. Immediate Addressing Mode


2. Direct Addressing mode
3. Register Addressing mode
4. Register Indirect Addressing mode
5. Indexed Addressing Mode
6. Register Relative addressing mode
7. Based indexed addressing mode
8. Relative based indexed Addressing mode

IMMEDIATE ADDRESSING MODE


The addressing mode in which the data operand is a part of the instruction itself is
known as immediate addressing mode.

Example
MOV DL, 08H
The 8-bit data (08H) given in the instruction is moved to DL
(DL)  08H
MOV AX, 0A9FH
The 16-bit data (0A9FH) given in the instruction is moved to AX register
(AX)  0A9FH

DIRECT ADDRESSING MODE


The addressing mode in which the effective address of the memory location at which
the data operand is stored is given in the instruction. The effective address (Offset) is
just a 16-bit number written directly in the instruction.
Example:MOV BX, [1354H]
MOV BL, [0400H]
The square brackets around the 1354H denote the contents of the memory location.
When executed, this instruction will copy the contents of the memory location into BX
register. This addressing mode is called direct because the displacement of the operand
from the segment base is specified directly in the instruction.

REGISTER ADDRESSING MODE

The instruction will specify the name of the register which holds the data to be operated by the
instruction. All registers except IP may be used in this mode

Example:

MOV CL, DH
The content of 8-bit register DH is moved to another 8-bit register CL
(CL)  (DH)

REGISTER INDIRECT ADDRESSING MODE


This addressing mode allows data to be addressed at any memory location through an
offset address held in any of the following registers: BP, BX, DI & SI.

Example
MOV AX, [BX]; suppose the register BX contains 4895H, then the contents
; 4895H are moved to AX
ADD CX, {BX}

INDEXED ADDRESSING MODE

In this addressing mode, the operands offset address is found by adding the contents of
SI or DI register and 8-bit/16-bit displacements. DS and ES are the default segments for
index registers SI and DI respectively. This is the special case of the of register indirect
addressing mode.

Example

MOV BX, [SI+16], ADD AL, [DI+16]

REGISTER RELATIVE ADDRESSING MODE


In register relative Addressing, BX, BP, SI and DI is used to hold the base value for
effective address and a signed 8-bit or unsigned 16-bit displacement will be specified in
the instruction. In case of 8-bit displacement, it is sign extended to 16-bit before adding
to the base value. When BX holds the base value of EA, 20-bit physical address is
calculated from BX and DS.When BP holds the base value of EA, BP and SS is used.
Example:
MOV AX, [BX + 08H] MOV AX, 08H [BX]
BASED INDEXED ADDRESSING MODE
In this addressing mode, the offset address of the operand is computed by summing the
base register to the contents of an Index register. The default segment registers may be
ES or DS
Example:
MOV DX, [BX + SI] MOV DX, [BX][SI]

RELATIVE BASED INDEXED ADDRESSING MODE


In this addressing mode, the operands offset is computed by adding the base register
contents. An Index registers contents and 8 or 16-bit displacement.
Example
MOV AX, [BX+DI+08]
ADD CX, [BX+SI+16]

CONTROL TRANSFER INSTRUCTIONS ADDRESSING MODES /BRANCH ADDRESSING


MODE
The control transfer instructions transfer control to some predefined address or the
address somehow specified in the instruction after their execution
Examples : INT , CALL ,RET and JUMP instructions
The control transfer instruction the addressing modes depend upon whether destination
location is within the same segment or a different one .It also depends on the method of
passing the destination address to the processor
Basically there are two methods for passing control transfer instructions
1. Intersegment addressing mode
2. Intrasegment addressing mode
INTRASEGMENT ADDRESSING MODE
If the destination location is within the same segment the mode is called intrasegment
addressing mode
There are two types
1. Intrasegement direct mode
2. Intrasegment indirect mode
INTRASEGMENT DIRECT MODE:
In this mode the address to which the control is to be transferred lies within the
segment in which the control transfer instruction lies and appears directly in the
instruction as an immediate displacement value .The displacement is computed relative
to the content of the instruction pointer IP.
JMP SHORT LABEL;
is a control transfer instruction following intra segment direct mode. Here, SHORT LABEL
represents a signed displacement.
INTRASEGMENT INDIRECT MODE :
In this mode the displacement to which the control is to be transferred is in the same
segment in which the control transfer instruction lies but it is passed to the instruction
indirectly Here the branch address is found as the content of a register or a memory
location .
Example
JMP [AX]
INTERSEGMENT ADDRESSING MODE
If the destination location is in the different segment the mode is called intersegment
addressing mode
There are two types
1. Intersegment direct mode
2. Intersegment indirect mode
INTERSEGMENT DIRECT MODE:
In this mode the address to which the control is to be transferred is in a different
segment this addressing mode provides a means of branching from one code segment to
another code segment. Here the CS and IP of the destination address are specified
directly in the instruction.
Example
JMP 2000H: 3000H;
INTERSEGMENT INDIRECT MODE :
In this the address to which the control is to be transferred lies in a different segment
and it is passed to the instruction indirectly .Content of memory block containing four
bytes IP(LSB) ,IP(MSB),CS(LSB) and CS(MSB) sequentially The starting address of the
memory block may be referred using any of the addressing mode except immediate
mode .
Example
JMP [5000H];
INSTRUCTION SET OF 8086
The 8086 microprocessor supports 8 types of instructions −

 Data Transfer Instructions


 Arithmetic Instructions
 logical Instructions
 String Instructions
 Program Execution Transfer Instructions (Branch & Loop Instructions)
 Processor Control Instructions
 Iteration Control Instructions
 Interrupt Instructions

1. DATA TRANSFER INSTRUCTIONS


These instructions are used to transfer the data from the source operand to the
destination operand. Following are the list of instructions under this group −

INSTRUCTION TO TRANSFER A WORD


 MOV − Used to copy the byte or word from the provided source to the provided
destination.
 PPUSH − Used to put a word at the top of the stack.
 POP − Used to get a word from the top of the stack to the provided location.
 PUSHA − Used to put all the registers into the stack.
 POPA − Used to get words from the stack to all registers.
 XCHG − Used to exchange the data from two locations.
 XLAT − Used to translate a byte in AL using a table in the memory.

INSTRUCTIONS FOR INPUT AND OUTPUT PORT TRANSFER


 IN − Used to read a byte or word from the provided port to the accumulator.

 OUT − Used to send out a byte or word from the accumulator to the provided
port.

INSTRUCTIONS TO TRANSFER THE ADDRESS


 LEA − Used to load the address of operand into the provided register.

 LDS − Used to load DS register and other provided register from the memory
 LES − Used to load ES register and other provided register from the memory.

INSTRUCTIONS TO TRANSFER FLAG REGISTERS


 LAHF − Used to load AH with the low byte of the flag register.

 SAHF − Used to store AH register to low byte of the flag register.


 PUSHF − Used to copy the flag register at the top of the stack.
 POPF − Used to copy a word at the top of the stack to the flag register.

2. ARITHMETIC INSTRUCTIONS
These instructions are used to perform arithmetic operations like addition,
subtraction, multiplication, division, etc.
Following is the list of instructions under this group −

INSTRUCTIONS TO PERFORM ADDITION


 ADD − Used to add the provided byte to byte/word to word.

 ADC − Used to add with carry.


 INC − Used to increment the provided byte/word by 1.
 AAA − Used to adjust ASCII after addition.
 DAA − Used to adjust the decimal after the addition/subtraction operation.

INSTRUCTIONS TO PERFORM SUBTRACTION


 SUB − Used to subtract the byte from byte/word from word.

 SBB − Used to perform subtraction with borrow.


 DEC − Used to decrement the provided byte/word by 1.
 NPG − Used to negate each bit of the provided byte/word and add 1/2’s
complement.
 CMP − Used to compare 2 provided byte/word.
 AAS − Used to adjust ASCII codes after subtraction.
 DAS − Used to adjust decimal after subtraction.
INSTRUCTION TO PERFORM MULTIPLICATION
 MUL − Used to multiply unsigned byte by byte/word by word.

 IMUL − Used to multiply signed byte by byte/word by word.


 AAM − Used to adjust ASCII codes after multiplication.

INSTRUCTIONS TO PERFORM DIVISION


 DIV − Used to divide the unsigned word by byte or unsigned double word by
word.
 IDIV − Used to divide the signed word by byte or signed double word by word.
 AAD − Used to adjust ASCII codes after division.
 CBW − Used to fill the upper byte of the word with the copies of sign bit of the
lower byte.
 CWD − Used to fill the upper word of the double word with the sign bit of the
lower word.

3. LOGICAL INSTRUCTIONS
These instructions are used to perform operations where data bits are involved,
i.e. operations like logical, shift, etc.
Following is the list of instructions under this group −

INSTRUCTIONS TO PERFORM LOGICAL OPERATION


 NOT − Used to invert each bit of a byte or word.

 AND − Used for adding each bit in a byte/word with the corresponding bit in
another byte/word.
 OR − Used to multiply each bit in a byte/word with the corresponding bit in
another byte/word.
 XOR − Used to perform Exclusive-OR operation over each bit in a byte/word with
the corresponding bit in another byte/word.
 TEST − Used to add operands to update flags, without affecting operands.

INSTRUCTIONS TO PERFORM SHIFT OPERATIONS


 SHL/SAL − Used to shift bits of a byte/word towards left and put zero(S) in LSBs.

 SHR − Used to shift bits of a byte/word towards the right and put zero(S) in
MSBs.
 SAR − Used to shift bits of a byte/word towards the right and copy the old MSB
into the new MSB.

INSTRUCTIONS TO PERFORM ROTATE OPERATIONS


 ROL − Used to rotate bits of byte/word towards the left, i.e. MSB to LSB and to
Carry Flag [CF].
 ROR − Used to rotate bits of byte/word towards the right, i.e. LSB to MSB and to
Carry Flag [CF].
 RCR − Used to rotate bits of byte/word towards the right, i.e. LSB to CF and CF to
MSB.
 RCL − Used to rotate bits of byte/word towards the left, i.e. MSB to CF and CF to
LSB.

4. STRING INSTRUCTIONS
String is a group of bytes/words and their memory is always allocated in a
sequential order.
Following is the list of instructions under this group −
 REP − Used to repeat the given instruction till CX ≠ 0.
 REPE/REPZ − Used to repeat the given instruction until CX = 0 or zero flag ZF = 1.
 REPNE/REPNZ − Used to repeat the given instruction until CX = 0 or zero flag ZF
= 1.
 MOVS/MOVSB/MOVSW − Used to move the byte/word from one string to
another.
 COMS/COMPSB/COMPSW − Used to compare two string bytes/words.
 INS/INSB/INSW − Used as an input string/byte/word from the I/O port to the
provided memory location.
 OUTS/OUTSB/OUTSW − Used as an output string/byte/word from the provided
memory location to the I/O port.
 SCAS/SCASB/SCASW − Used to scan a string and compare its byte with a byte in
AL or string word with a word in AX.
 LODS/LODSB/LODSW − Used to store the string byte into AL or string word into
AX.

5. PROGRAM EXECUTION TRANSFER INSTRUCTIONS (BRANCH AND LOOP INSTRUCTIONS)


These instructions are used to transfer/branch the instructions during an execution. It
includes the following instructions −
Instructions to transfer the instruction during an execution without any condition −
 CALL − Used to call a procedure and save their return address to the stack.
 RET − Used to return from the procedure to the main program.
 JMP − Used to jump to the provided address to proceed to the next instruction.
Instructions to transfer the instruction during an execution with some conditions −
 JA/JNBE − Used to jump if above/not below/equal instruction satisfies.
 JAE/JNB − Used to jump if above/not below instruction satisfies.
 JBE/JNA − Used to jump if below/equal/ not above instruction satisfies.
 JC − Used to jump if carry flag CF = 1
 JE/JZ − Used to jump if equal/zero flag ZF = 1
 JG/JNLE − Used to jump if greater/not less than/equal instruction satisfies.
 JGE/JNL − Used to jump if greater than/equal/not less than instruction satisfies.
 JL/JNGE − Used to jump if less than/not greater than/equal instruction satisfies.
 JLE/JNG − Used to jump if less than/equal/if not greater than instruction
satisfies.
 JNC − Used to jump if no carry flag (CF = 0)
 JNE/JNZ − Used to jump if not equal/zero flag ZF = 0
 JNO − Used to jump if no overflow flag OF = 0
 JNP/JPO − Used to jump if not parity/parity odd PF = 0
 JNS − Used to jump if not sign SF = 0
 JO − Used to jump if overflow flag OF = 1
 JP/JPE − Used to jump if parity/parity even PF = 1
 JS − Used to jump if sign flag SF = 1

6. PROCESSOR CONTROL INSTRUCTIONS


These instructions are used to control the processor action by setting/resetting the flag
values.
Following are the instructions under this group −
 STC − Used to set carry flag CF to 1
 CLC − Used to clear/reset carry flag CF to 0
 CMC − Used to put complement at the state of carry flag CF.
 STD − Used to set the direction flag DF to 1
 CLD − Used to clear/reset the direction flag DF to 0
 STI − Used to set the interrupt enable flag to 1, i.e., enable INTR input.
 CLI − Used to clear the interrupt enable flag to 0, i.e., disable INTR input.

7. ITERATION CONTROL INSTRUCTIONS


These instructions are used to execute the given instructions for number of times.
Following is the list of instructions under this group −
 LOOP − Used to loop a group of instructions until the condition satisfies, i.e., CX
=0
 LOOPE/LOOPZ − Used to loop a group of instructions till it satisfies ZF = 1 & CX =
0
 LOOPNE/LOOPNZ − Used to loop a group of instructions till it satisfies ZF = 0 &
CX = 0
 JCXZ − Used to jump to the provided address if CX = 0

8. INTERRUPT INSTRUCTIONS
These instructions are used to call the interrupt during program execution.
 INT − Used to interrupt the program during execution and calling service
specified.
 INTO − Used to interrupt the program during execution if OF = 1
 IRET − Used to return from interrupt service to the main program

ASSEMBLER DIRECTIVES
Assembler directives are the Instructions to the Assembler, linker and loader
regarding the program being executed. also called ‘pseudo instructions. Control the
generation of machine codes and organization of the program; but no machine codes
are generated for assembler directives.
They are used to
› specify the start and end of a program
› attach value to variables
› allocate storage locations to input/ output data
› define start and end of segments, procedures, macros etc..
ASSUME
Used to tell the assembler the name of the logical segment it should use for a
specified segment. You must tell the assembler that what to assume for any segment
you use in the program.
Example
ASSUME: CODE
Tells the assembler that the instructions for the program are in segment named CODE.
DB – Defined Byte
Used to declare a byte type variable or to set aside one or more locations of type byte in
memory.
Example
PRICES DB 49H, 98H, 29H:
Declare array of 3 bytes named PRICES and initialize 3 bytes as shown.
DD – Define Double Word
Used to declare a variable of type doubleword or to reserve a memory location which
can be accessed as doubleword.
DQ – Define Quadword
Used to tell the assembler to declare the variable as 4 words of storage in memory.
DT – Define Ten Bytes
Used to tell the assembler to declare the variable which is 10 bytes in length or reserve
10 bytes of storage in memory.
DW – Define Word
Used to tell the assembler to define a variable type as word or reserve word in memory.
DUP: used to initialize several locations and to assign values to location
END – End the Program
To tell the assembler to stop fetching the instruction and end the program execution.
ENDP – it is used to end the procedure.
ENDS – used to end the segment.
EQU – EQUATE
Used to give name to some value or symbol.
EVEN – Align On Even Memory Address
Tells the assembler to increment the location counter to the next even address if it is not
already at an even address.
EXTRN
Used to tell the assembler that the name or labels following the directive are in some
other assembly module.
GLOBAL – Declares Symbols As Public Or Extrn
Used to make the symbol available to other modules.It can be used in place of EXTRN or
PUBLIC keyword.
GROUP – Group related segment
Used to tell the assembler to group the logical segments named after the
directive into one logical segment. This allows the content of all the segments to be
accessed from the same group.
INCLUDE – include source code from file
Used to tell the assembler to insert a block of source code from the named file
into the current source module. This shortens the source code.
LABEL
Used to give the name to the current value in the location counter. The LABEL directive
must be followed by a term which specifies the type you want associated with that
name.
LENGTH
Used to determine the number of items in some data such as string or array.
NAME
Used to give a specific name to a module when the programs consisting of several
modules.
OFFSET
It is an operator which tells the assembler to determine the offset or displacement of
named data item or procedure from the start of the segment which contains it.
ORG – Originate
Tells the assembler to set the location counter value.
Example, ORG 7000H sets the location counter value to point to 7000H location in
memory.
$ is often used to symbolically represent the value of the location counter. It is
used with ORG to tell the assembler to change the location according to the current
value in the location counter. E.g. ORG $+100.
Arithematic Operation 8086 microprocessor

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 ? // reserving uninitialized space
DATA ENDS

END

List file:

ADDRESS OPCODE OPERATION COMMENTS


0000 B88E4E MOV AX,4E8E INITIALIZATION OF
0003 8ED8 MOV DS,AX DATA SEGMENT

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 ? // reserving uninitialized space
DATA ENDS

END

List file:

ADDRESS OPCODE OPERATION COMMENTS


0000 B88E4E MOV AX,4E8E INITIALIZATION OF
0003 8ED8 MOV DS,AX DATA SEGMENT
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 ? // reserving uninitialized space
RESHW DW ? // reserving uninitialized space
DATA ENDS

END

List file:

ADDRESS OPCODE OPERATION COMMENTS


0000 B88E4E MOV AX,4E8E INITIALIZATION OF
0003 8ED8 MOV DS,AX DATA SEGMENT

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 ? // reserving uninitialized space
RESHW DW ? // reserving uninitialized space
DATA ENDS

END

List File:

ADDRESS OPCODE OPERATION COMMENTS


0000 B88E4E MOV AX,4E8E INITIALIZATION OF
0003 8ED8 MOV DS,AX DATA SEGMENT

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 ? // reserving uninitialized space
RESR DB ? // reserving uninitialized space
DATA ENDS

END

List file:

ADDRESS OPCODE OPERATION COMMENTS


0000 B88F4E MOV AX,4E8F INITIALIZATION OF
0003 8ED8 MOV DS,AX DATA SEGMENT

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 ? // reserving uninitialized space
RESR DW ? // reserving uninitialized space
DATA ENDS

END

List file:

ADDRESS OPCODE OPERATION COMMENTS


0000 B88F4E MOV AX,4E8F INITIALIZATION OF
0003 8ED8 MOV DS,AX DATA SEGMENT

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 ? // reserving uninitialized space
DATA ENDS

END

List file:

ADDRESS OPCODE OPERATION COMMENTS


0000 B88E4E MOV AX,4E8E INITIALIZATION OF
0003 8ED8 MOV DS,AX DATA SEGMENT

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 ? // reserving uninitialized space
DATA ENDS

END

List file:

ADDRESS OPCODE OPERATION COMMENTS


0000 B88E4E MOV AX,4E8F INITIALIZATION OF
0003 8ED8 MOV DS,AX DATA SEGMENT

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 ? // reserving uninitialized space
DATA ENDS

END

List file:

ADDRESS OPCODE OPERATION COMMENTS


0000 B88E4E MOV AX,4E8F INITIALIZATION OF
0003 8ED8 MOV DS,AX DATA SEGMENT

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 ? // reserving uninitialized space
RESR DB ? // reserving uninitialized space
DATA ENDS

END

List file:

ADDRESS OPCODE OPERATION COMMENTS


0000 B88F4E MOV AX,4E8F INITIALIZATION OF
0003 8ED8 MOV DS,AX DATA SEGMENT

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
ADDRESS OPCODE OPERATION COMMENTS
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)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy