0% found this document useful (0 votes)
22 views107 pages

PA Unit-2 and 3

The document outlines the default segment and offset register combinations used in the 8086 microprocessor, detailing their purposes for code, data, stack, and string addresses. It also explains various addressing modes, including register, immediate, direct, and indirect addressing, along with their applications in memory access and I/O port operations. Additionally, it covers assembler directives that guide the assembly process and control machine code generation.

Uploaded by

om.vasudeo.dangi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views107 pages

PA Unit-2 and 3

The document outlines the default segment and offset register combinations used in the 8086 microprocessor, detailing their purposes for code, data, stack, and string addresses. It also explains various addressing modes, including register, immediate, direct, and indirect addressing, along with their applications in memory access and I/O port operations. Additionally, it covers assembler directives that guide the assembly process and control machine code generation.

Uploaded by

om.vasudeo.dangi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 107

UNIT_2

Default Segment and Offset Register


combinations...

Segment Offset Purpose


CS IP Code / Instruction address
BX, DI, SI,
DS 8-bit or 16- Data address
bit number

SS SP or BP Stack address

ES SI String Source address


2
ADDRESSING MODES
8086 Microprocessor
Addressing Modes
Every instruction of a program has to operate on a data.
The different ways in which a source operand is denoted
in an instruction are known as addressing modes.
1. Register Addressing
Group I : Addressing modes for register and
2. Immediate Addressing immediate data

3. Direct Addressing

4. Register Indirect Addressing

5. Based Addressing
Group II : Addressing modes for memory data
6. Indexed Addressing

7. Based Index Addressing

8. String Addressing

9. Direct I/O port Addressing


Group III : Addressing modes for I/O ports
10. Indirect I/O port Addressing

11. Relative Addressing Group IV : Relative Addressing mode

12. Implied Addressing Group V : Implied Addressing mode


4
8086 Microprocessor Group I : Addressing modes for register and
immediate data

1. Register Addressing The instruction will specify the name of the


register which holds the data to be operated by
2. Immediate Addressing the instruction.
3. Direct Addressing Example:
4. Register Indirect Addressing
MOV CL, DH
5. Based Addressing
The content of 8-bit register DH is moved to
6. Indexed Addressing another 8-bit register CL

7. Based Index Addressing (CL)  (DH)

8. String Addressing

9. Direct I/O port Addressing

10. Indirect I/O port Addressing

11. Relative Addressing

12. Implied 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

7. Based Index Addressing (DL)  08H

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

Addressing Modes : Memory Access


To access memory we use these four registers: BX, SI, DI, BP

Combining these registers inside [ ] symbols, we can get different


memory locations (Effective Address, EA)

Supported combinations:

[BX + SI] [BX + SI + d8]


[SI]
[BX + DI] [BX + DI + d8]
[DI]
[BP + SI] [BP + SI + d8]
d16 (variable offset only)
[BP + DI] [BP + DI + d8]
[BX]

[SI + d8] [BX + SI + d16] [SI + d16]


[DI + d8] [BX + DI + d16] [DI + d16]
[BP + d8] [BP + SI + d16] [BP + d16]
[BX + d8] [BP + DI + d16] [BX + d16]

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.

5. Based Addressing The effective address is just a 16-bit number


written directly in the instruction.
6. Indexed Addressing
Example:
7. Based Index Addressing
MOV BX, [1354H]
8. String Addressing MOV BL, [0400H]
9. Direct I/O port Addressing
The square brackets around the 1354H denotes
the contents of the memory location. When
10. Indirect I/O port Addressing
executed, this instruction will copy the contents of
11. Relative Addressing the memory location into BX register.

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

1. Register Addressing In Register indirect addressing, name of the


register which holds the effective address (EA)
2. Immediate Addressing will be specified in the instruction.

3. Direct Addressing Registers used to hold EA are any of the following


registers:
4. Register Indirect Addressing
BX, BP, DI and SI.
5. Based Addressing
Content of the DS register is used for base
6. Indexed Addressing
address calculation.
7. Based Index Addressing
Example:
8. String Addressing Note : Register/ memory
MOV CX, [BX] enclosed in brackets refer to
9. Direct I/O port Addressing content of register/ memory
Operations:
10. Indirect I/O port Addressing
EA = (BX)
11. Relative Addressing BA = (DS) x 1610
MA = BA + EA
12. Implied Addressing
(CX)  (MA) or,

(CL)  (MA)
(CH)  (MA +1)
9
8086 Microprocessor Group II : Addressing modes for memory
data

