Download as pdf or txt
Download as pdf or txt
You are on page 1of 81

8086 INSTRUCTION SET

1-May-23 1
1. Data Copy / Transfer Instructions
2. Arithmetic Instructions
3. Logical Instructions
4. Shift and Rotate Instructions
5. String Manipulation Instructions
6. Branch and Loop Instructions
5. Flag Manipulation Instructions
6. Machine Control Instructions

1-May-23 2
Instruction Set (continued...)
1. DATA TRANSFER INSTRUCTIONS

 Transfers data from one register or memory


location to another register or memory location

▪ Source: Seg.Register /
Gen.Register /Mem.location
▪ Destination: Seg.Register /
Gen.Register /Mem.location
▪ Note: Both Source and
Destination cannot be
Mem.Locn

1-May-23 3
Instruction Set (continued...)
General purpose byte or word transfer instructions
MOV Copy byte or word from specified source to specified
destination
PUSH Copy specified word to top of stack
POP Copy word from top of stack to specified
location
XCHG Exchange bytes or exchange words
XLAT Translate a byte in AL using a table in
memory
Simple input and output port transfer instructions
IN Copy a byte or word from specified port to
accumulator
OUT Copy a byte or word from accumulator to
specified port
1-May-23 4
Instruction Set (continued...)
Special address transfer instructions:
LEA Load effective address of operand into specified
register
LDS Load DS register and other specified register from
memory
LES Load ES register and other specified register from
memory
Flag transfer instructions
LAHF Load (copy to ) AH with the low byte of the flag
register
SAHF Store (copy) AH register to low byte of flag
register
PUSHF Copy flag register to top of stack
POPF Copy word at top of stack to flag register
1-May-23 5
Instruction Set (continued...)
Instruction Comments
MOV DS, 5000H Not Valid; immediate addressing with
segment registers not permitted

MOV AX, 5000H; Immediate addressing


MOV DS, AX Register addressing
MOV AX, [2000H] Direct addressing
MOV AX, [SI] Indirect addressing
MOV AX , 50H[BX] Register relative / Indexed addr
• Push Higher Byte AH into the location
PUSH AX pointed by SP and Push Lower Byte AL
PUSH DS into the location pointed by SP-1;
PUSH [5000H] SPSP-2
• Content of loc. 5000H followed by
5001H in DS are pushed into the stack
1-May-23 6
Instruction Set (continued...)
Do not click the mouse to see the full animation

Example 1 :

*Knowing that,
DS=2042H.
SI=500H, AH AL
AX=214E, 21 4E

MOV [SI], AX 21 20921H


4E 20920H

SI=500

DS=2042H DS=20420H

1-May-23 7
Instruction Set (continued...)
Example 2 :
Physical Address
PUSH AX
Stack Segment

AH AL 20000 SS = 2000
88 44 SP =

44 2FFFB FFFB
88 2FFFC FFFC
XX 2FFFD FFFD
XX 2FFFE FFFE
XX 2FFFF FFFF

1-May-23 8
Instruction Set (continued...)
Instruction Comments
• Pops stack top content pointed by SP into
Lower Byte of the register / mem. location
1. POP AX and next byte in the stack into Higher Byte
2. POP DS of the register / mem. location ; SPSP+2
3. POP [5000H] • Content of stack top and next byte in the
stack are popped into 5000H & 5001H in
DS
• Exchanges data b/w AX &
1. XCHG [5000H], AX
2. XCHG BX, AX mem.loc [5000H] in data seg.
•Exchange data b/w AX & BX
1. IN AL, 03H
2. IN AX, DX 03H is 8-bit port address
3. MOV DX, 0800H [DX] is 6-bit port address
IN AX, DX
1-May-23 9
Instruction Set (continued...)
Example 3 :
Physical Address
POP AX
Stack Segment

AH AL 20000 SS = 2000
xx
88 xx
44 SP =

44 2FFFB FFFB
88 2FFFC FFFC
XX 2FFFD FFFD
2FFFE FFFE
2FFFF FFFF

1-May-23 10
Instruction Set (continued...)
Instruction Comments

1. OUT 03H, AL 1. Send data in AL to a port whose


address is 03H
2. OUT DX, AX 2. Send data in AX to a port whose
3. MOV DX, 0300H address is in DX
OUT DX, AX 3. Send data in AX to a port whose
address is in DX, set as 0300H

1-May-23 11
Instruction Set (continued...)
Example 4 :

AL Mem[AL+BX]

dddd+mmmm data

