Mod 2
Mod 2
8086 Addressing Modes, 8086 Instruction set and Assembler Directives - Assembly Language
Programming with Subroutines, Macros, Passing Parameters, Use of stack.
Instruction Format in 8086
The instruction format of 8086 has one or more number of fields associated with it.
The first filled is called operation code field or opcode field, which indicates the type of operation.
The instruction format also contains other fields known as operand fields.
There are six general formats of instructions in 8086 instruction set. The length of an instruction may vary
from one byte to six bytes.
a) One byte Instruction: This format is only one byte long and may have the implied data or register
operands. The least significant 3 bits of the opcode are used for specifying the register operand, if any.
Otherwise, all the eight bits used to store opcode
CLC : clear carry
b) Register to Register: This format is 2 bytes long. The first byte of the code specifies the operation
code and the width of the operand specifies by w bit. The second byte of the opcode shows the register
operands and R/M field.
D7 D1 D0 D7 D6 D5 D4 D3 D2 D1 D0
The register represented by the REG field is one of the operands. The R/M field specifies another register
or memory location, ie., the other operand. The register specified by REG is a source operand if D = 0 ,
else it is a destination operand.
For example:
MOV: data transfer operation from Register to Register.
MOV CL, AL, i.e CL � AL
C) Register to/from memory with no displacement: This format is also 2 bytes long and similar to the
register to register format except for the MOD field.
D7 D1 D0 D7 D6 D5 D4 D3 D2 D1 D0
D7 D0 D7 D0
Lower byte
DATA Higher byte DATA
f) immediate operand to memory with 16-bit displacement : This type of instruction format requires 5
to 6 bytes for coding. The first two bytes contain the information regarding OPCODE, MOD and R/M
fields. The remaining 4 bytes contain 2 bytes of displacement and 2 bytes of data.
D7 D0 D7 D6 D5 D4 D3 D2 D1 D0
D7 D0 D7 D0 D7 D0
Higher byte of
Lower byte of Higher byte of
displacement DATA DATA
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.
ADD AX,BX – add content of AX &BX and store result into AX reg
ADD AX, 0100 - add content of AX &0100 and store result into AX reg
ADD AX, [SI] - add content of AX &content of SI and store result into AX reg
ADC − Used to add with carry.
ADC AX,BX - Add AX+BX+ carry one
ADC AX, 0100H
ADC AX, [5000H]
INC − Used to increment the provided byte by 1.
INC AX - ( Increment AX by one)
INC [BX] Content of BX incremented by one
DEC − Used to decrement the provided byte by 1.
DEC AX - ( Decrement AX by one)
CWD − (Convert signed word to double word)-Used to fill the upper word of the double word
with the sign bit of the lower word.
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 register ≠ 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.
MOVSB/MOVSW − Used to move the byte/word from one string to another.
a string of bytes stored in a set of consecutive memory locations is to be moved to
another set of destination locations.
Example
To move a string of length 4 bytes from SRC to DST
MOV SI, SRC
MOV DI, DST
MOV CX, 04H
CLD; Clear the direction flag
REP MOVSB
LODS − Used to store the string byte into AL or string word into AX.
This instruction copies a byte (word) of string from the location pointed to by SI. The SI value is
automatically incremented (depending on the DF value) after each movement by 1 byte (2 bytes) for a
byte string (word string).
CLD
MOV SI, Offset STR
LODSB
STOS(Store string or string word)
The STOS instruction copies a byte (word) from AL (AX) to a memory location stored in [ES:DI]. DI is
automatically incremented after the copy; the DF determines the increment/decrement.
MOV DI offset STR
STOSW
Assembler Directives
Assembler is a program used to convert an assembly language program into machine equivalent code.
There are some instructions in the assembly language program which are not a part of instruction set.
These instructions are instructions to the assembler, linker and loader. These are referred to as pseudo-
operations or as assembler directives.
There are many specialized assembler directives. Let us see the commonly used assembler directive in
8086 assembly language programming.
1. ASSUME:
It is used to tell the name of the logical segment the assembler to use for a specified segment.
E.g.: ASSUME CS: CODE tells that the instructions for a program are in a segment named CODE.
2. DB -Define Byte:
The DB directive is used to reserve byte or bytes of memory locations in the available memory. The
following examples show how the DB directive is used for different purposes.
RANKS DB 01H,02H,03H,04H
This statement directs the assembler to reserve four memory locations for a list named RANKS and
initialize them with the above specified four values.
MESSAGE DB ‘GOOD MORNING’
11. EVEN - Tells the assembler to increment the location counter to the next even address if it is not
already at an even address.
Used because the processor can read even addressed data in one clock cycle
EVEN
PROCEDURE ROOT
……..
ROOT ENDP
MODULE 2 SEGMENT
EXTRN FACTORIAL FAR
MODULE2 ENDS
MOV AX, [BX] ; Suppose the register BX contains 4895H, then the contents
; 4895H are moved to AX
ADD CX, {BX}
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)
• Used to store temporary data during program execution
• One point of access - the top of the stack
• A stack is always operated as Last-In-First-Out (LIFO) storage, i.e., data are retrieved in the
reverse order to which they were stored
• Instructions that directly manipulate the stack
– PUSH - place element on top of stack
– POP - remove element from top of stack
CSE ,ICET Page 19
• SS - Stack Segment
• SP (stack pointer) always points to the top of the stack
– SP initially points to top of the stack (high memory address).
– SP decreases as data is PUSHed
PUSH AX ==> SUB SP, 2 ; MOV [SS:SP], AX
– SP increases as data is POPed
POP AX ==> MOV AX, [SS:SP] ; ADD SP, 2
In this figure top of stack = 03800 ( Add 0 to the right of SS ie 03000 + SPie 0800)
First decrement top of stack by one (03800-1=037FF)
CSE ,ICET Page 20
Push 6A into that location. Decrement top of stack by one and push B3 to that location(037FE)