1|Page
2|Page
3|Page
LABEL: Label
The Label directive is used to assign a name to the current content of the location
counter. During the assembly process, whenever the assembler comes across the LABEL
directive, it assigns the declared label with the current contents of the location counter.
EQU: Equate
The EQU directive is used to give name to some value or symbol. Each time the
assembler finds the given names in the program, it will replace the name with the value or a
symbol. The value can be in the range 0 through 65535 and it can be another Equate declared
anywhere above or below.
The examples given below show the syntax.
Example
LABEL
EQU 0500H
ADDITION EQU ADD
The first statement assigns the constant 500H with the label LABEL, while the second
statement assigns another label ADDITION with mnemonic ADD.
ORG: Origin
The ORG directive directs the assembler to start the memory allotment for the
particular segment, block or code from the declared address in the ORG statement while
starting the assembly process for a module, the assembler initializes a location counter to
keep track of the allotted addresses for the module.
If the ORG statement is not written in the program, the location counter is initialized
to 0000. Thus, the code for different modules and segments can be located in the available
memory as required by the programmer. The ORG directive can even be used with data
segments similarly.
PROC: Procedure
The PROC directive is used to identify the start of a procedure. The term near or far is used to
specify the type of the procedure.
INSTRUCTION SET OF 8086
The 8086 instructions are categorized into the following main types.
1.
Data Copy / Transfer Instructions
2.
Arithmetic and Logical Instructions
3.
Branch Instructions
4.
Loop Instructions
5.
Machine Control Instructions
6.
Flag Manipulation Instructions
7.
Shift and Rotate Instructions
8.
String Instructions
4|Page
5|Page
XLAT:
Translate byte using look-up table
Eg.
LEA BX, TABLE1
MOV AL, 04H
XLAT
Simple input and output port transfer Instructions:
IN:
Copy a byte or word from specified port to accumulator.
Eg.
IN AL,03H
IN AX,DX
OUT:
Copy a byte or word from accumulator specified port.
Eg.
OUT 03H, AL
OUT DX, AX
LEA:
Load effective address of operand in specified register.
REG = address of memory (offset)
Example:
MOV BX, 35h
MOV DI, 12h
LEA SI, [BX+DI]; SI = 35h + 12h = 47h
org 100h
LEA AX, mem; AX = offset of mem
RET
mem dw 1234h
END
LDS:
Load DS register and other specified register from memory.
REG = first word
DS = second word
Example:
ORG 100h
LDS AX, mem
RET
mem DW 1234h
DW 5678h
END
AX is set to 1234h, DS is set to 5678h.
6|Page
LES:
Load ES register and other specified register from memory.
REG = first word
ES = second word
Example:
ORG 100h
LES AX, m
RET
mem DW 1234h
DW 5678h
END
AX is set to 1234h, ES is set to 5678h.
Flag transfer instructions:
LAHF:
Load (copy to) AH with the low byte of the flag register.
[AH] [ Flags low byte]
Eg.
LAHF
SAHF:
Store (copy) AH register to low byte of flag register.
[Flags low byte] [AH]
Eg.
SAHF
PUSHF:
Copy flag register to top of stack.
[SP] [SP] 2
[[SP]] [Flags]
Eg.
PUSHF
POPF :
Copy word at top of stack to flag register.
[Flags] [[SP]]
[SP] [SP] + 2
Eg.
POPF
Arithmetic Instructions:
The 8086 provides many arithmetic operations: addition, subtraction, negation,
multiplication and comparing two values.
ADD: Addition
The add instruction adds the contents of the source operand to the destination
operand.
Eg.
ADD AX, 0100H
ADD AX, BX
ADD AX, [SI]
ADD AX, [5000H]
Unit-2: P-5 [ 8086 Microprocessor and Interfacing] [ELE-355]
7|Page
8|Page
Eg.
9|Page
10 | P a g e
Branch Instructions :
Branch Instructions transfers the flow of execution of the program to a new address
specified in the instruction directly or indirectly. When this type of instruction is executed,
the CS and IP registers get loaded with new values of CS and IP corresponding to the
location to be transferred.
The Branch Instructions are classified into two types
1. Unconditional Branch Instructions.
2. Conditional Branch Instructions.
Unit-2: P-5 [ 8086 Microprocessor and Interfacing] [ELE-355]
11 | P a g e
12 | P a g e
JZ/JE Label
Transfer execution control to address Label, if ZF=1.
JNZ/JNE Label
Transfer execution control to address Label, if ZF=0
JS Label
Transfer execution control to address Label, if SF=1.
JNS Label
Transfer execution control to address Label, if SF=0.
JO Label
Transfer execution control to address Label, if OF=1.
JNO Label
Transfer execution control to address Label, if OF=0.
JNP Label
Transfer execution control to address Label, if PF=0.
JP/JPE Label
Transfer execution control to address Label, if PF=1.
JB/JNAE/JC Label
Transfer execution control to address Label, if CF=1.
JNB/JAE/JNC Label
Transfer execution control to address Label, if CF=0.
JBE/JNA Label
Transfer execution control to address Label, if CF=1 or ZF=1.
JNBE/JA Label
Transfer execution control to address Label, if CF=0 or ZF=0.
JL/JNGE Label
Transfer execution control to address Label, if neither SF=1 nor OF=1.
JNL/JGE Label
Transfer execution control to address Label, if neither SF=0 nor OF=0.
JLE/JNG Label
Transfer execution control to address Label, if ZF=1 or neither SF nor OF is 1.
JNLE/JE Label
Transfer execution control to address Label, if ZF=0 or neither any one of SF and OF is 1
JCXZ Label
Transfer execution control to address Label, if CX=0
Conditional LOOP Instructions.
LOOPZ / LOOPE Label
Loop through a sequence of instructions from label while ZF=1 and CX=0.
LOOPNZ / LOOPENE Label
Loop through a sequence of instructions from label while ZF=1 and CX=0.
13 | P a g e
memory locations, where each character may be represented by its ASCII equivalent. The
8086 supports a set of more powerful instructions for string manipulations for referring to a
string, two parameters are required.
1. Starting and End Address of the String.
2. Length of the String.
The length of the string is usually stored as count in the CX register. The
incrementing or decrementing of the pointer, in string instructions, depends upon the
Direction Flag (DF) Status. If it is a Byte string operation, the index registers are updated by
one. On the other hand, if it is a word string operation, the index registers are updated by two.
REP : Repeat Instruction Prefix
This instruction is used as a prefix to other instructions, the instruction to which the
REP prefix is provided, is executed repeatedly until the CX register becomes zero (at each
iteration CX is automatically decremented by one).
1. REPE / REPZ - repeat operation while equal / zero.
2. REPNE / REPNZ - repeat operation while not equal / not zero.
These are used for CMPS, SCAS instructions only, as instruction prefixes.
MOVSB / MOVSW :Move String Byte or String Word
Suppose a string of bytes stored in a set of consecutive memory locations is to be
moved to another set of destination locations. The starting byte of source string is located in
the memory location whose address may be computed using SI (Source Index) and DS (Data
Segment) contents.
The starting address of the destination locations where this string has to be relocated
is given by DI (Destination Index) and ES (Extra Segment) contents.
CMPS : Compare String Byte or String Word
The CMPS instruction can be used to compare two strings of byte or words. The
length of the string must be stored in the register CX. If both the byte or word strings are
equal, zero Flag is set.
The REP instruction Prefix is used to repeat the operation till CX (counter) becomes
zero or the condition specified by the REP Prefix is False.
SCAN : Scan String Byte or String Word
This instruction scans a string of bytes or words for an operand byte or word specified
in the register AL or AX. The String is pointed to by ES:DI register pair. The length of the
string s stored in CX. The DF controls the mode for scanning of the string.
Whenever a match to the specified operand, is found in the string, execution stops and
the zero Flag is set. If no match is found, the zero flag is reset.
LODS : Load String Byte or String Word
The LODS instruction loads the AL / AX register by the content of a string pointed to
by DS : SI register pair. The SI is modified automatically depending upon DF, If it is a byte
transfer (LODSB), the SI is modified by one and if it is a word transfer (LODSW), the SI is
modified by two. No other Flags are affected by this instruction.
STOS : Store String Byte or String Word
The STOS instruction Stores the AL / AX register contents to a location in the string
pointer by ES : DI register pair. The DI is modified accordingly, No Flags are affected by this
instruction.
Unit-2: P-5 [ 8086 Microprocessor and Interfacing] [ELE-355]
14 | P a g e
The direction Flag controls the String instruction execution, The source index SI and
Destination Index DI are modified after each iteration automatically. If DF=1, then the
execution follows auto decrement mode, SI and DI are decremented automatically after each
iteration. If DF=0, then the execution follows auto increment mode. In this mode, SI and DI
are incremented automatically after each iteration.
Flag Manipulation and a Processor Control Instructions
These instructions control the functioning of the available hardware inside the
processor chip. These instructions are categorized into two types:
1. Flag Manipulation instructions.
2. Machine Control instructions.
Flag Manipulation instructions
The Flag manipulation instructions directly modify some of the Flags of 8086.
1. CLC Clear Carry Flag.
2. CMC Complement Carry Flag.
3. STC Set Carry Flag.
4. CLD Clear Direction Flag.
5. STD Set Direction Flag.
6. CLI Clear Interrupt Flag.
7. STI Set Interrupt Flag.
Machine Control instructions
The Machine control instructions control the bus usage and execution
1. HLT This instruction stops the execution. The processor stops fetching and executing
instructions. The processor enters a halt stage.
2. WAIT When WAIT instruction is executed, the processor enters an idle state in which
the processor does no processing. The processor remains in the idle state until
TEST pin is made low or until INTR or NMI interrupt occurs.
3. NOP This instruction performs no operation. It takes 3 clock cycles and increments
instruction pointer to point to the next instruction. It can be used to produce delay.
4. ESC This instruction makes bus free for external master like a coprocessor or
peripheral device.
5. LOCK It makes LOCK pin low, no other processor can take the control of the system
bus in a multiprocessor system.
STACKS
The stack is a block of memory that may be used for temporarily storing the contents
of the registers inside the CPU. It is a top-down data structure whose elements are accessed
using the stack pointer (SP) which gets decremented by two as we store a data word into the
stack and gets incremented by two as we retrieve a data word from the stack back to the CPU
register.
The process of storing the data in the stack is called pushing into the stack and the
reverse process of transferring the data back from the stack to the CPU register is known as
popping off the stack. The stack is essentially Last-In-First-Out (LIFO) data segment. This
means that the data which is pushed into the stack last will be on top of stack and will be
Unit-2: P-5 [ 8086 Microprocessor and Interfacing] [ELE-355]
15 | P a g e
popped off the stack first. The stack pointer is a 16-bit register that contains the offset address
of the memory location in the stack segment.
The stack segment, like any other segment, may have a memory block of a maximum
of 64 Kbytes locations, and thus may overlap with any other segments. Stack Segment
register (SS) contains the base address of the stack segment in the memory.
The Stack Segment register (SS) and Stack pointer register (SP) together address the
stack-top as explained below:
SS ---5000H
SP ---2050H
Physical address of Stack-top = 5000H * 10H + 2050H
If the stack top points to a memory location 52050H, it means that the location 52050H is
already occupied with the previously pushed data. The next 16 bit push operation will
decrement the stack pointer by two, so that it will point to the new stack-top 5204EH and the
decremented contents of SP will be 204EH. This location will now be occupied by the
recently pushed data.
Thus for a selected value of SS, the maximum value of SP=FFFFH and the segment
can have maximum of 64K locations. If the SP starts with an initial value of FFFFH, it will
be decremented by two whenever a 16-bit data is pushed onto the stack. After successive
push operations, when the stack pointer contains 0000H, any attempt to further push the data
to the stack will result in stack overflow.
PROCEDURES
A series of instructions written separately to carry out a specific subtask in a main
program in called subprogramme or procedure. This is also called subroutine.
The main purpose of procedure in when we have to execute a small program (or' at
part of main program) again and again. So to write that part of programs again and again, in
main program will waste the memory space and also the execution time of programs are
increased. Sometimes. procedures are written to break a large programs in to small modules.
Doing this make easy to write, test and debugg a program.
A CALL instruction in the main program calls the procedure and then procedure is
executed. At the end of procedure a RET, return instruction, sends execution back to the next
instruction after the CALL in the main program.
Unit-2: P-5 [ 8086 Microprocessor and Interfacing] [ELE-355]
16 | P a g e
MACROS
Macro is a segment of code that needs to be written only once but whose basic
structure can be caused to be repeated several times within a source module by placing a
single statement at the point of each reference.
The macro is also a similar concept of subroutine. Suppose, a number of instructions
are repeating through in the main program, the listings becomes lengthily. So a macro is
assigned with the repeatedly appearing string of instructions. The MACRO label is then used
throughout the main program to refer to that string of instructions. A macro within a macro is
called a nested macro.
The difference between a macro and a subroutine is that in the macro the complete
code of the instructions string is inserted at each place where the macro label appears. Macro
does not utilize the service of stack, so there is no transfer of control. Macro requires less
time for execution, as it does not contain CALL and RET instructions as the subroutines do.
In subroutines the executable code becomes smaller as compared to macros which appears
only once in the complete code. The subroutine utilizes the stack service and program
requires less memory space for execution than that using macro.
Introduction to Assembly Language Tools
Software tools are used for editing, assembling, linking, and debugging assembly language
programming. They contain an assembler, a linker, a debugger, and an editor. These tools
are briefly explained below.
Editor
A text editor to create assembly language source files. We can use NotePad , or any other
editor that produces plain ASCII text files.
Assembler
An assembler is a program that converts source-code programs written in assembly
language into object files in machine language. Popular assemblers have emerged over the
years for the Intel family of processors. These include MASM (Macro Assembler from
Microsoft), TASM (Turbo Assembler from Borland) and NASM (Netwide Assembler for
both Windows and Linux).
Linker
A linker is a program that combines your program's object file created by the assembler with
other object files and link libraries, and produces a single executable program.
Debugger
A debugger is a program that allows you to trace the execution of a program and examine
the content of registers and memory. For 16-bit programs, MASM supplies a 16-bit debugger
named CodeView. CodeView can be used to debug only 16-bit programs and is already
provided with the MASM distribution.
17 | P a g e