AL=dddd

BX=mmmm AL=data

for the example slides do not click the mouse to


see the full animation 1-May-23 12
Instruction Set (continued...)
More clearly the XLAT instruction converts the
content of register AL, using a table. Its beginning
address is contained in register BX. The content of
register AL is interpreted as a relative address in the
table.

• XLAT is the only instruction that adds 8 bit number


to 16 bit number!

• XLAT does not Affect the flags..

1-May-23 13
Instruction Set (continued...)
1 2 83
KB(0-F) 8255 8086 CPU
User 16-Keys KBD 4 AL BX 5
interface 7
7-seg 9 6
Display

LUT
-
-
-
Example 5 : LUT- Mem.Seg

Program using XLAT Comments

1. Address of seg containing


MOV AX, SEG TABLE LUT is stored in DS
MOV DS, AX 2. Code of key pressed
MOV AL, CODE transferred to AL
MOV BX, OFFSET TABLE 3. Offset of code LUT into BX
XLAT used as Base Addr by XLAT
EffectAddr =10*SEG + BX+AL
4. AL [10*SEG + BX+AL]
8086 Instruction Set (continued...) 1-May-23 14
Instruction Comments
1. LEA BX, ADR 1. BX  Offset (ADR)
2. LEA SI, ADR[BX] 2. SI  Offset (ADR) + [BX]
3. LDS BX, 5000H 3. BX(LB)  [5000H]
BX(HB)  [5001H]
4. LES BX, 5000H
DS/ES(LB)  [5002H]
DS/ES(HB)  [5003H]

Note: Effective Address is also called as Offset

8086 Instruction Set (continued...) 1-May-23 15


Instruction Comments
1. LAHF 1. AH  [Flag (LB)]
2. SAHF 2. Set/Reset all condition code flags except
Overflow flag
3. PUSHF 3. SP  SP-2; for each PUSH operation;
Push FlagRegr(HB) followed by
FlagRegr(LB) into the stack SS:SP
4. POPF 4. SP  SP+2; for each POP operation;
Pop into FlagRegr(LB) followed into
FlagRegr(HB) from stack SS:SP

8086 Instruction Set (continued...) 1-May-23 16


1. Data Copy / Transfer Instructions
2. Arithmetic Instructions
3. Logical Instructions
4. Shift and Rotate Instructions
5. String Manipulation Instructions
6. Branch and Loop Instructions
5. Flag Manipulation Instructions
6. Machine Control Instructions

8086 Instruction Set (continued...) 1-May-23 17


2. ARITHMETIC INSTRUCTIONS

Performs
❑ Addition,
❑ Subtraction,
❑ Multiplication,
❑ Division,
❑ Increment,
❑ Decrement
along with ASCII and Decimal adjustment

▪ Operands in : Immediate / Register / Memory locn.


▪ Note: Flag bits in flag register are affected

8086 Instruction Set (continued...) 1-May-23 18


Addition instructions:
ADD Add specified byte to byte or specified word to
word
ADC Add byte + byte + carry flag (or)
word + word + carry flag
INC Increment specified byte or specified word by 1
AAA ASCII adjust after addition
DAA Decimal (BCD) adjust after addition
Multiplication instructions
MUL Multiply unsigned byte by byte or unsigned word
by word
IMUL Multiply signed byte by byte or signed word by
word
AAM ASCII adjust after multiplication
8086 Instruction Set (continued...) 1-May-23 19
Subtraction Instructions:
SUB Subtract byte from byte or word for word
flag from word.
SBB Subtract byte and carry flag from byte or word ad
carry
DEC Decrement specified byte or specified word by 1
NEG Negate – invert each bit of a specified byte or word
and add 1. (form 2’s complement)
CMP Compare two specified bytes or two specified
words
AAS ASCII adjust after subtraction
DAS Decimal (BCD) adjust after subtraction
8086 Instruction Set (continued...) 1-May-23 20
Division Instructions:
DIV Divide unsigned word by byte or unsigned DW by
word
IDIV Divide signed word by byte or signed DW by word
AAD ASCII adjust before division
CBW Fill upper byte of word with copies of sign bit of
lower byte
CWD Fill upper word of DW with sign bit of lower word

8086 Instruction Set (continued...) 1-May-23 21


• Result is in destination operand
• All the condition code flags are affected
• Content of segment registers cannot be added
• Memory to memory addition not possible
ADD Add
1. ADD AX, 0100H 1. Immediate addressing
2. ADD AX, BX 2. Register addressing
3. ADD AX, [SI] 3. Indexed / indirect addressing
4. ADD AX, [5000H] 4. Direct addressing