1. Register Addressing In Based Addressing, BX or BP is used to hold the


base value for effective address and a signed 8-bit
2. Immediate Addressing or unsigned 16-bit displacement will be specified
in the instruction.
3. Direct Addressing
In case of 8-bit displacement, it is sign extended
4. Register Indirect Addressing to 16-bit before adding to the base value.

5. Based Addressing When BX holds the base value of EA, 20-bit


physical address is calculated from BX and DS.
6. Indexed Addressing
When BP holds the base value of EA, BP and SS is
7. Based Index Addressing
used.
8. String Addressing
Example:
9. Direct I/O port Addressing
MOV AX, [BX + 08H]
10. Indirect I/O port Addressing
Operations:
11. Relative Addressing
0008H  08H (Sign extended)
12. Implied Addressing EA = (BX) + 0008H
BA = (DS) x 1610
MA = BA + EA

(AX)  (MA) or,

(AL)  (MA)
10
(AH)  (MA + 1)
8086 Microprocessor Group II : Addressing modes for memory
data

1. Register Addressing SI or DI register is used to hold an index value for


memory data and a signed 8-bit or unsigned 16-
2. Immediate Addressing bit displacement will be specified in the
instruction.
3. Direct Addressing
Displacement is added to the index value in SI or
4. Register Indirect Addressing DI register to obtain the EA.

5. Based Addressing In case of 8-bit displacement, it is sign extended


to 16-bit before adding to the base value.
6. Indexed Addressing

7. Based Index Addressing


Example:
8. String Addressing
MOV CX, [SI + 0A2H]
9. Direct I/O port Addressing
Operations:
10. Indirect I/O port Addressing
FFA2H  A2H (Sign extended)
11. Relative Addressing
EA = (SI) + FFA2H
12. Implied Addressing BA = (DS) x 1610
MA = BA + EA

(CX)  (MA) or,

(CL)  (MA)
(CH)  (MA + 1)
11
8086 Microprocessor Group II : Addressing modes for memory
data

1. Register Addressing In Based Index Addressing, the effective address


is computed from the sum of a base register (BX
2. Immediate Addressing or BP), an index register (SI or DI) and a
displacement.
3. Direct Addressing
Example:
4. Register Indirect Addressing
MOV DX, [BX + SI + 0AH]
5. Based Addressing
Operations:
6. Indexed Addressing
000AH  0AH (Sign extended)
7. Based Index Addressing

8. String Addressing EA = (BX) + (SI) + 000AH


BA = (DS) x 1610
9. Direct I/O port Addressing MA = BA + EA

10. Indirect I/O port Addressing (DX)  (MA) or,

11. Relative Addressing (DL)  (MA)


(DH)  (MA + 1)
12. Implied Addressing

12
8086 Microprocessor Group II : Addressing modes for memory
data

1. Register Addressing Employed in string operations to operate on string


data.
2. Immediate Addressing
The effective address (EA) of source data is stored
3. Direct Addressing in SI register and the EA of destination is stored in
DI register.
4. Register Indirect Addressing
Segment register for calculating base address of
5. Based Addressing source data is DS and that of the destination data
is ES
6. Indexed Addressing

7. Based Index Addressing


Example: MOVS BYTE
8. String Addressing
Operations:
9. Direct I/O port Addressing
Calculation of source memory location:
10. Indirect I/O port Addressing EA = (SI) BA = (DS) x 1610 MA = BA + EA

11. Relative Addressing Calculation of destination memory location:


EAE = (DI) BAE = (ES) x 1610 MAE = BAE + EAE
12. Implied Addressing

Note : Effective address of the (MAE)  (MA)


Extra segment register
If DF = 1, then (SI)  (SI) – 1 and (DI) = (DI) - 1
If DF = 0, then (SI)  (SI) +1 and (DI) = (DI)13+ 1
8086 Microprocessor Group III : Addressing modes for I/O
ports

1. Register Addressing These addressing modes are used to access data


from standard I/O mapped devices or ports.
2. Immediate Addressing
In direct port addressing mode, an 8-bit port
3. Direct Addressing address is directly specified in the instruction.

4. Register Indirect Addressing Example: IN AL, [09H]

5. Based Addressing Operations: PORTaddr = 09H


(AL)  (PORT)
6. Indexed Addressing
Content of port with address 09H is
7. Based Index Addressing
moved to AL register
8. String Addressing
In indirect port addressing mode, the instruction
9. Direct I/O port Addressing will specify the name of the register which holds
the port address. In 8086, the 16-bit port address
10. Indirect I/O port Addressing is stored in the DX register.

