microprocessors & interfacing (a1423 unit ii 8086 … · department of electronics and...
TRANSCRIPT
2/20/2015
1
Department of Electronics and Communication Engineering
MICROPROCESSORS & INTERFACING (A1423)UNIT – II 8086 ASSEMBLY LANGUAGE PROGRAMMING
VARDHAMAN COLLEGE OF ENGINEERING(AUTONOMOUS)
Shamshabad, Hyderabad - 501218
Microprocessors & Interfacing (A1423)
UNIT-II
2
8086 Family Assemble Language Programming
8086 Instruction Set
Simple Programs
Assembly language programs involving
1. logical, branch and call instructions
2. Sorting, evaluation of arithmetic expressions
3. String manipulations
Assembler directives
Procedures
Macros
2/20/2015
2
Microprocessors & Interfacing (A1423)
Machine Language Programming
3
One way to write a program is to assign a fixed binary
pattern for each instruction and represent the program
by sequencing these binary patterns.
Such a program is called a Machine language
program or an object program
Ex:
0011 1110 ; LOAD AL Register with
0000 0101 ; Value 5
Microprocessors & Interfacing (A1423)
Assembly Language Programming
4
Use of symbols in programming to improve the
readability.
Giving symbolic names for each instruction.
These names are called mnemonics and a program
written using these symbols are called as Assembly
Language Program.
Ex:
MOV AL, 05H ; load AL register with 5
2/20/2015
3
Microprocessors & Interfacing (A1423)
Features of ALP
C.LOKANATH REDDY5
For using Assembly language, the programmer needs
to know the internal Architecture of the Microprocessor.
The Assembly language written for one processor will
not usually run on other processors.
An assembly language program cannot be executed by
a machine directly, as it not in a binary form.
Microprocessors & Interfacing (A1423)
Features of ALP cont..
6
An Assembler is needed in order to translate an
assembly language (source program) into the object
code executable by the machine.
Unlike the other programming languages, assembly
language is not a single language, but rather a group
of languages.
Each processor family (and sometimes individual
processors within a processor family) has its own
assembly language.
2/20/2015
4
Microprocessors & Interfacing (A1423)
Format of Assembly Program
7
LABEL OPCODE MNEMONICS OPERAND COMMENT
Start B8,0010 MOV AX,0010H Move 0010H into AX
Labels :Begins from column1. Should start with a
letter. After the letter a combination of letters and
numbers may be used. Restricted by most
assemblers from 6 to 8 characters. Use is to identify
opcode and operands. Are needed on executable
statements, so that the statement can be branched to.
Microprocessors & Interfacing (A1423)
Format of Assembly Program cont..
8
Opcode: It indicates the type of operation to be performed
by the CPU.
Mnemonics: Symbolic abbreviations for operation codes or
comment to the Assembler. Specifies how data are to be
manipulated, which is residing within a microprocessor
register or in the main memory. Ex: MOV, LD, ADD
Operand: use to specify the addresses or registers used
by the operands by the Machine instructions. Operands
may be Registers, memory location, constants etc.
Comments: prefixed by ; for documentation
2/20/2015
5
Microprocessors & Interfacing (A1423)
Simple Program
9
Write a program to add a data byte located at offset 0500H
in 2000H segment to another data byte located at 0600H
in the same segment and store the result at 0700H in the
same segment. START
Initialize Seg. Reg
Get content of 0500H in a G.P. register
Perform addition
Store result in 0700H
STOP
Microprocessors & Interfacing (A1423)
Simple Program cont..
10
MOV AX, 2000H; Initializing DS with value 2000H
MOV DS, AX
MOV AX, [500H]; Get first byte from 0500H offset
ADD AX, [600H]; Add this to the second byte from
0600H
MOV [700H], AX; Store AX in 0700H (result)
HLT ; Stop
2/20/2015
6
Microprocessors & Interfacing (A1423)
Instruction Set of 8086
11
An instruction is a binary pattern designed inside a
microprocessor to perform a specific function.
The entire group of instructions that a microprocessor
supports is called Instruction Set.
8086 has more than 20,000 instructions.
There are 117 basic instructions in the instruction set
of 8086
Microprocessors & Interfacing (A1423)
Classification of Instruction Set
12
Data Transfer Instructions
Arithmetic Instructions
Bit Manipulation Instructions
Program Execution Transfer Instructions
String Instructions
Processor Control Instructions
2/20/2015
7
Microprocessors & Interfacing (A1423)
Data Transfer Instructions
13
General purpose byte or word transfer instructions
1. MOV: copy byte or word from specified source to specified destination.
Syntax: MOV Destination, Source
Source operand can be register, memory location or immediate operand.
Destination can be register or memory location.
Both Source and Destination cannot be memory location at the same time.
Microprocessors & Interfacing (A1423)
Data Transfer Instructions cont..
14
General purpose byte or word transfer instructions
1. MOV: copy byte or word from specified source to
specified destination.
EX:
MOV CX, 037AH
MOV BL, [437AH]
MOV AX,BX
MOV DL, [BX]
2/20/2015
8
Microprocessors & Interfacing (A1423)
Data Transfer Instructions cont..
15
General purpose byte or word transfer instructions
2. PUSH: copy word to top of stack.
The PUSH instruction decrements the stack pointer by 2 and copies a word from a specified source to the location in the stack segment where the stack pointer then points
EX:
PUSH BX
PUSH DS
PUSH AL; illegal, must push a word
Microprocessors & Interfacing (A1423)
Data Transfer Instructions cont..
C.LOKANATH REDDY16
General purpose byte or word transfer instructions
3. POP: copy word from top of stack to specified
location.
EX:
POP BX
POP DS
2/20/2015
9
Microprocessors & Interfacing (A1423)
Data Transfer Instructions cont..
17
General purpose byte or word transfer instructions
4. XCHG: Exchange bytes or exchange words.
* The segment registers cannot be used in this
instruction.
EX:
XCHG AX, DX
XCHG BL, CH
Microprocessors & Interfacing (A1423)
Data Transfer Instructions cont..
18
Simple input and output port transfer instructions
5. IN: Copy a byte or word from specified port to
accumulator .
EX:
IN AL, 0C8H
IN AX, 34H
2/20/2015
10
Microprocessors & Interfacing (A1423)
Data Transfer Instructions cont..
19
Simple input and output port transfer instructions
6. OUT: Copy a byte or word from accumulator to
specified port .
EX:
OUT 0C8H, AL
OUT 34H, AX
Microprocessors & Interfacing (A1423)
Data Transfer Instructions cont..
20
Special address transfer instructions
7. LEA: Load effective address of operand into
specified register.
It loads a 16-bit register with the offset address of the
data specified by the Source.
EX:
LEA BX, [DI]
This instruction loads the contents of DI (offset) into the BX register.
2/20/2015
11
Microprocessors & Interfacing (A1423)
Data Transfer Instructions cont..
C.LOKANATH REDDY21
Special address transfer instructions
8. LDS: Load DS register and another specified register from
memory.
It loads 32-bit pointer from memory source to destination
register and DS
EX: LDS BX, [4326]
Copy contents of memory at displacement 4326H in DS to BL,
contents of 4327H to BH. Copy contents at displacement of 4328H
and 4329H in DS to DS register.
Microprocessors & Interfacing (A1423)
Data Transfer Instructions cont..
22
Special address transfer instructions
9. LES: Load ES register and another specified register
from memory.
It loads 32-bit pointer from memory source to destination
register and ES
EX: LES BX, [4326]
Copy contents of memory at displacement 4326H in DS to BL,
contents of 4327H to BH. Copy contents at displacement of
4328H and 4329H in DS to ES register.
2/20/2015
12
Microprocessors & Interfacing (A1423)
Data Transfer Instructions cont..
23
Flag transfer instructions
10. LAHF: It copies the lower byte of flag
register to AH.
11. SAHF: It copies the contents of AH to lower
byte of flag register.
12. PUSHF: Pushes flag register to top of stack.
13. POPF: Pops the stack top to flag register.
Microprocessors & Interfacing (A1423)
ARITHMETIC INSTRUCTIONS
24
Addition instructions
Subtraction instructions
Multiplication instructions
Division instructions
2/20/2015
13
Microprocessors & Interfacing (A1423)
Addition instructions
25
ADD : Add specified byte to byte or specified word to word.
This instruction add a number from source to number from
destination and put the result in the specified destination.
The source may be an immediate number, a register or a
memory location specified by any of the addressing modes.
The destination may be a register or a memory location
specified by any of the addressing modes.
The source and destination in an instruction cannot both be
memory locations.
Ex: ADD AL, 74H
Flags affected: AF, CF, OF, PF, SF, ZF.
Microprocessors & Interfacing (A1423)
Addition instructions Cont..
26
ADC : Add byte + byte + carry flag
Add word + word + carry flag
This instruction add a number from source to number
from destination and put the result in the specified
destination. The add with carry also adds the status of
carry flag into the result
Ex: ADC CL, BL; Add contents of BL plus carry status
to content of CL
Flags affected: AF, CF, OF, PF, SF, ZF.
2/20/2015
14
Microprocessors & Interfacing (A1423)
Addition instructions Cont..
27
INC : Increment specified byte or specified word by 1
The INC instruction adds 1 to a specified register or to
a memory location
Ex: INC CL
Flags affected: AF, OF, PF, SF, ZF.
Carry flag is not affected i.e. if an 8-bit destination
containing FFH or a 16-bit destination containing
FFFFH is incremented, the result will be all 0‟s with no
carry
Microprocessors & Interfacing (A1423)
Addition instructions Cont..
28
AAA : ASCII adjust after addition
Numerical data coming into a computer from a
terminal is usually in ASCII code. In this code the
numbers 0 to 9 are represented by the ASCII codes
30H to 39H.
The 8086 allows to add the ASCII codes for two
decimal digits without masking off the “3” in the upper
nibble of each.
After addition, the AAA instruction is used to make
sure the result is the correct unpacked BCD
2/20/2015
15
Microprocessors & Interfacing (A1423)
Addition instructions Cont..
29
AAA : ASCII adjust after addition
Ex: assume AL=0011 0101, ASCII 5
BL=0011 1001, ASCII 9
ADD AL,BL; Result: AL=0110 1110= 6EH, in
correct BCD
AAA ; Now AL=00000100, unpacked BCD
4.
;CF=1 indicates answer is 14 decimal
Flags affected: AF, CF.
The AAA instruction works only on the AL register.
Microprocessors & Interfacing (A1423)
Addition instructions Cont..
30
DAA : Decimal (BCD) adjust after addition
This instruction is used to make sure the result of
adding two packed BCD numbers is adjusted to
be a legal BCD number.
The result of the addition must be in AL for DAA
to work correctly.
If the lower nibble in AL after an addition is
greater than 9 or AF was set by addition, then the
DAA instruction will add 6 to lower nibble in AL.
2/20/2015
16
Microprocessors & Interfacing (A1423)
Addition instructions Cont..
31
DAA : Decimal (BCD) adjust after addition
If the result upper nibble of AL is greater than 9 or CF
was set by addition, then the DAA instruction will add
60H to AL.
Ex: assume AL=0101 1001, BCD 59
BL=0011 0101, BCD 35
ADD AL,BL; Result: AL=1000 1110= 8EH
DAA ;add 0110 because 1110>9
;AL=1001 0100= 94 BCD
Flags affected: AF, PF, ZF, CF.
Microprocessors & Interfacing (A1423)
Subtraction instructions
C.LOKANATH REDDY32
SUB : Subtract byte from byte or word from word.
For subtraction the carry flag (CF) functions as borrow
flag.
The source and the destination must both be of type
byte or both be of type word.
Ex: SUB CX, BX; CX-BX. Result in CX
Flags affected: AF, CF, OF, PF, SF, ZF.
2/20/2015
17
Microprocessors & Interfacing (A1423)
Subtraction instructions Cont..
33
SBB : Subtract byte and carry flag from byte or word
and carry flag from word.
Ex: SBB CH, AL; subtract contents of AL and
;contents of CF from
;contents of CH. Result in CH
Flags affected: AF, CF, OF, PF, SF, ZF.
Microprocessors & Interfacing (A1423)
Subtraction instructions Cont..
34
DEC : Decrement specified byte or specified word by 1
Ex: DEC CL
Flags affected: AF, OF, PF, SF, ZF.
Carry flag is not affected i.e. if an 8-bit destination
containing 00H or a 16-bit destination containing
0000H is decremented, the result will be all FFH or
FFFFH with no carry(borrow).
2/20/2015
18
Microprocessors & Interfacing (A1423)
Subtraction instructions Cont..
35
NEG : Negate– invert each bit of a specified byte or
word and add 1 (forms 2‟s complement).
Ex: NEG AL; Replace number in AL with its 2‟s
complement
Flags affected: AF, CF, OF, PF, SF, ZF.
Microprocessors & Interfacing (A1423)
Subtraction instructions Cont..
36
CMP : Compare two specified bytes or two specified
words.
The comparison is actually done by subtracting the
source byte or word from destination byte or word.
The source and the destination are not changed but
the flags are set to indicate the result of comparison.
Ex: CMP CX, BX
Flags affected: AF, CF, OF, PF, SF, ZF.
2/20/2015
19
Microprocessors & Interfacing (A1423)
Subtraction instructions Cont..
37
CMP : Compare two specified bytes or two specified
Words.
For the instruction CMP CX, BX; CF, ZF, SF are left as
follows
Comparison CF ZF SF comment
CX = BX 0 1 0 Result of subtraction is zero
CX > BX 0 0 0 No borrow is required, so CF = 0
CX < BX 1 0 1 Subtraction required borrow, so CF = 1
Microprocessors & Interfacing (A1423)
Subtraction instructions Cont..
38
AAS : ASCII adjust after subtraction
After subtraction, the AAS instruction is used to make
sure the result is the correct unpacked BCD
Flags affected: AF, CF
2/20/2015
20
Microprocessors & Interfacing (A1423)
Subtraction instructions Cont..
39
DAS : Decimal (BCD) adjust after subtraction
This instruction is used to make sure the result of
subtracting two packed BCD numbers is adjusted to
be a legal BCD number.
The result of the subtraction must be in AL for DAS to
work correctly.
If the lower nibble in AL after subtraction is greater
than 9 or AF was set by subtraction, then the DAS
instruction will subtract 6 from lower nibble in AL.
Microprocessors & Interfacing (A1423)
Multiplication instructions
40
MUL : Multiply unsigned byte by byte or unsigned
word by word.
This instruction multiplies an unsigned byte from some
source times an unsigned byte in AL register or an
unsigned word from some source times an unsigned word
in AX
When a byte is multiplied by the contents of AL, the result
(product) is put in AX. A 16-bit destination is required
because the result of multiplying an 8-bit number by an 8-
bit number can be as large as 16 bit.
The MSB of result is put in AH, LSB of result is put in AL
2/20/2015
21
Microprocessors & Interfacing (A1423)
Multiplication instructions Cont..
41
When a word is multiplied by the contents of AX, the
result (product) can be as large as 32 bit.
The MSB of result is put in DX, LSB of result is put in
AX
Ex: MUL CX ; AX times CX, result high word in DX
;low word in AX
Flags affected: CF, OF.
Microprocessors & Interfacing (A1423)
Multiplication instructions Cont..
42
IMUL : Multiply signed byte by byte or signed word by
word.
This instruction multiplies an signed byte from some
source times an signed byte in AL register or an
unsigned word from some source times an unsigned
word in AX.
Ex: IMUL AX ; AX times AX, result high word in DX
;low word in AX
Flags affected: CF, OF.
2/20/2015
22
Microprocessors & Interfacing (A1423)
Multiplication instructions Cont..
C.LOKANATH REDDY43
AAM : ASCII adjust after multiplication.
Before multiply of two ASCII digits, first the upper 4
bits of each digit must be masked. This leaves
unpacked BCD (one BCD digit per byte) in each byte
After the two unpacked BCD digits are multiplied, the
AAM instruction is used to adjust the product to two
unpacked BCD digits in AX
Microprocessors & Interfacing (A1423)
Multiplication instructions Cont..
44
AAM : ASCII adjust after multiplication.
Ex: assume AL=0011 0101, ASCII 5
BL=0011 1001, ASCII 9
MUL BL; AL× BL; Result in AX
AX=00000000 00101101= 002DH,
AAM ; Now AX=00000100 00000101= 0405H,
Which is unpacked BCD for 45.
If ASCII codes for the result are desired, put 3 in upper
nibble of each byte. i.e. 3435H (ASCII code for 45)
Flags affected: PF, ZF, SF.
2/20/2015
23
Microprocessors & Interfacing (A1423)
Division instructions
45
DIV : Divide unsigned word by byte or unsigned double
word
by word.
When a word is divided by a byte, the word must be in
the AX register. The divisor can be in register or a
memory location.
After division, AL will contain an 8 bit result (quotient)
and AH will contain an 8-bit remainder
For a DIV, the dividend (numerator) must always be in
AX or DX and AX
Ex: DIV CX
Microprocessors & Interfacing (A1423)
Division instructions Cont..
46
IDIV : Divide signed word by byte or signed double
word by word.
Ex: IDIV BL
2/20/2015
24
Microprocessors & Interfacing (A1423)
Division instructions Cont..
47
AAD : ASCII adjust before division
AAD converts two unpacked BCD digits in AH and
AL to equivalent binary number in AL. This
adjustment must be made before dividing two
unpacked BCD digits in AX by an unpacked BCD
byte.
Ex:
Microprocessors & Interfacing (A1423)
Division instructions Cont..
48
AAD : ASCII adjust before division
Ex: assume AX=0607H unpacked BCD for 67
CH=09H, now adjust to binary
AAD; Result: AX=0043=43H=67 decimal
DIV CH ; Quotient: AL= 07H,
; Remainder: AH=04
2/20/2015
25
Microprocessors & Interfacing (A1423)
Division instructions Cont..
49
CBW : Convert Signed Byte to Signed Word.
Fill upper byte of word with copies of sign bit of lower
byte.
Ex: assume AX= 00000000 10011011=-155 decimal
CBW; Result: AX= 11111111 10011011=-155
Microprocessors & Interfacing (A1423)
Division instructions Cont..
50
CWD : Convert Signed Word to Signed Double word.
Fill upper word of double word with copies of sign bit
of lower word.
Ex: assume DX=00000000 00000000
AX= 11110000 11000111=-3897 decimal
CWD; Result: DX=11111111 11111111
AX= 11110000 11000111=-3897 decimal
2/20/2015
26
Microprocessors & Interfacing (A1423)
BIT MANIPULATION INSTRUCTIONS
51
Logical Instructions
Shift Instructions
Rotate Instructions
Microprocessors & Interfacing (A1423)
Logical Instructions
52
NOT : Invert each bit of a byte or word.
Ex: NOT BX
Flags affected: NO.
2/20/2015
27
Microprocessors & Interfacing (A1423)
Logical Instructions Cont..
53
AND : AND each bit in a byte or word with the
corresponding bit in another byte or word.
The result for each bit position will follow the truth table for
a two-input AND gate.
Ex: AND BH,CL
Flags affected: PF, SF, ZF.
CF and OF are both 0‟s after operation
PF has meaning only for the lower 8 bits of the destination.
AF will be undefined.
Microprocessors & Interfacing (A1423)
Logical Instructions Cont..
54
OR : OR each bit in a byte or word with the
corresponding bit in another byte or word.
The result for each bit position will follow the truth table for
a two-input OR gate.
Ex: OR BH,CL
Flags affected: PF, SF, ZF.
CF and OF are both 0‟s after operation
PF has meaning only for the lower 8 bits of the destination.
AF will be undefined.
2/20/2015
28
Microprocessors & Interfacing (A1423)
Logical Instructions Cont..
55
XOR : XOR each bit in a byte or word with the
corresponding bit in another byte or word.
The result for each bit position will follow the truth table for
a two-input Exclusive OR gate.
Ex: XOR BH,CL
Flags affected: PF, SF, ZF.
CF and OF are both 0‟s after operation
PF has meaning only for the lower 8 bits of the destination.
AF will be undefined.
Microprocessors & Interfacing (A1423)
Logical Instructions Cont..
56
TEST: AND operands to update flags, but don‟t
change operands.
The TEST instruction is often used to set flags before a
conditional jump instruction.
Ex: TEST AH,CL
Flags affected: PF, SF, ZF.
CF and OF are both 0‟s after operation
PF has meaning only for the lower 8 bits of the destination.
AF will be undefined.
2/20/2015
29
Microprocessors & Interfacing (A1423)
Shift Instructions
57
SAL/SHL: Shift bits of word or byte left, put zeros in
LSB.
The MSB will be shifted into CF
Ex: MOV CL, 02H
SAL BP,CL
Flags affected: PF, SF, ZF.
For multiple shifts OF is undefined.
PF has meaning only for the lower 8 bits of the
destination.
AF will be undefined.
Microprocessors & Interfacing (A1423)
Shift Instructions Cont..
58
SHR: Shift bits of word or byte right, put zeros in
MSB.
The bit shifted out of LSB will be shifted into CF
Ex: MOV CL, 02H
SHR BP,CL
Flags affected: PF, SF, ZF.
For multiple shifts OF is undefined.
PF has meaning only for the lower 8 bits of the
destination.
AF will be undefined.
2/20/2015
30
Microprocessors & Interfacing (A1423)
Shift Instructions Cont..
59
SAR: Shift bits of word or byte right, copy old MSB into
new MSB.
The sign bit is copied into MSB.
The LSB will be shifted into CF
Ex: MOV CL, 02H
SAR BP,CL
Flags affected: PF, SF, ZF.
For multiple shifts OF is undefined.
PF has meaning only for the lower 8 bits of the destination.
AF will be undefined.
Microprocessors & Interfacing (A1423)
Rotate Instructions
60
ROL: Rotate bits of word or byte left, MSB to LSB and
to
CF
The operation can be thought of as circular , because
the data bit is rotated out of MSB is circled back into
the LSB
Ex: MOV CL, 02H
ROL BL,CL
Flags affected: CF, OF.
2/20/2015
31
Microprocessors & Interfacing (A1423)
Rotate Instructions Cont..
61
ROR: Rotate bits of word or byte right, LSB to MSB
and to CF
Ex: MOV CL, 02H
ROR BL,CL
Flags affected: CF, OF.
Microprocessors & Interfacing (A1423)
Rotate Instructions Cont..
62
RCL: Rotate bits of word or byte left, MSB to CF and
CF to LSB
Ex: MOV CL, 02H
RCL BL,CL
Flags affected: CF, OF.
2/20/2015
32
Microprocessors & Interfacing (A1423)
Rotate Instructions Cont..
63
RCR: Rotate bits of word or byte right, LSB to CF and
CF to MSB
Ex: MOV CL, 02H
RCR BL,CL
Flags affected: CF, OF.
Microprocessors & Interfacing (A1423)
STRING INSTRUCTIONS
64
String is a series of bytes or a series of words in
sequential memory locations.
REP INS/INSB/INSW
REPE/REPZ OUTS/OUTSB/OUTSW
REPNE/REPNZ SCAS/SCASB/SCASW
MOVS/MOVSB/MOVSW LODS/LODSB/LODSW
COMPS/COMPSB/COMPSW STOS/STOSB/STOSW
2/20/2015
33
Microprocessors & Interfacing (A1423)
STRING INSTRUCTIONS Cont..
65
REP/REPE/REPZ/REPNE/REPNZ: Repeat String
Instruction
REP is a prefix which is written before one string
instructions. It will cause the CX register to be
decremented and the string instruction to be repeated
until CX=0.
The instruction REP MOVSB, for example will
continue to copy string bytes until the number of bytes
loaded into CX has been copied.
Microprocessors & Interfacing (A1423)
STRING INSTRUCTIONS Cont..
66
REPE and REPZ are two mnemonics for the same prefix.
They stand for Repeat if Equal and Repeat if Zero
respectively.
REPE or REPZ will cause the string instruction to be
repeated as long as the compare bytes or words are equal
(ZF=1) and CX is not yet counted down to zero.
There are two conditions to stop the repetition
1.CX=0
2.Sting bytes or words not equal.
EX: REPE CMPSB
2/20/2015
34
Microprocessors & Interfacing (A1423)
STRING INSTRUCTIONS Cont..
67
REPNE and REPNZ are two mnemonics for the same
prefix.
They stand for Repeat if Not Equal and Repeat if Not Zero
respectively.
REPNE or REPNZ will cause the string instruction to be
repeated until the compare bytes or words are equal
(ZF=1) and CX=0.
EX: REPNE SCASW
Microprocessors & Interfacing (A1423)
STRING INSTRUCTIONS Cont..
68
MOVS/MOVSB/MOVSW- Move String Byte or String
Word
This instruction copies a byte or a word from a location in
the data segment to a location in extra segment.
The offset of the source byte or word in the data segment
must be in the SI register.
The offset of the destination byte or word in the extra
segment must be contained in the DI register.
2/20/2015
35
Microprocessors & Interfacing (A1423)
STRING INSTRUCTIONS Cont..
69
SIMPLE PROGRAM
MOV SI, OFFSET SOURCE_STRING; Load offset of start of
source in DS into SI
MOV DI, OFFSET DESTINATION_STRING; Load offset of
start of destination in ES into DI
CLD; Clear direction flag to auto increment SI & DI after move
MOV CX,04H; Load length of string into CX as counter
REP MOVSB; Decrement CX and copy string bytes until CX=0
Microprocessors & Interfacing (A1423)
STRING INSTRUCTIONS Cont..
70
CMPS/CMPSB/CMPSW- Compare String Bytes or
String Words
The CMPS instruction can be used to compare a byte or
word in one string to a byte or word in another string.
SI is used to hold the offset of a byte or word in the source
string .
DI is used to hold the offset of a byte or word in the other
string .
2/20/2015
36
Microprocessors & Interfacing (A1423)
STRING INSTRUCTIONS Cont..
71
CMPS/CMPSB/CMPSW- Compare String Bytes or
String Words
The comparison is done by subtracting the byte or word
pointed to by DI from the byte or word pointed to by SI
The AF, CF, OF, PF, SF and ZF flags are affected by the
comparison, but neither operand is affected.
The CMPS instruction can be used with REPE or REPNE
as prefix to compare all the elements of a string.
EX: REPE CMPSB
Microprocessors & Interfacing (A1423)
STRING INSTRUCTIONS Cont..
C.LOKANATH REDDY72
SCAS/SCASB/SCASW- Scan a String Byte or String
Word
SCAS compares a byte in AL or a word in AX with a byte or
word pointed to by DI in ES.
The string to be scanned must be in the extra segment and
DI must contain offset of the byte or word to be compared.
SCAS affects AF, CF, OF, PF, SF and ZF flags, but it does
not change either the operand in AL (AX) or the operand in
the string.
2/20/2015
37
Microprocessors & Interfacing (A1423)
STRING INSTRUCTIONS Cont..
73
LODS/LODSB/LODSW- Load a String Byte into AL or
Load a String Word into AX
This instruction copies a byte from a string location pointed
to by SI to AL, or a word from a string location pointed to
by SI to AX.
LODS affects no flags.
EX: LODS SOURCE_STRING
Microprocessors & Interfacing (A1423)
STRING INSTRUCTIONS Cont..
74
STOS/STOSB/STOSW- Store Byte from AL or Word
from AX into String.
This instruction copies a byte from AL or a word from AX to
a memory location in the extra segment pointed to by DI.
STOS affects no flags.
2/20/2015
38
Microprocessors & Interfacing (A1423)
PROGRAM EXECUTION TRANSFER INSTRUCTIONS
75
These instruction are used to tell the 8086 to start
fetching instructions from some new address, rather
than continuing in sequence.
1. Unconditional transfer instructions
2. Conditional transfer instructions
3. Iteration control instructions
4. Interrupt instructions
Microprocessors & Interfacing (A1423)
Unconditional transfer instructions
76
MNEMONIC Description Example
CALL Call a procedure (sub program), save return
address on stack
CALL BX
RET Return from a procedure to calling program. RET
JMP Go to specified address to get next instruction JMP BX
2/20/2015
39
Microprocessors & Interfacing (A1423)
Conditional transfer instructions
77
These instructions are often used after a compare
instruction.
The terms above and below refers to unsigned binary
numbers. Above means larger in magnitude.
The terms greater than or less than refer to signed
binary numbers. Greater means more positive.
Microprocessors & Interfacing (A1423)
Conditional transfer instructions
Cont..
78
MNEMONIC Description Flags
JA/JNBE Jump if above/ Jump if not below or
Equal
CF=0
ZF=0
JAE/ JNB/
JNC
Jump if above or Equal/ Jump if Not
Below/ Jump if No Carry
CF=0
JB/ JC/
JNAE
Jump if below/ Jump if Carry/ Jump if
Not Above or Equal
CF=1
JBE/ JNA Jump if Below or Equal/ Jump if Not
Above
Either ZF=1 or
CF=1
JCXZ Jump if the CX register is Zero
JE/ JZ Jump if Equal/ Jump if Zero ZF=1
JG/ JNLE Jump if Greater/ Jump if Not Less
than or Equal
ZF=0 &
CF=OF
2/20/2015
40
Microprocessors & Interfacing (A1423)
Conditional transfer instructions
Cont..
79
MNEMONIC Description Flags
JGE/ JNL Jump if Greater than or Equal/ Jump
if Not Less than
SF=OF
JL/ JNGE Jump if Less than/ Jump if Not
Greater than or Equal
SF≠ OF
JLE/ JNG Jump if Less than or Equal/ Jump if
Not Greater than
ZF=1 or
SF≠ OF
JNE/ JNZ Jump if Not Equal/ Jump if Not Zero ZF=0
JNO Jump if Not Overflow OF=0
JNP/ JPO Jump if No parity/ Jump if Parity Odd PF=0
JNS Jump if not Signed SF=0
JO Jump if Overflow OF=1
Microprocessors & Interfacing (A1423)
Conditional transfer instructions
Cont..
80
MNEMONIC Description Flags
JP/ JPE Jump if Parity/ Jump if Parity Even PF=1
JS Jump if Signed SF=1
EX: CMP AX, 4371H
JA LABEL
2/20/2015
41
Microprocessors & Interfacing (A1423)
Iteration Control instructions
C.LOKANATH REDDY81
These instructions can be used to execute a series of
instructions some number of times.
MNEMONIC DESCRIPTION
LOOP Loop through a sequence of instructions until
CX=0
LOOPE/ LOOPZ Loop through a sequence of instructions while
ZF=1 and CX≠0
LOOPNE/ LOOPNZ Loop through a sequence of instructions while
ZF=0 and CX≠0
Microprocessors & Interfacing (A1423)
Iteration Control instructions Cont..
82
Sample Program
MOV BX, OFFSET PRICES
MOV CX, 40
NEXT: MOV AL, [BX]
ADD AL, 07H
DAA
MOV [BX], AL
INC BX
LOOP NEXT
2/20/2015
42
Microprocessors & Interfacing (A1423)
Interrupt instructions
83
MNEMONIC DESCRIPTION
INT Interrupt program execution, call service
procedure
INTO Interrupt program execution if OF=1
IRET Return from Interrupt service procedure to
main program
Microprocessors & Interfacing (A1423)
PROCESSOR CONTROL INSTRUCTIONS
84
Flag set/ clear instructions
External hardware synchronization instructions
No operation instruction
2/20/2015
43
Microprocessors & Interfacing (A1423)
Flag set/ clear instructions
85
MNEMONIC Description
STC Set carry flag CF=1
CLC Clear carry flag CF=0
CMC Complement the state of the carry flag CF
STD Set direction flag DF to 1(decrement string pointers)
CLD Clear direction flag DF to 0(increment string pointers)
STI Set interrupt enable flag to 1(enable INTR input)
CLI Clear interrupt enable flag to 0(disable INTR input)
Microprocessors & Interfacing (A1423)
External hardware synchronization instructions
86
MNEMONIC Description
HLT Halt(do nothing) until interrupt or reset
WAIT Wait(do nothing) until signal on the TEST pin is low
ESC Escape to external coprocessor such as 8087 or 8089
LOCK An instruction prefix. Prevents another processor from
taking the bus while adjacent instruction executes
2/20/2015
44
Microprocessors & Interfacing (A1423)
No operation instructions
87
MNEMONIC Description
NOP No action except fetch and decode
Microprocessors & Interfacing (A1423)
Assembler Directives
88
An assembler is a program used to convert an ALP into
equivalent machine code modules which may further be
converted to executable codes.
Assembler Directives are directions to the assembler.
Assembler directives are the commands to the assembler that
direct the assembly process.
They indicate how an operand is treated by the assembler and
how assembler handles the program.
They also direct the assembler how program and data should be
arranged in the memory.
2/20/2015
45
Microprocessors & Interfacing (A1423)
List of Assembler Directives
89
ASSUME DB DW DD DQ
DT END ENDP ENDS EQU
EVEN EXTRN GLOBAL GROUP INCLUDE
LABEL LENGTH NAME OFFSET ORG
PROC PTR SEGMENT SHORT TYPE
Microprocessors & Interfacing (A1423)
Data Definition and Storage Allocation Directives
90
Data definition directives are used to define the program
variables and allocate a specified amount of memory to them.
DB: Define Byte:
The DB directive is used to reserve byte or bytes of memory
locations in the available memory.
Syntax: Name of the variable DB Initialization Value
Ex : RANKS DB 01H, 02H, 03H, 04H
This statement direct the assembler to reserve four memory
locations for a list named RANKS and initializes them with the
above specified four values.
2/20/2015
46
Microprocessors & Interfacing (A1423)Data Definition and Storage Allocation Directives
Cont..
91
DW: Define Word
The DW directive serves the same purposes as the DB directive,
but it now makes the assembler the number of memory words (16-
bit) instead of bytes.
Syntax: Name of the variable DW Initialization Value
Ex: WORDS DW 1234H, 4567H, 78AH, 045CH
This makes the assembler to reserve four words in memory (8
bytes), and initialize the words with the specified values in the
statements.
Another option of the DW directive is explained with the DUP
operator.
Ex: WDATA DW 5 DUP (6666H)
This statement reserves five words, i.e., 10-bytes of memory for a
word label WDATA and initializes all the word locations with 6666H.
Microprocessors & Interfacing (A1423)Data Definition and Storage Allocation Directives
Cont..
92
DQ: Define Quad word
This directive is used to direct the assembler to reserve 4 words (8 bytes) of memory for the specified variable and may initialize it with the specified values.
Syntax: Name of the variable DQ Initialization Value
Ex: TOTAL DQ 0
Reserves 8 bytes of memory for the variable TOTAL and initializes with 0.
2/20/2015
47
Microprocessors & Interfacing (A1423)Data Definition and Storage Allocation Directives
Cont..
93
DD: Define Double
The directive DD is used to define a double word (4
bytes) variable.
Syntax: Name of the variable DD Initialization value
Ex: TOTAL DD 0
Reserves 4 bytes of memory for the variable TOTAL
and initializes with 0.
Microprocessors & Interfacing (A1423)Data Definition and Storage Allocation Directives
Cont..
94
DT: Define Ten bytes
The DT directive directs the assembler to define the
specified variable requiring 10 bytes for its storage
and initialize the 10 bytes with the specified values.
Syntax: Name of the variable DT Initialization
Value
Ex: ACODE DT 0
The above statement informs the assembler to
reserve 10 bytes of memory for a variable named
ACODE and initialize with 0.
2/20/2015
48
Microprocessors & Interfacing (A1423)
PROGRAM ORGANIZATION DIRECTIVES
95
The 8086 programs are organized as a collection of logical
segments. The directives used to organize the program segments
are: SEGMENT, ASSUME, ENDS etc.
SEGMENT:
The directive „SEGMENT‟ is used to indicate the beginning
of a logical segment. The directive segment follows the name of
the segment. To end the segment, SEGMENT must be ended
with ENDS statement.
Syntax: DATA SEGMENT
;
;
; program data definition
DATA ENDS
Microprocessors & Interfacing (A1423)
PROGRAM ORGANIZATION DIRECTIVES Cont..
96
ENDS:
The directive ENDS informs the assembler the
end of the segment. The directives ENDS and
SEGMENT must enclose the segment data or
code of the program.
Syntax: Segment name ENDS.
2/20/2015
49
Microprocessors & Interfacing (A1423)
PROGRAM ORGANIZATION DIRECTIVES Cont..
97
ASSUME:
The directive assume informs the assembler the
name of the logical segment that should be used for a
specified segment when program is loaded and the
processor segment registers should point to the
respective logical segments.
Syntax: ASSUME SEGREG : SEGNAME
Ex: ASSUME CS : _code
ASSUME CS : _code, DS : _Data, SS : _stack
Microprocessors & Interfacing (A1423)
PROGRAM ORGANIZATION DIRECTIVES Cont..
98
END: End of program
Program termination directive and it is used to
inform the assembler the physical end of the
program.
The statement after the directive END will be
ignored by the assembler.
2/20/2015
50
Microprocessors & Interfacing (A1423)
ASSEMBLER DIRECTIVES Cont..
99
OFFSET: The directive OFFSET informs the assembler to
determine the displacement of the specified variable with respect to
the base of the segment. It is usually used to load the offset of a
variable into the register using this OFFSET value a variable can be
referenced using indexed addressing modes.
Syntax: OFFSET Variable Name
Ex: DATA Segment
MSG DB „Hello World‟
A DW 50 DUP (?)
DATA ENDS
MOV DX, OFFSET MSG; DX= offset of the variable MSG
MOV SI, OFFSET A; SI= offset of the array.
MOV AX, [SI]; AX= element of array pointed to by SI i.e.[SI] refers to A[1]
[SI+2] refers to A[2]
Microprocessors & Interfacing (A1423)
ASSEMBLER DIRECTIVES Cont..
100
PROC: Procedure
The directive PROC indicates the beginning of the
procedure. The directive PROC follows the name of
the procedure and the term NEAR or FAR follows the
PROC indicating the type of procedure. The directive
PROC is used with the directive ENDP to enclose the
procedure code.
Syntax: Procedure Name PROC NEAR/FAR
Ex: ONE PROC NEAR
TWO PROC FAR
2/20/2015
51
Microprocessors & Interfacing (A1423)
ASSEMBLER DIRECTIVES Cont..
101
ENDP: End of Procedure
The directive ENDP informs the assembler the end of
procedure.
Syntax: Procedure Name ENDP
Ex: ONE1 ENDP.
Microprocessors & Interfacing (A1423)
ASSEMBLER DIRECTIVES Cont..
102
EVEN: Align on Even Memory address
The directive EVEN updates the location counter to the
next even address, if the current location counter
contents are not even.
EX: EVEN
Proc ROOT
ENDP
2/20/2015
52
Microprocessors & Interfacing (A1423)
ASSEMBLER DIRECTIVES Cont..
103
EQU: Equate
The directive EQU is used to assign a label with a value
or symbol. The use of this directive is just to reduce
recurrence of the numerical values or constants in the
program.
EX: LABEL EQU 0500H
LABEL EQU ADD
Microprocessors & Interfacing (A1423)
ASSEMBLER DIRECTIVES Cont..
104
LABEL:
The directive LABEL is used to assign a name to the
current content of the location counter.
2/20/2015
53
Microprocessors & Interfacing (A1423)
ASSEMBLER DIRECTIVES Cont..
105
EXTRN: External and PUBLIC:
The directive EXTRN informs the assembler that the names, procedures and labels declared after this directive have already been defined in some other assembly language modules.
while in the other module, where the names, procedures and labels actually appear, they must be declared public, using PUBLIC directive.
EX: MOD1 SEGMENT
PUBLIC FACTORIAL
MOD1 ENDS
MOD2 SEGMENT
EXTRN FACTORIAL
MOD2 ENDS
Microprocessors & Interfacing (A1423)
ASSEMBLER DIRECTIVES Cont..
106
GROUP: Group the related Segments
The directive GROUP informs the assembler to
form logical groups of segments with similar purpose or
type. This directive inform the assembler to form a logical
group of the following segment names.
EX: PROGRAM GROUP CODE, DATA, STACK
2/20/2015
54
Microprocessors & Interfacing (A1423)
ASSEMBLER DIRECTIVES Cont..
107
ORG: Origin
The directive ORG directs the assembler to start
the memory allocation for the particular segment, block
or code from the declared address in the ORG
statement.
EX: ORG 0200H
Microprocessors & Interfacing (A1423)
ASSEMBLER DIRECTIVES Cont..
108
NAME: Logical Name of a Module
LOCAL: The labels, variables, constants or procedures
declared LOCAL in a module are to be used only by
that particular module.
LOCAL a, b, DATA, ARRAY, ROUTINE
LENGTH: This directive is not available in MASM. This
is used to refer to the length of a data array or a
string.
MOV CX, LENGTH ARRAY
GLOBAL: The labels, variables, constants or
procedures declared GLOBAL in a module are to be
used by other modules of the program.
2/20/2015
55
Microprocessors & Interfacing (A1423)
Procedures
109
A procedure is a collection of instructions to which we can direct
the flow of our program, and once the execution of these
instructions is over control is given back to the next line to
process of the code which called on the procedure.
At the time of invoking a procedure the address of the next
instruction of the program is kept on the stack so that, once the
flow of the program has been transferred and the procedure is
done, one can return to the next line
of the original program, the one which called the procedure.
Intrasegment procedure(IP in stack)
Intersegment procedure(CS:IP in stack)
Microprocessors & Interfacing (A1423)
Procedures Cont..
110
Syntax:
Procedure name PROC
near
instruction 1
instruction 2
RET
Procedure name ENDP
Example:
ADD2 PROC near
ADD AX,BX
RET
ADD2 ENDP
2/20/2015
56
Microprocessors & Interfacing (A1423)
Macros
111
A macro is a group of repetitive instructions in
a program which are codified only once and
can be used as many times as necessary.
Macro with in a macro is a nested MACRO
A macro can be defined anywhere in program
using the directives MACRO and ENDM
Microprocessors & Interfacing (A1423)
Defining a MACRO
112
Display MACRO
MOV AX, SEG MSG
MOV DS,AX
MOV DX, OFFSET MSG
MOV AH, 09H
INT 21H
ENDM
2/20/2015
57
Microprocessors & Interfacing (A1423)
Passing parameters to a MACRO
113
Display MACRO MSG
MOV AX, SEG MSG
MOV DS,AX
MOV DX, OFFSET MSG
MOV AH, 09H
INT 21H
ENDM
Parameter MSG can be replaced
by MSG1 or MSG2 while calling
the MACRO
.
.
DISPLAY MSG1
.
.
.
DISPLAY MSG2
Microprocessors & Interfacing (A1423)
PROCEDURE Vs MACRO
114
Procedures Macros
Accessed by CALL and RET
mechanism during program execution
Accessed by name given to macro
when defined during assembly
Machine code for instructions only put
in memory once
Machine code generated for
instructions each time called
Parameters are passed in registers,
memory locations or stack
Parameters passed as part of
statement which calls macro
Procedures uses stack Macro does not utilize stack
A procedure can be defined anywhere
in program using the directives PROC
and ENDP
A macro can be defined anywhere in
program using the directives MACRO
and ENDM
Procedures takes huge memory for
CALL(3 bytes each time CALL is used)
instruction
Length of code is very huge if macro‟s
are called for more number of times