5. ADD [5000H], 0100H 5. Immediate addressing

6. ADD 0100H 6. Implicit addressing


(destination AX) &
• Source : Regr / Mem.Locn / immediate
• Destination : Regr / Mem.Locn immediate addressing

8086 Instruction Set (continued...) 1-May-23 22


• Same as ADD instruction
• Adds the carry flag bit with the result.
• All the condition code flags are affected
ADDC Add with carry
1. ADC 0100H 1. Immediate addressing
2. ADC AX, BX 2. Register addressing
3. ADC AX, [SI] 3. Indexed / indirect addressing
4. ADC AX, [5000H] 4. Direct addressing

5. ADC [5000H], 0100H 5. Immediate addressing

6. ADD 0100H 6. Implicit addressing


(destination AX) &
• Source : Regr / Mem.Locn / immediate
• Destination : Regr / Mem.Locn
immediate addressing

8086 Instruction Set (continued...) 1-May-23 23


• Register/Mem.content incremented / decremented by 1
• All the condition code flags are affected except CF
• Immediate operand cannot be incremented

INC Increment
1. INC AX 1. Register addressing
2. INC [BX] 2. Register indirect addressing
3. INC [5000H] 3. Direct addressing

DEC Decrement
1. DEC AX 1. Register addressing
2. DEC [BX] 2. Register indirect addressing
3. DEC [5000H] 3. Direct addressing

8086 Instruction Set (continued...) 1-May-23 24


• Destn.operand  Destn.operand – Source operand
• All the condition code flags are affected

SUB Subtract
1. SUB AX, 0100H 1. Immediate addressing
2. SUB AX, BX 2. Register addressing
3. SUB AX, [SI] 3. Indexed / indirect addressing
4. SUB AX, [5000H] 4. Direct addressing
5. SUB [5000H], 0100H 5. Immediate addressing

• Source : Regr / Mem.Locn / immediate


• Destination : Regr / Mem.Locn

8086 Instruction Set (continued...) 1-May-23 25


• Destn.operand  Destn.operand – (Source operand + CF)
• All the condition code flags are affected

SBB Subtract with borrow


1. SBB AX, 0100H 1. Immediate addressing
2. SBB AX, BX 2. Register addressing
3. SBB AX, [SI] 3. Indexed / indirect addressing
4. SBB AX, [5000H] 4. Direct addressing
5. SBB [5000H], 0100H 5. Immediate addressing

• Source : Regr / Mem.Locn / immediate


• Destination : Regr / Mem.Locn

8086 Instruction Set (continued...) 1-May-23 26


• Destn.operand – Source operand
• Result not stored anywhere
• All the condition code flags are affected
• ZF=1(equal); CY=1 (src > destn); CY =0, ZF=0 (src < destn)

CMP Compare
1. CMP AX, 0100H 1. Immediate addressing
2. CMP BX, 0100H 2. Immediate addressing
3. CMP BX, [SI] 3. Indexed / indirect addressing
4. CMP BX, CX 4. Register addressing
5. CMP [5000H], 0100H 5. Direct/Immediate addressing

• Source : Regr / Mem.Locn / immediate


• Destination : Regr / Mem.Locn

8086 Instruction Set (continued...) 1-May-23 27


AAA ASCII Adjust after addition

• Executed after ADD


• ADD must have added two ASCII no’s & result is in AL
• AAA converts AL to unpacked decimal digits(Implicit addressing)
AX AH (8 bits) AL (8 bits)

ALHIGH ALLOW

(1) If 0 < ALLOW < 9 and AF=0, then ALHIGH = 0


(2) If 0 < ALLOW < 9 and AF=1,
then ALLOW = ALLOW +06 and ALHIGH = 0 and AH=AH+1

(3) If ALLOW > 9, then AL=AL+06H and ALHIGH = 0 and


AH=AH+1, ACF=CF= 1

8086 Instruction Set (continued...) 1-May-23 28


DAA Decimal Adjust Accumulator
• Executed after adding two packed BCD numbers
• Converts packed BCD numbers to unpacked BCD no’s
• Result has to be in AL
AX AH (8 bits) AL (8 bits)

(1) If ALLOW > 9 ALHIGH ALLOW


OR
if AF=1, then DAA adds 06H to AL
(2) After adding 06H to AL, if ALHIGH > 9
OR
if CF=1, then DAA adds 60H to AL.