11. Relative Addressing Example: OUT [DX], AX

12. Implied Addressing Operations: PORTaddr = (DX)


(PORT)  (AX)

Content of AX is moved to port


whose address is specified by DX
register. 14
8086 Microprocessor Group IV : Relative Addressing
mode

1. Register Addressing

2. Immediate Addressing

3. Direct Addressing In this addressing mode, the effective address of


a program instruction is specified relative to
4. Register Indirect Addressing Instruction Pointer (IP) by an 8-bit signed
displacement.
5. Based Addressing
Example: JZ 0AH
6. Indexed Addressing
Operations:
7. Based Index Addressing

8. String Addressing 000AH  0AH (sign extend)

9. Direct I/O port Addressing If ZF = 1, then

10. Indirect I/O port Addressing EA = (IP) + 000AH


BA = (CS) x 1610
11. Relative Addressing MA = BA + EA

12. Implied Addressing If ZF = 1, then the program control jumps to


new address calculated above.

If ZF = 0, then next instruction of the


program is executed.
15
8086 Microprocessor Group IV : Implied Addressing
mode

1. Register Addressing

2. Immediate Addressing

3. Direct Addressing

4. Register Indirect 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

11. Relative Addressing

12. Implied 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.

• Control the generation of machine codes and organization of the


program; but no machine codes are generated for assembler
directives.

• Also called ‘pseudo instructions’

• 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

DW Define a byte type (8-bit) variable

SEGMENT Reserves specific amount of memory locations to each


ENDS variable

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

DW Define a word type (16-bit) variable

SEGMENT Reserves two consecutive memory locations to each variable


ENDS
Range : 0000H – FFFFH for unsigned value; 0000H –
ASSUME 7FFFH for positive value and 8000H – FFFFH for negative
value
ORG
END General form : variable DW value/ values
EVEN
EQU

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

MACRO User defined name of the


segment
ENDM
Assembler Directives
DB Informs the assembler the name of the program/ data
segment that should be used for a specific segment.
DW
General form:
SEGMENT
ENDS
ASSUME segreg : segnam, .. , segreg : segnam
ASSUME

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

DW END is used to terminate a program; statements after END will be


ignored
SEGMENT
ENDS EVEN : Informs the assembler to store program/ data segment
starting from an even address
ASSUME
EQU (Equate) is used to attach a value to a variable

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

SHORT User defined name of the


procedure
MACRO
ENDM
Assembler Directives
DB
Examples:
DW

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

DW ENDM End of a macro

SEGMENT General form:


ENDS
macroname MACRO[Arg1, Arg2 ...]
ASSUME Program statements
… in the macro

ORG …
END
EVEN ENDM
EQU

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

(Opcode) (Destination operand) (Source operand )


• Two key benefits of assembly language
programming
– It takes up less memory
– It executes much faster
Applications
• One of the most beneficial uses of
assembly language programming is
real-time applications.
Real time means the task required by the application must be
completed before any other input to the program that will
alter its operation can occur
For example the device service routine which controls the
operation of the floppy disk drive is a good example that is
usually written in assembly language
• Assembly language not only good for
controlling hardware devices but also
performing pure software operations
– Searching through a large table of data for a special string
of characters
– Code translation from ASCII to EBCDIC
– Table sort routines
– Mathematical routines
Assembly language: perform real-time operations
High-level languages: used to write those parts
that are not time critical
Elements of an Instruction
• Operation code (opcode)
– Do this: ADD, SUB, MPY, DIV, LOAD, STOR

• Source operand reference


– To this: (address of) argument of op, e.g. register,
memory location

• Destination operand reference


– Put the result here (as above)
Instruction Cycle State Diagram
Instruction Types
• Data transfer: registers, main memory, stack
or I/O
• Data processing: arithmetic, logical
• Control: systems control, transfer of control
Types of Operand
• Addresses: immediate, direct, indirect, stack
• Numbers: integer or fixed point (binary, twos
complement), floating point (sign, significand,
exponent), (packed) decimal (246 = 0000 0010
0100 0110)
• Characters: ASCII (128 printable and control
characters + bit for error detection)
• Logical Data: bits or flags, e.g., Boolean 0 and
1
Classification of Instruction Set
• Data Transfer Instructions

• Arithmetic Instructions

• Bit Manipulation Instructions

• Program Execution Transfer Instructions

• String Instructions

• Processor Control Instructions


