PA Unit-2 and 3
PA Unit-2 and 3
SS SP or BP Stack address
3. Direct Addressing
5. Based Addressing
Group II : Addressing modes for memory data
6. Indexed Addressing
8. String Addressing
8. String Addressing
5
8086 Microprocessor Group I : Addressing modes for register and
immediate data
1. Register Addressing
In immediate addressing mode, an 8-bit or 16-bit
2. Immediate Addressing data is specified as part of the instruction
3. Direct Addressing
Example:
4. Register Indirect Addressing
MOV DL, 08H
5. Based Addressing
The 8-bit data (08H) given in the instruction is
6. Indexed Addressing moved to DL
8. String Addressing
MOV AX, 0A9FH
9. Direct I/O port Addressing
The 16-bit data (0A9FH) given in the instruction is
10. Indirect I/O port Addressing
moved to AX register
11. Relative Addressing
(AX) 0A9FH
12. Implied Addressing
6
8086 Microprocessor
Supported combinations:
BX SI
+ disp
BP DI 7
8086 Microprocessor Group II : Addressing modes for memory
data
1. Register Addressing
2. Immediate Addressing
Here, the effective address of the memory
3. Direct Addressing
location at which the data operand is stored is
4. Register Indirect Addressing given in the instruction.
12. Implied Addressing This addressing mode is called direct because the
displacement of the operand from the segment
base is specified directly in the instruction.
8
8086 Microprocessor Group II : Addressing modes for memory
data
(CL) (MA)
(CH) (MA +1)
9
8086 Microprocessor Group II : Addressing modes for memory
data
(AL) (MA)
10
(AH) (MA + 1)
8086 Microprocessor Group II : Addressing modes for memory
data
(CL) (MA)
(CH) (MA + 1)
11
8086 Microprocessor Group II : Addressing modes for memory
data
12
8086 Microprocessor Group II : Addressing modes for memory
data
1. Register Addressing
2. Immediate Addressing
1. Register Addressing
2. Immediate Addressing
3. Direct Addressing
5. Based Addressing
6. Indexed Addressing
Instructions using this mode have no operands.
The instruction itself will specify the data to be
7. Based Index Addressing
operated by the instruction.
8. String Addressing
Example: CLC
9. Direct I/O port Addressing
This clears the carry flag to zero.
10. Indirect I/O port Addressing
16
Instruction Set and Assembly
programming
Assembler directives
Instructions
LABEL: INSTRUCTION ; COMMENT
Address identifier Does not generate any machine code
• Ex. START: MOV AX, BX ; copy BX into AX
• There is a one-to-one relationship between assembly
and machine language instructions
• A compiled machine code implementation of a
program written in a high-level language results in
inefficient code
– More machine language instructions than an assembled
version of an equivalent handwritten assembly language
program
Assembler Directives
• Instructions to the Assembler regarding the program being
executed.
• Used to :
• specify the start and end of a program
• attach value to variables
• allocate storage locations to input/ output data
• define start and end of segments, procedures, macros etc..
Assembler Directives
DB Define Byte
ASSUME Range : 00H – FFH for unsigned value; 00H – 7FH for
positive value and 80H – FFH for negative value
ORG
END General form : variable DB value/ values
EVEN
EQU
PROC
FAR Example:
NEAR LIST DB 7FH, 42H, 35H
ENDP
Three consecutive memory locations are reserved for the variable LIST
SHORT and each data specified in the instruction are stored as initial value in
the reserved memory location
MACRO
ENDM
Assembler Directives
DB Define Word
PROC
FAR Example:
NEAR ALIST DW 6512H, 0F251H, 0CDE2H
ENDP
Six consecutive memory locations are reserved for the variable ALIST and
SHORT each 16-bit data specified in the instruction is stored in two consecutive
memory location.
MACRO
ENDM
Assembler Directives
DB SEGMENT : Used to indicate the beginning of a code/ data/
stack segment
DW
ENDS : Used to indicate the end of a code/ data/ stack
SEGMENT segment
ENDS
General form:
ASSUME
ORG
END Segnam SEGMENT
EVEN
…
EQU … Program code
… or
PROC … Data Defining Statements
…
FAR …
NEAR
ENDP Segnam ENDS
SHORT
ORG
User defined name of the
END Segment Register
segment
EVEN
EQU
PROC Example:
FAR
NEAR ASSUME CS: ACODE, DS:ADATA Tells the compiler that the instructions of the
ENDP program are stored in the segment ACODE and
data are stored in the segment ADATA
SHORT
MACRO
ENDM
Assembler Directives
ORG (Origin) is used to assign the starting address (Effective address)
DB
for a program/ data segment
ORG
Examples:
END
EVEN ORG 1000H Informs the assembler that the statements following ORG
EQU 1000H should be stored in memory starting with effective
address 1000H
PROC
FAR
val EQU 10FEH Value of variable val is 10FEH
NEAR
ENDP
_SDATA SEGMENT In this data segment, effective address of memory location
SHORT ORG 1200H assigned to A will be 1200H and that of B will be 1202H and
A DB 4CH 1203H.
EVEN
MACRO B DW 1052H
ENDM _SDATA ENDS
Assemble Directives
PROC Indicates the beginning of a procedure
DB
ENDP End of procedure
DW
FAR Intersegment call
SEGMENT
ENDS NEAR Intrasegment call
General form
ASSUME
ORG
procname PROC[NEAR/ FAR]
END
EVEN …
… Program statements of the procedure
EQU
…
Last statement of the procedure
PROC RET
ENDP
FAR procname ENDP
NEAR
SEGMENT ADD64 PROC NEAR The subroutine/ procedure named ADD64 is declared as
ENDS NEAR and so the assembler will code the CALL and RET
… instructions involved in this procedure as near call and
… return
ASSUME …
RET
ORG
ADD64 ENDP
END
EVEN
EQU CONVERT PROC FAR The subroutine/ procedure named CONVERT is declared as
FAR and so the assembler will code the CALL and RET
… instructions involved in this procedure as far call and return
PROC …
ENDP …
FAR
RET
NEAR CONVERT ENDP
SHORT
MACRO
ENDM
Assemble Directives
DB Reserves one memory location for 8-bit signed displacement
in jump instructions
DW
Example:
SEGMENT
ENDS
ASSUME JMP SHORT AHEAD The directive will reserve one memory location
for 8-bit displacement named AHEAD
ORG
END
EVEN
EQU
PROC
ENDP
FAR
NEAR
SHORT
MACRO
ENDM 28
Assemble Directives
DB MACRO Indicate the beginning of a macro
PROC
ENDP mPutchar MACRO char .code
FAR PUSH eax mPutchar 'A'
NEAR MOV al,char
MOV ah,01h
SHORT INT 21H
POP eax
MACRO
ENDM
ENDM
Software
• The sequence of commands used to tell a microcomputer
what to do is called a program,
• Each command in a program is called an instruction
• A program written in machine language is referred to as
machine code
ADD AX, BX
• Arithmetic Instructions
• String Instructions
POP Des:
It pops the operand from top of stack to Des.
Des can be a general purpose register, segment
register (except CS) or memory location.
E.g.: POP AX
Data Transfer Instructions
• XCHG Des, Src:
– This instruction exchanges Src with Des.
– It cannot exchange two memory locations directly.
– E.g.: XCHG DX, AX
– XCHG mem, reg
– immediate value can’t be a operand
Data Transfer Instructions
• IN Accumulator, Port Address:
– It transfers the operand from specified port to accumulator
register.
• SAHF:
– It copies the contents of AH to lower byte of flag register.
• PUSHF:
– Pushes flag register to top of stack.
• POPF:
– Pops the stack top to flag register.
Data Transfer Instructions
• XLAT/XLATB (translate) (contents of lookup table into AL)
– Locates a byte entry in a table in memory, using the contents of the
AL register as a table index, then copies the contents of the table
entry back into the AL register.
– AL DS:[BX+Al]
Arithmetic Instructions
• ADD Des, Src:
– It adds a byte to byte or a word to word.
– It effects AF, CF, OF, PF, SF, ZF flags.
– E.g.:
• ADD AL, 74H
• ADD DX, AX
• ADD AX, [BX]
Arithmetic Instructions
• ADC Des, Src:
– It adds the two operands with CF.
– It effects AF, CF, OF, PF, SF, ZF flags.
– E.g.:
• ADC AL, 74H
• ADC DX, AX
• ADC AX, [BX]
Arithmetic Instructions
• SUB Des, Src:
– It subtracts a byte from byte or a word from word.
– It affects AF, CF, OF, PF, SF, ZF flags.
– For subtraction, CF acts as borrow flag. 0 if in A-B if A>B
and 1 if A<B
– E.g.:
• SUB AL, 74H
• SUB DX, AX
• SUB AX, [BX]
Arithmetic Instructions
• SBB Des, Src:
– It subtracts the two operands and also the borrow
from the result.
– It effects AF, CF, OF, PF, SF, ZF flags.
– E.g.:
• SBB AL, 74H
• SBB DX, AX
• SBB AX, [BX]
Arithmetic Instructions
• INC Src:
– It increments the byte or word by one.
– The operand can be a register or memory location.
– It effects AF, OF, PF, SF, ZF flags.
– CF is not effected.
– E.g.: INC AX
Arithmetic Instructions
• DEC Src:
– It decrements the byte or word by one.
– The operand can be a register or memory location.
– It effects AF, OF, PF, SF, ZF flags.
– CF is not effected.
– E.g.: DEC AX
Arithmetic Instructions
• AAA (ASCII Adjust after Addition):
– The data entered from the terminal is in ASCII format.
– In ASCII, 0 – 9 are represented by 30H – 39H.
– This instruction allows us to add the ASCII codes.
– This instruction does not have any operand.
Condition CF ZF
CX>BX 1 0
CX<BX 0 0
CX=BX 0 1
Arithmetic Instructions
• MUL Src:
– It is an unsigned multiplication instruction.
– It multiplies two bytes to produce a word or two words to produce a double
word.
– AX = AL * Src
– DX : AX = AX * Src
– This instruction assumes one of the operand in AL or AX.
– Src can be a register or memory location.
– CF/OF = 0 if high byte/word/double word = 0. I.e., the upper byte is the
unsigned extension of the low byte/word/double word.
– CF/OF = 1 if high byte/word/double word <> 0. I.e., the upper byte is not the
unsigned extension of low byte/word/double word.
• IMUL Src:
– It is a signed multiplication instruction.
Arithmetic Instructions
• DIV Src:
– It is an unsigned division instruction.
– It divides word by byte or double word by word.
– The operand is stored in AX, divisor is Src and the result is
stored as:
• AH = remainder AL = quotient [If 8 bit operand]
• DX = remainder AX = quotient [If 16 bit operand]
• IDIV Src:
– It is a signed division instruction.
Arithmetic Instruction
• MUL src
– If the source is a byte then other element is in AL and product is
stored in AX
– If the source is a 16-bit word, the AX register is automatically
used as the second parameter and the product is stored in
DX:AX. DX register holds the high part and the AX register holds
the low part of a 32-bit number.
• DIV src
– If the divisor is a byte then AX is used as dividend and quotient
is stored in the AL and remainder in AH
– If the divisor is a word, the DX:AX 32-bit register pair is used as
dividend and quotient is stored in AX and remainder in DX
• IDIV src
• IMUL src
Arithmetic Instructions
• CBW (Convert Byte to Word):
– This instruction converts byte in AL to word in AX.
– The conversion is done by extending the sign bit of AL
throughout AH.
X
0 1 1 0 1 0 1 0
1 1 0 1 0 1 0 0
Shift Instruction
• SHR – Shift Right
0 1 1 0 1 0 1 X
0
0 0 1 1 0 1 0 1
Shift Instruction
• SAR – Shift Arithmetic Right
• SAL – Shift Arithmetic Left
1 1 1 0 1 0 1 X
0
1
0 1 1 1 0 1 0 1
Bit Manipulation Instructions
• ROL Des, Count:
– It rotates bits of byte or word left, by count.
– MSB is transferred to LSB and also to CF.
– If the number of bits desired to be shifted is 1, then
the immediate number 1 can be written in Count.
– However, if the number of bits to be shifted is more
than 1, then the count is put in CL register.
Bit Manipulation Instructions
• ROR Des, Count:
– It rotates bits of byte or word right, by count.
– LSB is transferred to MSB and also to CF.
– If the number of bits desired to be shifted is 1, then
the immediate number 1 can be written in Count.
– However, if the number of bits to be shifted is more
than 1, then the count is put in CL register.
Rotate Instruction
• ROL – Rotate Left
0 1 1 0 1 0 1 0
1 1 0 1 0 1 0 0
0
CY
Rotate Instruction
• ROR – Rotate Right
0 1 1 0 1 0 1 0
0 0 1 1 0 1 0 1
0
CY
Rotate Instruction
• RCL – Rotate Left through Carry
0 1 1 0 1 0 1 0
1 1 0 1 0 1 0 1
0
1
CY
Rotate Instruction
• RCR – Rotate Right through Carry
0 1 1 0 1 0 1 0
1 0 1 1 0 1 0 1
1
0
CY
Compare Instruction
CMP destination, source
Condition CF ZF
CX>BX 1 0
CX<BX 0 0
CX=BX 0 1
Program Execution Transfer Instructions
• These instructions cause change in the
sequence of the execution of instruction.
• This change can be through a condition or
sometimes unconditional.
• The conditions are represented by flags.
Program Execution Transfer Instructions
• CALL Des:
– This instruction is used to call a subroutine or function
or procedure.
– The address of next instruction after CALL is saved
onto stack.
• RET:
– It returns the control from procedure to calling
program.
– Every CALL instruction should have a RET.
Program Execution Transfer Instructions
• JMP Des:
– This instruction is used for unconditional jump
from one place to another.
• CLC:
– It clears the carry flag to 0.
• CMC:
– It complements the carry flag.
Processor Control Instructions
• STD:
– It sets the direction flag to 1.
– If it is set, string bytes are accessed from higher
memory address to lower memory address.
• CLD:
– It clears the direction flag to 0.
– If it is reset, the string bytes are accessed from lower
memory address to higher memory address.
String Instructions
• String in assembly language is just a
sequentially stored bytes or words.
• There are very strong set of string instructions
in 8086.
• By using these string instructions, the size of
the program is considerably reduced.
String Instructions
• MOVS / MOVSB / MOVSW:
– It causes moving of byte or word from one string
to another.
– In this instruction, the source string is in Data
Segment and destination string is in Extra
Segment.
– SI and DI store the offset values for source and
destination index.
• MOVS/MOVSB/MOVSW Instruction : This instruction
copies a byte or word from a location in the data
segment to a location in the 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 in the extra segment must be contained in
the DI register.
• For multiple byte or multiple word moves the number of
elements to be moved is put in the CX register so that it
can function as a counter.
• After the byte or word is moved SI and DI are
automatically adjusted to point to the next source and
the next destination. If the direction flag is 0, then SI and
DI will be incremented by 1 after a byte move and they
will incremented by 2 after a word move. If the DF is a 1,
then SI and DI will be decremented by 1 after a byte
move and they will be decremented by 2 after a word
move. MOVS affects no flags.
• The way to tell the assembler whether to code the instruction
for a byte or word move is to add a “B” or a “W” to the MOVS
mnemonic. MOVSB, for example, says move a string as bytes.
MOVSW says move a string as words.
After move SI will be one greater than offset of last byte in source
string. DI will be one greater than offset of last byte of destination
string. CX will be 0.
String Instructions
• CMPS Des, Src:
– It compares the string bytes or words.
CMPS/CMPSB/CMPSW Instruction : A 8086 String
Instructions is a series of the same type of data
items in sequential memory locations.
106
8086 Instruction Format...
• 4 byte
– CMP BX, 0504 81 FB 04 05
– MOV [500],AL 88 06 00 05
– ADD CX,10 81 C1 10 00
• 5 byte inter segment CALL or JMP
– JMP 2000:3000, EA 0030 0020
– CALL 1000:2000
• 6 byte immediate data to memory
– MOV START, 1234
– MOV [1234], 1234
107