8086 Instruction Set (continued...) 1-May-23 29


1. Data Copy / Transfer Instructions
2. Arithmetic Instructions
3. Logical Instructions
4. Shift and Rotate Instructions
5. String Manipulation Instructions
6. Branch and Loop Instructions
5. Flag Manipulation Instructions
6. Machine Control Instructions

8086 Instruction Set (continued...) 1-May-23 30


3. LOGICAL INSTRUCTIONS

Logical Instructions:
NOT Logical Invert: Invert each bit of a byte or word
in another byte or word
AND Logical AND: AND each bit in a byte or word with
the corresponding bit
OR Logical OR: OR each bit in a byte or word with the
corresponding bit in another byte or word
XOR Logical XOR: Exclusive OR each bit in a byte or
word with the corresponding bit in another byte or
word
TEST Logical Compare: AND operation to update flags,
[OF, CF, SF, ZF, PF] but don’t change operands

8086 Instruction Set (continued...) 1-May-23 31


3. LOGICAL INSTRUCTIONS

Logical Instructions:
NOT NOT AX;
NOT [5000H];
AND AND AX, 0008H;
AND AX, BX;
AND [5000H], DX;
OR OR AX, 0008H;
OR AX, BX;
OR DX, [5000H];
XOR XOR AX, 0098H; XOR AX, BX; XOR AX, [5000H];
TEST TEST AX, BX; TEST [5000H] 06H;
TEST [BX][DI], CX

8086 Instruction Set (continued...) 1-May-23 32


1. Data Copy / Transfer Instructions
2. Arithmetic Instructions
3. Logical Instructions
4. Shift and Rotate Instructions
5. String Manipulation Instructions
6. Branch and Loop Instructions
5. Flag Manipulation Instructions
6. Machine Control Instructions

8086 Instruction Set (continued...) 1-May-23 33


4. SHIFT AND ROTATE INSTRUCTIONS
SHIFT INSTRUCTIONS

Shift Instructions (count is either 1 or specified by CL)


SHL/ Shift Left / Shift Arithmetic Left:
SAL Shift bits of byte or word left, put zero(s) in LSB(s).
SHR Shift Right:
Shift bits of byte or word right, put zero(s) in MSB(s).
SAR Shift Arithmetic Right:
Shift bits of word or byte right, copy old MSB into new MSB

8086 Instruction Set (continued...) 1-May-23 34


SHL/ SAL Shift bits of byte or word left, put zero(s) in LSB(s)
1. All flags are affected

x0 Carry Flag
MSB LSB
SHL / SAL
1st execution 10 1 01 10 01 0 1
0 01

1 Carry Flag
MSB LSB
SHL / SAL
2nd execution 1 0 1 0 0 1 0 0

8086 Instruction Set (continued...) 1-May-23 35


SHR Shift bits of byte or word right, put zero(s) in MSB(s).
1. All flags are affected

Carry Flag
MSB LSB
0 1 1 0 1 0 0 1 x

SHR 1st execution 0 0 1 1 0 1 0 0 1

SHR 2nd execution 0 0 0 1 1 0 1 0 0

8086 Instruction Set (continued...) 1-May-23 36


SAR Shift bits of word or byte right, copy old MSB into
new MSB
1. All flags are affected

Carry Flag
MSB LSB
1 0 1 0 1 0 0 1 x

SHR 1st execution 1 1 0 1 0 1 0 0 1

SHR 2nd execution 1 1 1 0 1 0 1 0 0

8086 Instruction Set (continued...) 1-May-23 37


4. SHIFT AND ROTATE INSTRUCTIONS
ROTATE INSTRUCTIONS

Rotate Instructions (count is either 1 or specified by CL)


ROL Rotate Left without Carry: Rotate bits of byte or word
left, MSB to LSB and to CF
ROR Rotate Right without Carry: Rotate bits of byte or word
right, LSB to MSB and to CF
RCL Rotate Left through Carry: Rotate bits of byte or word
left, MSB to CF and CF to LSB
RCR Rotate Right through Carry: Rotate bits of byte or word
right, LSB to CF and CF to MSB

8086 Instruction Set (continued...) 1-May-23 38


ROL Rotate bits of byte or word left, MSB to LSB and to CF
RCL Rotate bits of byte or word left, MSB to CF and CF to LSB

 ROL vs. RCL


x10 Carry Flag
MSB LSB
ROL 10 01 10 01 01 10 0
1 10
st execution
12nd execution
Carry Flag
1
0
x
MSB LSB
RCL
01 10 01 01 1
0 x10 0x1 10x
321rd
nd
st execution
execution