Data Transfer Instructions
• These instructions are used to transfer
data from source to destination.
• The operand can be a constant, memory
location, register or I/O port address.
• These instructions will never affect FLAG
register
Data Transfer Instructions
MOV Des, Src:
– Src operand can be register, memory
location, indirect or immediate operand.
– Des can be register or memory operand.
– Both Src and Des cannot be memory
location at the same time.
E.g.
Instruction Description
MOV CX, 037A H Immediate

MOV AL, BL Register

MOV BX, [0301 H] Direct addressing

MOV AL, [SI] Indirect addressing


Data Transfer Instructions
PUSH Operand:
 It pushes the operand into top of stack.
 E.g.: PUSH BX
 PUSH memory

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.

– E.g.: IN AX, 0028 H

• Ax is given address of a port

• OUT Port Address, Accumulator:


– It transfers the operand from accumulator to specified port.

– E.g.: OUT 0028 H, AX


Data Transfer Instructions
• LEA Register, Src:
– It loads a 16-bit register with the offset
address of the data specified by the Src.
– E.g.: LEA BX, [DI]
• This instruction loads the contents of DI
(offset) into the BX register.
E.G: LEA BX, ADDR
• Where ADDR is a label and effective
address of label is copied in BX.
Data Transfer Instructions
• LDS Des, Src
– It loads pointer from memory source to
destination register and DS.
– The offset is placed in the destination register
and the segment is placed in DS.
– To use this instruction the word at the lower
memory address must contain the offset and
the word at the higher address must contain
the segment.
– E.g.: LDS BX, [0301 H]
Data Transfer Instructions
• LES Des, Src
– It loads pointer from memory source to
destination register and ES.
– The offset is placed in the destination register
and the segment is placed in ES.
– This instruction is very similar to LDS except
that it initializes ES instead of DS.
– E.g.: LES BX, [0301 H]
Data Transfer Instructions
• LAHF:
– It copies the lower byte of flag register to AH.

• 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.

– DS:BX contains base address of memory location

– 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.

• Other ASCII Instructions:


– AAS (ASCII Adjust after Subtraction)
– AAM (ASCII Adjust after Multiplication)
– AAD (ASCII Adjust Before Division)
Arithmetic Instructions
• DAA (Decimal Adjust after Addition)
– It is used to make sure that the result of adding two
BCD numbers is adjusted to be a correct BCD number.
– It only works on AL register.

• DAS (Decimal Adjust after Subtraction)


– It is used to make sure that the result of subtracting
two BCD numbers is adjusted to be a correct BCD
number.
– It only works on AL register.
Arithmetic Instructions
• NEG Src:
– It creates 2’s complement of a given number.
– That means, it changes the sign of a number.
Arithmetic Instructions
• CMP Des, Src:
– It compares two specified bytes or words.
– The Src and Des can be a constant, register or memory
location.
– Both operands cannot be a memory location at the
same time.
– The comparison is done simply by internally
subtracting the source from destination.
– The value of source and destination does not change,
but the flags are modified to indicate the result.
Compare Instruction
CMP destination, source

Ex:- CMP BX, CX

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]

– all flags unaffected

• 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.

• CWD (Convert Word to Double Word):


– This instruction converts word in AX to double word in
DX : AX.
– The conversion is done by extending the sign bit of AX
throughout DX.
• Unit-3
• https://www.youtube.com/watch?v=o5ZunoG
QLVo
Bit Manipulation Instructions
• These instructions are used at the bit level.
• These instructions can be used for:
– Testing a zero bit
– Set or reset a bit
– Shift bits across registers
Bit Manipulation Instructions
• NOT Src:
– It complements each bit of Src to produce 1’s
complement of the specified operand.
– The operand can be a register or memory location.
Bit Manipulation Instructions
• AND Des, Src:
– It performs AND operation of Des and Src.
– Src can be immediate number, register or memory
location.
– Des can be register or memory location.
– Both operands cannot be memory locations at the same
time.
– CF and OF become zero after the operation.
– PF, SF and ZF are updated.
Bit Manipulation Instructions
• OR Des, Src:
– It performs OR operation of Des and Src.
– Src can be immediate number, register or memory
location.
– Des can be register or memory location.
– Both operands cannot be memory locations at the
same time.
– CF and OF become zero after the operation.
– PF, SF and ZF are updated.
Bit Manipulation Instructions
• XOR Des, Src:
– It performs XOR operation of Des and Src.
– Src can be immediate number, register or memory
location.
– Des can be register or memory location.
– Both operands cannot be memory locations at the
same time.
– CF and OF become zero after the operation.
– PF, SF and ZF are updated.
Bit Manipulation Instructions
• SHL Des, Count:
– It shift bits of byte or word left, by count.
– It puts zero(s) in LSBs.
– MSB is shifted into carry flag.
– 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
• SHR Des, Count:
– It shift bits of byte or word right, by count.
– It puts zero(s) in MSBs.
– LSB is shifted into carry flag.
– 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.
Shift Instruction
• SHL – Shift Left

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