8086 Instruction Set (continued...) 1-May-23 39


1. Data Copy / Transfer Instructions
2. Arithmetic Instructions
3. Logical Instructions
4. Shift and Rotate Instructions
5. String Manipulation Instructions
6. Branch and Loop Instructions
5. Flag Manipulation Instructions
6. Machine Control Instructions

8086 Instruction Set (continued...) 1-May-23 40


5. STRING MANIPULATION INSTRUCTIONS
String Instructions
REP An instruction prefix. Repeat following
instruction until CX=0
REPE/REPZ Repeat while equal/zero
REPNE/REPNZ Repeat while not equal/zero
MOVX/MOVSB/ Move byte or word from one string to another
MOVSW
COMPS/COMPSB/ Compare two string bytes or two string words
COMPSW
SCAS/SCASB/ Scan a string. Compare a string byte with a byte
SCASW in AL or a string word with a word in AX
LODS/LODSB/ Load string byte into AL or string word into AX
LODSW
STOS/STOSB/STOSW Store byte from AL or word from AX into string
8086 Instruction Set (continued...) 1-May-23 41
1. Data Copy / Transfer Instructions
2. Arithmetic Instructions
3. Logical Instructions
4. Shift and Rotate Instructions
5. String Manipulation Instructions
6. Branch and Loop Instructions
5. Flag Manipulation Instructions
6. Machine Control Instructions

8086 Instruction Set (continued...) 1-May-23 42


6. BRANCH AND LOOP INSTRUCTIONS

1. Unconditional Branch Instructions


2. Conditional Branch Instructions
3. Loop Instructions

Unconditional Branch Instructions:


CALL Call a procedure (subprogram), save return
address on stack
RET Return from procedure to calling program
JMP Go to specified address to get next instruction

8086 Instruction Set (continued...) 1-May-23 43


Conditional Branch Instructions
JA/JNBE Jump if above/ jump if not below or equal
JAE/JNB Jump if above or equal/ jump if not below
JB/JNAE Jump if below/ jump if not above or equal
JC Jump if carry =1
JBE/JNA Jump if below or equal/ jump if not above
JE/JZ Jump if equal/ jump if zero
JG/JNLE Jump if greater/ jump if not less than or equal
JGE/JNL Jump if greater than or equal/ jump if not less than
JL/JNGE Jump if less than/ jump if not greater than or equal
JLE/JNG Jump if less than or equal/ jump if not greater than
JNC Jump if no carry
JNE/JNZ Jump if not equal/ jump if not zero
8086 Instruction Set (continued...) 1-May-23 44
Conditional Branch Instructions
JNO Jump if no overflow
JNP/JPO Jump if not parity/ jump if parity odd (PF = 0)
JNS Jump if not sign
JO Jump if overflow
JP/JPE Jump if parity/ jump if parity even
JS Jump if sign
Interrupt Instructions:
INT Interrupt program execution, call service
procedure
INTO Interrupt program execution if OF=1
IRET Return from interrupt service procedure to
main program
8086 Instruction Set (continued...) 1-May-23 45
Loop Instructions
LOOP Loop through a sequence of
instructions until CX=0
LOOPE/LOOPZ Loop through a sequence of
instruction while ZF=1 and CX !=0
LOOPNE/LOOPNZ Loop through a sequence of
instruction while ZF=0 and CX!=0.
JCXZ Jump to specified address if CX=0.

8086 Instruction Set (continued...) 1-May-23 46


1. Data Copy / Transfer Instructions
2. Arithmetic Instructions
3. Logical Instructions
4. Shift and Rotate Instructions
5. String Manipulation Instructions
6. Branch and Loop Instructions
7. Flag Manipulation Instructions
8. Machine Control Instructions

8086 Instruction Set (continued...) 1-May-23 47


7. FLAG MANIPULATION INSTRUCTIONS

Flag Manipulation Instructions:


STC Set carry flag
CLC Clear carry flag
CMC Complement the status of carry flag
STD Set direction flag
CLD Clear direction flag
STI Set interrupt enable flag (enable INTR)
CLI Clear interrupt enable flag (disable INTR)

8086 Instruction Set (continued...) 1-May-23 48


1. Data Copy / Transfer Instructions
2. Arithmetic Instructions
3. Logical Instructions
4. Shift and Rotate Instructions
5. String Manipulation Instructions
6. Branch and Loop Instructions
7. Flag Manipulation Instructions
8. Machine Control Instructions

8086 Instruction Set (continued...) 1-May-23 49


8. MACHINE CONTROL INSTRUCTIONS

Machine Control Instructions:


HLT Halt until interrupt or reset
WAIT Wait until signal on the TEST pin is low
LOCK An instruction prefix.
Prevents another processor from taking the bus
while the adjacent instruction executes
NOP No action except fetch and decode

8086 Instruction Set (continued...) 1-May-23 50


OVERVIEW
➢ INTRODUCTION TO 8086
➢ 8086 ARCHITECTURE
➢ ADDRESSING MODES
➢ 8086 INSTRUCTION SET
➢ ASSEMBLER DIRECTIVES
➢ ASSEMBLY LANGUAGE PROGRAMMING
➢ MODULAR PROGRAMMING
➢ STACKS
➢ PROCEDURES
➢ MACROS
➢ INTERRUPTS AND INTERRUPT SERVICE ROUTINES
➢ BYTE AND STRING PROGRAMMING
1-May-23 51
ASSEMBLER DIRECTIVES

1-May-23 52
ASSEMBLER DIRECTIVES &
OPERATORS
Hints given to the assembler using predefined alphabetical
strings to correctly understand the assembly language
programs and prepare machine codes – called as
ASSEBLER DIRECTIVES

Hints given to the assembler


1. to assign a particular constant with a label or
2. Initialize particular memory locations or labels with
constants - Called as OPERATORS
- OPERATORS perform arithmetic and logical tasks.

1-May-23 53
Assembler Directives (continued...)
ASSEMBLER DIRECTIVES USED IN
MICROSOFT MACRO ASSEMBLER OR TURBO ASSEMBLER

DIRECTIVES
1.DB Define Byte
2. DW Define Word
3. DQ Define Quadword
4. DT Define Ten Bytes
5. END End of Program
6. ENDP End of Procedure
7. ENDS End of Segment
8. ASSUME Assume Logical Segment Name
9. SEGMENT Logical segment

1-May-23 54
Assembler Directives (continued...)
DIRECTIVES (continued….)
10. EQU Equate
11. EVEN Align on even memory address
12. EXTRN and External and Public
PUBLIC
13. LOCAL Local
14. GLOBAL Global
15. GROUP Group the related segments
16. LABEL Label
17. LENGTH Byte length of a label
18. NAME Logical name of a module
19. ORG Origin
20. PROC Procedure
1-May-23 55
Assembler Directives (continued...)
OPERATORS USED IN
MICROSOFT MACRO ASSEMBLER OR TURBO ASSEMBLER
DIRECTIVES (continued….)
21. FAR PTR Near pointer
22. NEAR PTR Far pointer
OPERATORS
1.OFFSET Offset of a label
2. PTR Pointer
3. SEG Segment of a label
4. SHORT Short
5. ‘+ and -’ Addition and subtraction operators
6. TYPE Type

1-May-23 56
Assembler Directives (continued...)
DIRECTIVES
1.DB DB directive is used to reserve bytes or bytes
of memory location , with data type that
may be a
-Constant
-Variable
-String etc

Example - 1
PRICE DB 49h, 98h, 29h ; Directs assembler to -
reserve an array of 3 bytes named as PRICE and initialize.

1-May-23 57
Assembler Directives (continued...)
Example - 2
MESSAGE DB ‘GOOD MORNING’;
Reserve an array of bytes equal to the number of
characters in the string named as MESSAGE and
initialize by the ASCII equivalent of these characters

Example – 3
TEMP DB 100 DUP(?) ;
Set 100 bytes of storage in memory and give it the name
as TEMP, but leave the 100 bytes uninitialized.
Program instructions will load values into
these locations.
DUP - Duplicates

1-May-23 58
Assembler Directives (continued...)
DIRECTIVES
2. DW DW directive is used to reserve words (16
bits) or words of memory location , with
data type that may be a
-Constant
-Variable
-String etc

Example - 1
WORDS DW 4987h, 1F98h, AB29h ;
Directs assembler to reserve an array of 3 Words named as
WORDS and initialize.
Lower bytes stored in lower and upper bytes stored in higher
addresses
1-May-23 59
Assembler Directives (continued...)
Example - 2
STAR DW 5 DUP (1234H);
Directs assembler to reserve an array of 5 Words
named as STAR and initializes all the locations with
Lower bytes stored in lower and upper bytes stored in
higher addresses