Ex:- CMP BX, CX

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.

• Jxx Des (Conditional Jump):


– All the conditional jumps follow some conditional
statements or any instruction that affects the flag.
Conditional Jump Table
Mnemonic Meaning Jump Condition
JA Jump if Above CF = 0 and ZF = 0
JAE Jump if Above or Equal CF = 0
JB Jump if Below CF = 1
JBE Jump if Below or Equal CF = 1 or ZF = 1
JC Jump if Carry CF = 1
JE Jump if Equal ZF = 1
JNC Jump if Not Carry CF = 0
JNE Jump if Not Equal ZF = 0
JNZ Jump if Not Zero ZF = 0
JPE Jump if Parity Even PF = 1
JPO Jump if Parity Odd PF = 0
JZ Jump if Zero ZF = 1
Program Execution Transfer Instructions
• Loop Des:
– This is a looping instruction.
– The number of times looping is required is placed
in the CX register.
– With each iteration, the contents of CX are
decremented.
– ZF is checked whether to loop again or not.
Processor Control Instructions
• These instructions control the processor itself.
• 8086 allows to control certain control flags
that:
– causes the processing in a certain direction
– processor synchronization if more than one
microprocessor attached.
Processor Control Instructions
• STC:
– It sets the carry flag to 1.

• 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.

The CMPS instruction can be used to compare a


byte in one string with a byte in another string or to
compare a word in one string with a word in
another string.
CMPS Des, Src:

• SI is used to hold the offset of a byte or word


in the source string and DI is used to hold the
offset of a byte or a word in the other string.
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.
CMPS/CMPSB/CMPSW Instruction :

After the comparison SI and DI will be automatically incremented or decremented


according to direction flag to point to the next element in the two strings (if DF =
0, SI and DI ↑ ) CX functions as a counter which is decremented after each
comparison. This will go on until CX = 0
String Instructions
• SCAS String:
– It scans a string.
– It compares the String with byte in AL or with word in AX.

SCAS/SCASB/SCASW Instruction : SCAS compares a


string byte with a byte in AL or a string word with word
in AX. The instruction affects the flags, but it does not
change either the operand in AL (AX) or the operand in
the 8086 String Instructions.

The string to be ‘scanned must be in the extra segment


and DI must contain the offset of the byte or the word
to be compared.
SCAS/SCASB/SCASW Instruction :

• After the comparison DI will be automatically


incremented or decremented according to
direction flag, to point to the next element in the
two strings (if DF = 0, SI and DI ↑ )

• CX functions as a counter which is decremented


after each comparison. This will go on until CX =
0. SCAS affects the AF, CF, OF, PF, SF and ZF flags.
SCAS/SCASB/SCASW Instruction :

SCASB says compare 8086 String Instructions as bytes


and SCASW says compare strings as words.
LODS/LODSB/LODSW Instruction
• LODS/LODSB/LODSW Instruction : 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 does not
affect any flags. LODSB copies byte and LODSW copies a
word.
String Instructions
• REP (Repeat):
– This is an instruction prefix.
– It causes the repetition of the instruction until CX
becomes zero.
– E.g.: REP MOVSB STR1, STR2
• It copies byte by byte contents.
• REP repeats the operation MOVSB until CX becomes
zero.
STOS/STOSB/STOSW Instruction :
The STOS instruction copies a byte from AL or a word from AX to a
memory location in the extra segment. DI is used to hold the offset
of the memory location in the extra segment. After the copy, DI is
automatically incremented or decremented to point to the next
string element in memory. If the direction flag, DF, is cleared, then DI
will automatically be incremented by one for a byte string or
incremented by two for a word 8086 String Instructions.

If the direction flag is set, DI will be automatically decremented by


ono for a byte string or decremented by two for a word string. STOS
does not affect any flags. STOSB copies byte and STOSW copies a
word.
STOS/STOSB/STOSW Instruction :
8086 Instruction Format
• 1 byte Mostly no operand instructions
– NOP, POPF, PUSHF, INC AX
• 2 byte most of the instructions are 2 byte long
– MOV AX, BX
– MOV AL, 90
• 3 byte
– MOV AX, 1234

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

You might also like