1-May-23 60
Assembler Directives (continued...)
DIRECTIVES
3. DQ DW directive is used to reserve 4 word bytes
(i.e 4 x 8 bits) of memory location , with data
type that may be a
-Constant
-Variable
-String etc; and may be initialized

DIRECTIVES
4. DT DW directive is used to reserve 10 bytes (i.e
10 x 8 bits) of memory location , with data
type that may be a
-Constant ; Variable; String etc
and may be initialized
- Generally processed by numerical
processors 1-May-23 61
Assembler Directives (continued...)
DIRECTIVES
5. END END of program directive marks the end of an
assembly language program (ALP).
-Ignores all source lines available after this line
-Must be used as the last statement in the file
6. ENDP END of procedure directive marks the end of
procedure.
1. In an ALP - subroutines are called procedure.
2. A procedure is an independent program
module
3. - PROCEDURE STAR
-
-
STAR ENDP

1-May-23 62
Assembler Directives (continued...)
DIRECTIVES
7. ENDS END of Segment directive - marks the end
of logical segment.
-Logical segments are assigned with names
using ASSUME directive
-Must be used the last statement in the
segment along with segment name as prefix
8. ASSUME -This directive is used to assign name for
logical segments
-ASSUME directive is a MUST at the
beginning of each ALP.
9. SEGMENT -Marks the starting of logical segment
-Also assigns a name to the logical segment

1-May-23 63
Assembler Directives (continued...)
EXAMPLE
ASSUME CS : CODE, DS : DATA
DATA SEGMENT
STAR DB 20 DUP (?)
:
:
DATA ENDS
CODE SEGMENT
:
:
:
CODE ENDS
END

1-May-23 64
Assembler Directives (continued...)
DIRECTIVES
10. EQU EQU directive is used to assign a label with
a value or symbol
-Used to reduce recurrence of numerical
values or constants in ALP code
-One change in the EQU statement will give
correct and modified code.
-When assembler comes across the label, it
substitutes the numerical values

EXAMPLE
CONST1 EQU 0510H assigns constant 0510h with
label CONST1
ADDITION EQU ADD assigns another label
ADDITION with mnemonic ADD

1-May-23 65
Assembler Directives (continued...)
DIRECTIVES
11. EVEN Align on even memory address directive updates
the location counter to next even address.
- While starting the assembling process, the
assembler initializes a location counter and
updates it sequentially to the program variables;
constants ; modules etc. as required.

EXAMPLE
EVEN
PROCEDURE ROOT
:
:
ROOT ENDP

1-May-23 66
Assembler Directives (continued...)
DIRECTIVES
12. EXTERN • EXTERN directive informs the assembler
and that the names / procedures / labels declared
PUBLIC after this directive have already been defined
in some other ALP module
• In the module where the names,
procedures, labels actually appear must be
declared PUBLIC

MODULE1 SEGMENT
EXAMPLE
PUBLIC FACTORIAL FAR
MODULE1 ENDS
MODULE2 SEGMENT
EXTRN FACTORIAL FAR
MODULE1 ENDS
1-May-23 67
Assembler Directives (continued...)
DIRECTIVES
13. LOCAL • LOCAL directive informs the assembler
that the labels / variables /constants /
procedures declared after this directive are
used ONLY by that particular module
• In some other module same labels /
variables /constants / procedures can be used
for different purposes.
• Single statement can be used to declare all
labels / variables /constants / procedures

EXAMPLE LOCAL a, b, DATA, ARRAY, ROUTINE

1-May-23 68
Assembler Directives (continued...)
DIRECTIVES
14. GLOBAL • LOCAL directive informs the assembler
that the labels / variables /constants /
procedures declared after this directive can
be used by OTHER modules in the program

EXAMPLE

ROUTINE PROC GLOBAL


Procedure ROUTINE is declared as a global label

1-May-23 69
Assembler Directives (continued...)
DIRECTIVES
15. GROUP • Forms logical groups of segments with
similar purpose or types
• Assembler informs loader/linker to place
group declared segments or operands must
lie within 64KB memory segment

EXAMPLE

PROGRAM GROUP CODE, DATA, STACK


CODE, DATA, STACK segments lie within 64 KB which
is named as PROGRAM

1-May-23 70
Assembler Directives (continued...)
DIRECTIVES
16. LABEL • Used to assign a name to the current
content of the location counter
•The type of the label must be specified (i.e)
NEAR or FAR label, BYTE or WORD label

EXAMPLE After reserving 50


locations for DATAS,
DATA SEGMENT the next location will
DATAS DB 50H DUP (?) be assigned a label
DATA-LAST LABEL BYTE FAR DATA-LAST and its
type will be Byte and
DATA ENDS Far

1-May-23 71
Assembler Directives (continued...)
DIRECTIVES
17. LENGTH Used to refer to the length of a data array or
string
EXAMPLE MOV CX, LENGTH ARRAY
Substitutes the length of the array ARRAY in bytes

DIRECTIVES
18. NAME Used to assign a name to an ALP module

MULTI_PGM NAME
:
:
:
END
1-May-23 72
Assembler Directives (continued...)
DIRECTIVES
19. ORG Directs the assembler to start the memory
allotment for the particular block or code from
the declared address in the ORG statement
Without ORG location counter is initialized to
0000
EXAMPLE ORG 200H
Initializes the location counter to the address 0200h instead
of 0000h

1-May-23 73
Assembler Directives (continued...)
DIRECTIVES
20. PROC Marks the start of a named procedure

NEAR / FAR specify the type of the procedure

NEAR- called by the main program located


within 64K of physical memory
FAR – Called by the main program located at a
more than 64K of physical memory

EXAMPLE RESULT PROC NEAR


ROUTINE PROC FAR
Procedure RESULT is placed in the same segment
Procedure ROUTINE is placed in some other segment

1-May-23 74
Assembler Directives (continued...)
DIRECTIVES
21. FAR PTR • Indicates the assembler that the label
following FAR PTR is not within the
same segment
• Address of label is of 32 bits (4 bytes)
[2 bytes offset followed by 2 bytes
segment address]

EXAMPLE JMP FAR PTR LABEL1


CALL FAR PTR ROUTINE

1-May-23 75
Assembler Directives (continued...)
DIRECTIVES
22. NEAR PTR • Indicates the assembler that the label
following FAR PTR is in the same
segment
• Address of label is of 16 bits (2 bytes)
[2 bytes offset only]

EXAMPLE JMP NEAR PTR LABEL1


CALL NEAR PTR ROUTINE

1-May-23 76
Assembler Directives (continued...)
OPERATORS
1. OFFSET OFFSET operator indicates the assembler
to compute the16 bit displacement (offset)
of the label and replace the string ‘OFFSET
LABEL’ by the computed displacement
Used with arrays, strings, labels and procedures to
decide their offset in their default segments

EXAMPLE CODE SEGMENT


MOV SI, OFFSET LIST
CODE ENDS
DATA SEGMENT
LIST DB 10H
DATA ENDS
1-May-23 77
Assembler Directives (continued...)
OPERATORS
2. PTR - PTR operator is used to declare the type of a
label, variable or memory operand.
- PTR operator is prefixed by BYTE or WORD
EXAMPLE
MOV AL, BYTE PTR [SI] - Moves content of memory
location addressed by SI to AL
INC BYTE PTR [BX] - Increments byte content of memory
location addressed by BX
MOV BX, WORD PTR [2000H] - Moves 16 bit content of
memory location 2000h to BX (i.e) [2000h] to BL, [2001h] to
BH
INC WORD PTR [2000H] - Increments word content of
memory location 3000h and 3001h as a 16 bit number

1-May-23 78
Assembler Directives (continued...)
OPERATORS
3. SEG SEG operator is used to decide the segment
address of the label, variable or procedure and
substitutes the segment base address in place of
‘SEG’ label

EXAMPLE
MOV AX, SEG ARRAY
MOV DS, AX

Moves the segment address of ARRAY in which it is


appearing into the register AX and then into DS

1-May-23 79
Assembler Directives (continued...)
OPERATORS
4. SHORT SHORT operator indicates to the assembler
that only one byte is required to code the
displacement for a jump
By specifying this way the assembler saves
memory.
EXAMPLE JMP SHORT LABEL
OPERATORS
5. +, - Represents arithmetic addition and subtration

MOV AL, [SI +2]


MOV DX, [BX -5]
MOV BX, [OFFSET LABEL +10H]
1-May-23 80
Assembler Directives (continued...)
OPERATORS
6. TYPE TYPE operator directs the assembler to decide
the data type of the specified label and replace
the ‘TYPE’ label by the decided data type.

For byte type variable, the data type is 1;


For word type variable, the data type is 2;
For double word type, the data type is 4;
EXAMPLE MOV AX, TYPE STRING
If STRING is a word array, then the value 0002h is
moved in AX

1-May-23 81
Assembler Directives (continued...)

You might also like