MPI Lec Merged
MPI Lec Merged
Things to Remember
Presentations will be uploaded on CMS in PDF format.
Saturday: 10-11 AM
CKV
3
CKV
I Say
Basic Subject
CKV
A I Say s
Basic Subject
CKV
A I Say s
CKV
Operating
Systems
Computer
Arch. Compiler
Design
MPI
CKV
Handout-Course Overview
Peripheral
Devices
I/O, Memory
Interfacing,
interrupts
Architecture and
Programming
CKV
Handout-Text Books
Textbooks:
Reference books
Lecture 1: Introduction
CKV
Microprocessor-Overview
X=A+B
Y=C+D
F=X+Y
where A, B, C and D are n-bit binary numbers
CKV
Microprocessor-Overview
X=A+B
Y=C+D
F=X+Y
A
B + X
C
Y
+ F
D +
CKV
Microprocessor-Overview
X=A+B
Y=C+D
Sequential Architecture F=X+Y
A
B
Mux
+ Reg 0
C
Clk
D
Control
CKV
Microprocessor-Overview
X=A+B
Y=C+D
Sequential Architecture F=X+Y
A
B
Mux
+
ALU
A
A+B Reg
A
0 A+B
C
Clk
D
X=A+B
Control Y=C*D
F=X-Y
CKV
Microprocessor-Overview
Micro
A A small chip whichALU
B Registers
Processor
Mux
+
has computing i.e. data
processing capabilities Reg
C
Clk
D
Stored Program Computer
X=A+B
Who invented this? Y=C*D
Instruction Control
F=X-Y
Memory
CKV
A General Computer
Mouse
Keyboard
Bus Input Devices
Thank You
CKV
Microprocessor-Overview
Data Memory Universal Architecture
Micro
A A small chip whichALU
B Registers
Processor
Mux
+
has computing i.e. data
processing capabilities Reg
C
Clk
D
Stored Program Computer
Instruction Control
Memory
CKV
A General Computer
Mouse
Keyboard
Bus Input Devices
A General Computer
What happens when you turn ON your computer
Input Devices
Disk DRAM
Memory CPU
ROM
BIOS Output Devices
Processor
What steps will the Microprocessor follow to execute a program??
1. Fetches Instruction
Control Unit
Memory
DRAM µP 2. Decodes Instruction
ROM
BIOS Datapath
and ALU 3. Executes Instruction
Processor-Memory Interaction
Address Address
Bus Send
Bus
Addr.
µP Data Bus
Receive
Data Bus
Memory
Data
Memory
Address
Receive
Bus
Memory
Addr. 8x3
011 0 1 1 0
1 1 0 0
2 0 0 1
3 1 0 1
Send 4 0 1 0
Data Bus
Data 5 0 0 0
6 0 1 0
(Read =1) 1 7 0 1 1
Receive Control
Ctrl. Bus
(Read Signal)
Read
CKV
Memory
Address
Receive
Bus
Memory
Addr.
011 0 1 1 0
1 1 0 0
2 0 0 1
1 1 0 3 1 10 1
0
Receive 4 0 1 0
Data Bus
Data 5 0 0 0
6 0 1 0
(Write =0) 0 7 0 1 1
Receive Control
Ctrl. Bus
(Write Signal)
Write
CKV
Memory
Address
Bus
Memory
8x3
N lines 0 1 1 0
1 1 0 0
2 0 0 1
M lines 3 1 0 1 2N
4 0 1 0
Data Bus
5 0 0 0
6 0 1 0
7 0 1 1
Control M
Bus
CKV
Memory
Speed Cost Size
Register Very Very Smallest
Address High High Bytes
Bus
Memory
8x3
ns
8x3
0 1 1 0 SRAM
1 1 0 0
2 0 0 1
3 1 0 1
4 0 1 0
Data Bus
5 0 0 0 DRAM
6 0 1 0
7 0 1 1
Control Flash
Bus
Memory
Register
Address
Bus
Memory
8x3
Volatile
8x3
0 1 1 0 SRAM Data is lost when
1 1 0 0 powered off
2 0 0 1
3 1 0 1
4 0 1 0
Data Bus
5 0 0 0 DRAM
6 0 1 0
7 0 1 1
Non-Volatile
Control Flash
Bus Data is retained even
when the power is off
Magnetic Disk
CKV
Memory
General
Register Purpose Store Temp. Data
Address Registers
Bus
Memory
8x38x3
0 1 1 0 SRAM Cache Store Recently
1 1 0 0 used Data
2 0 0 1
3 1 0 1
4 0 1 0 Store Current
Data Bus
5 0 0 0 DRAM Primary
6 0 1 0
Memory Application’s Data
7 0 1 1
Control Flash
Bus
Secondary
Store All the Data
Memory
Magnetic Disk
CKV
Memory
General
Register Purpose
GPRs
Address Registers
Bus
Memory
8x38x3
0 1 1 0 SRAM Cache
1
2
1 0 0
0 0 1 µP
3 1 0 1
4 0 1 0
Data Bus
5 0 0 0 DRAM Primary
Primary
Memory
Memory
6 0 1 0
7 0 1 1
Control Flash
Bus
Secondary
Memory
Magnetic Disk
CKV
Processor
What is an Instruction??
1. Fetches Instruction
Control Unit
Memory
DRAM µP 2. Decodes Instruction
ROM
BIOS Datapath
and ALU 3. Executes Instruction
CKV
Processor-Instruction
What is an Instruction??
Processor
Instruction Set Architecture
Execution Model
Processor registers
Address and Data formats
Microarchitecture
Interconnection of elements
µP ALU
Data path
Control Path
Physical Realization
Technology (e.g 10nm, 4nm)
Packaging
CKV
Thank You
CKV
Memory
Register
Address
Bus
Memory
8x38x3
0 1 1 0 SRAM
1 1 0 0
2 0 0 1
3 1 0 1
4 0 1 0
Data Bus
5 0 0 0 DRAM
6 0 1 0
7 0 1 1
Control Flash
Bus
Magnetic Disk
CKV
Processor
What is an Instruction??
1. Fetches Instruction
Control Unit
Memory
DRAM µP 2. Decodes Instruction
ROM
BIOS Datapath
and ALU 3. Executes Instruction
CKV
Processor-Instruction
What is an Instruction??
Processor
Instruction Set
Architecture
µP Microarchitecture
Physical Realization
CKV
Software
Hardware
ISA
For example:
C code
C=A+B
8086 ARM
R1 + R2 R1 R1 + R2 R3
CKV
For example:
8086 ARM
For example:
8086 ARM
One of the operands can be memory Operands can only be in registers
Processor
Classification of processor based in ISA
Reading Assignment
CKV
Processor
Instruction Set Architecture
Execution Model
Processor registers
Address and Data formats
Microarchitecture
Interconnection of elements
µP ALU (e.g. What type of adder?)
Data path
Control Path
Physical Realization
Technology (e.g 10nm, 4nm)
Packaging
CKV
8086 MicroProcessor
BIU (Bus Interface Unit)
8086
Generates memory and I/O addresses
BIU
For transfer of data between processor
Registers and outside world (Memory and I/O)
ALU
EU (Execution Unit)
EU
Control and Receives program instruction codes and
Timing data from BIU
Executes the instructions
8086
8086 Buses
8086
8086 Buses
8086
Thank You
CKV
Processor
Instruction Set
Architecture
µP Microarchitecture
Physical Realization
CKV
Processor
Instruction Set Architecture
Execution Model
Processor registers
Address and Data formats
Microarchitecture
Interconnection of elements
µP ALU (e.g. What type of adder?)
Data path
Control Path
Physical Realization
Technology (e.g 10nm, 4nm)
Packaging
CKV
8086
8086 Buses
8086
ALU
EU
Control
Flag register
Eg: Carry, Overflow
CKV
Bus
ALU Instruction Queue
Control
EU
Control
Flag register 6 Instructions
Eg: Carry, Overflow
CKV
Bus
ALU Instruction Queue
Control
EU
Control
Flag register 6 Instructions
Eg: Carry, Overflow
CKV
Concept of Segmentation
Memory
00000
8086 can address a memory of 1MB
Thank You
CKV
Bus
ALU Instruction Queue
Control
EU
Control
Flag register 6 Instructions
Eg: Carry, Overflow
CKV
Bus
ALU Instruction Queue
Control
EU
Control
Flag register 6 Instructions
Eg: Carry, Overflow
CKV
Concept of Segmentation
Memory
00000
8086 can address a memory of 1MB
Concept of Segmentation
Memory
The code segment is the area of the 00000
memory where code is stored
Code Segment
FFFFF
CKV
Concept of Segmentation
Memory
00000
Starting Address of Code
(CS) * 10H Segment
+ 0000H
64KB
Code Segment
Ending Address of Code
(CS) * 10H Segment
+ FFFFH
Segment base
Address
Multiply
CS
Value in CS
by 10H +
Register Offset
IP Address
Concept of Segmentation
Memory
The Data segment is the area of the 00000
memory where data required for
executing the instructions is stored
Data Segment
e.g. Operands for Arithmetic and Logical
operations
Concept of Segmentation
Memory
00000
Multiply
DS
Value in DS
by 10H + Data Segment
Register BX
Depends on SI
Instruction
DI
Offset Address
FFFFF
CKV
Concept of Segmentation
Memory
The Stack segment is the area of the 00000
memory used for storing address and data
temporarily.
Stack Segment
Any location within Stack segment can be
accessed using Stack Segment and Stack
Pointer/Base Pointer Registers
FFFFF
CKV
Concept of Segmentation
Memory
00000
Multiply
SS
Value in SS
by 10H + Stack Segment
Register BP
SP
FFFFF
CKV
Concept of Segmentation
Memory
The Extra segment is the area of the 00000
memory where data required for
executing the string instructions is stored
Multiply
ES
Value in ES
by 10H +
Register DI
Extra Segment
FFFFF
CKV
Concept of Segmentation
Default 16-bit segment and offset combinations
CS IP Instruction Address
DS BX Data Address
DI
SI
SS SP Stack Address
BP
Concept of Segmentation
Memory Memory
Non
Code Segment
overlapping
segments Code Segment
Data Segment
Data Segment
Stack Segment
Practice Problem
Memory
Calculate the address range for Code, 00000
Data, Stack and Extra Segment, given Code Segment
0FFFF
CS register has 0000H 10000
Data Segment
DS register has 1000H
1FFFF
SS register has 2400H
24000 Stack Segment
ES register has 3100H 31000
33FFF
Extra Segment
40FFF
FFFFF
CKV
Practice Problem
Calculate the Physical address for the following logical
addresses
0000:0100
0001:00F0
0010:0000
CKV
Memory Organization
Memory
00000
00001
?????
2 bytes
1byte
D15-D0
µP 220
FFFFF
8 bits
CKV
Thank You
CKV
Concept of Segmentation
Memory
Default 16-bit segment and offset combinations
DS BX Data Address
DI Data Segment
SI
Stack Segment
SS SP Stack Address
BP
Extra Segment
ES DI String Dest. Address
CKV
Memory Organization
Memory
00000
00001
1byte
Latch A19-A0 50000
12 50000
2 bytes D15-D8 34 50001
56 50003
µP D15-D0 D7-D0 78 50004
Big-Endian
8 bits
CKV
Memory Organization
Memory
Accessing Memory twice – Not optimal 00000
00001
1byte
Latch A19-A0 50000
12 50000
2 bytes D15-D8 34 50001
56 50002
µP D15-D0 D7-D0 78 50003
Memory Organization
Memory Even Bank Odd Bank
50000 12
50001 34
50002 56
50003 78
512KB 512KB
1MB
8 bits
CKV
Memory Organization
Aligned
Read Even Bank Odd Bank
word
µ D15-D0 D7-D0
A0 used to
enable Even
BHE used to
enable Odd
Bank Bank
P ALE
___
A0=0 BHE=0
RD
___
WR__ Even Bank Odd Bank
M/IO Enabled Enabled
___
BHE Both Banks enabled at once
Memory Organization
Write Even Bank Odd Bank
µ D15-D0
90
D7-D0
A0=0
Even Bank
BHE=0
Odd Bank
P ALE
___
2F Enabled Enabled
RD
___
WR__
M/IO
___
BHE Both Banks enabled at once
Memory Organization
Read Even Bank Odd Bank
µ D15-D0 D7-D0
A0 = 0 BHE =1
P ALE
___ Even Bank Odd Bank
RD
___ Enabled Disabled
WR
M/IO
___
BHE Even bank enabled
Memory Organization
Write Even Bank Odd Bank
µ D15-D0 D7-D0
A0 = 0 BHE =1
P ALE
___
2F
Even Bank Odd Bank
RD
___ Enabled Disabled
WR
M/IO
___
BHE Even bank enabled
Memory Organization
Read Even Bank Odd Bank
µ D15-D0 D7-D0
A0 =1 BHE = 0
P ALE
___ Even Bank Odd Bank
RD
___ Disabled Enabled
WR
M/IO
___
BHE Odd bank enabled
Memory Organization
Misaligned
Read Even Bank Odd Bank
word
Memory Organization
𝐁𝐇𝐄 𝐀𝟎 Selection
0 0 Word (16-bits)
0 1 8-bits from Odd Bank
1 0 8-bits from Even Bank
1 1 No Selection
CKV
Next Class
Thank You
CKV
Memory Organization
Memory Organization
D15-D8
D7-D0
Types of Instructions
Arithmetic Instructions
Logical Instructions
Operation
Source: Register, Memory, Immediate value
Flags
CKV
Addressing Modes
Register Addressing
During
Before Exec.
After Exec.
MOV AH, BL
AH 12 56 AL
BH 89 32 BL
Immediate Addressing
After
BeforeExec.
Exec.
MOV AL, 30h
56
30 AL
After
BeforeExec.
Exec.
MOV BX, 1390h
1956
1390 BX
CKV
Direct Addressing
Direct Addressing
Direct Addressing
MOV [3000h], AL
After
BeforeExec.
Exec.
DS:3000 7F
12
7F AL DS:3001 34
DS:3002 56
DS:3003 78
CKV
Direct Addressing
MOV [3002h], BX
After
BeforeExec.
Exec.
DS:3000 7F
FFAD BX DS:3001 34
DS:3002 AD
56
DS:3003 FF
78
CKV
MOV [BX], AL
MOV [SI], BX
CKV
Base-Plus-index Addressing
Register-Relative Addressing
Thank You
CKV
Addressing Modes
Register Addressing
Immediate Addressing
Direct Addressing
Base-Plus-Index Addressing
Addressing Modes
Register Addressing Instruction Code
format
Direct Addressing
Base-Plus-Index Addressing
OR
BYTE 1
1 0 0 0 1 0
OPCODE D W
Operation Code
Specifies data width
e.g. 100010 is
W=0 (Data-byte)
opcode for MOV
instruction W=1 (Data-word)
BYTE 1 BYTE 2
1 0 0 0 1 0
BYTE 1 BYTE 2
1 0 0 0 1 0
MOD 00 01 10 11 (R)
(M) (M) (M) W=0 W=1
R/M
000 [BX+SI] [BX+SI+d8] [BX+SI+d16] AL AX
001 [BX+DI] [BX+DI+d8] [BX+DI+d16] CL CX
010 [BP+SI] [BP+SI+d8] [BP+SI+d16] DL DX
011 [BP+DI] [BP+DI+d8] [BP+DI+d16] BL BX
100 [SI] [SI+d8] [SI+d16] AH SP
101 [DI] [DI+d8] [DI+d16] CH BP
Direct Addressing 110 d16 [BP+d8] [BP+d16] DH SI
Mode
111 [BX] [BX+d8] [BX+d16] BH DI
CKV
BYTE 1 BYTE 2
1 0 0 0 1 0
MOD 00 01 10 11 (R)
(M) (M) (M) W=0 W=1
R/M
000 [BX+SI] [BX+SI+d8] [BX+SI+d16] AL AX
001 [BX+DI] [BX+DI+d8] [BX+DI+d16] CL CX
010 [BP+SI] [BP+SI+d8] [BP+SI+d16] DL DX
011 [BP+DI] [BP+DI+d8] [BP+DI+d16] BL BX
100 [SI] [SI+d8] [SI+d16] AH SP
Register Indirect 101 [DI] [DI+d8] [DI+d16] CH BP
Addressing mode 110 d16 [BP+d8] [BP+d16] DH SI
111 [BX] [BX+d8] [BX+d16] BH DI
CKV
BYTE 1 BYTE 2
1 0 0 0 1 0
MOD 00 01 10 11 (R)
(M) (M) (M) W=0 W=1
R/M
000 [BX+SI] [BX+SI+d8] [BX+SI+d16] AL AX
Base plus index 001 [BX+DI] [BX+DI+d8] [BX+DI+d16] CL CX
Addressing Mode
010 [BP+SI] [BP+SI+d8] [BP+SI+d16] DL DX
011 [BP+DI] [BP+DI+d8] [BP+DI+d16] BL BX
100 [SI] [SI+d8] [SI+d16] AH SP
101 [DI] [DI+d8] [DI+d16] CH BP
110 d16 [BP+d8] [BP+d16] DH SI
111 [BX] [BX+d8] [BX+d16] BH DI
CKV
BYTE 1 BYTE 2
1 0 0 0 1 0
MOD 00 01 10 11 (R)
(M) (M) (M) W=0 W=1
R/M
000 [BX+SI] [BX+SI+d8] [BX+SI+d16] AL AX
001 [BX+DI] [BX+DI+d8] [BX+DI+d16] CL CX
010 [BP+SI] [BP+SI+d8] [BP+SI+d16] DL DX
011 [BP+DI] [BP+DI+d8] [BP+DI+d16] BL BX
100 [SI] [SI+d8] [SI+d16] AH SP
Register-relative 101 [DI] [DI+d8] [DI+d16] CH BP
addressing mode 110 d16 [BP+d8] [BP+d16] DH SI
111 [BX] [BX+d8] [BX+d16] BH DI
CKV
BYTE 1 BYTE 2
1 0 0 0 1 0
MOD 00 01 10 11 (R)
(M) (M) (M) W=0 W=1
R/M
000 [BX+SI] [BX+SI+d8] [BX+SI+d16] AL AX
001 [BX+DI] [BX+DI+d8] [BX+DI+d16] CL CX
Base Relative plus
index addressing 010 [BP+SI] [BP+SI+d8] [BP+SI+d16] DL DX
mode 011 [BP+DI] [BP+DI+d8] [BP+DI+d16] BL BX
100 [SI] [SI+d8] [SI+d16] AH SP
101 [DI] [DI+d8] [DI+d16] CH BP
110 d16 [BP+d8] [BP+d16] DH SI
111 [BX] [BX+d8] [BX+d16] BH DI
CKV
OR
d8
d16
CKV
Exercise Problems
Find the instruction codes for the following cases
MOV AX, BX
OR
Exercise Problems
Find the instruction codes for the following cases
MOD-R/M Table
MOV AX, BX
REG Table
BYTE 1 BYTE 2
1 0 0 0 1 0 1 1 1 1 0 0 0 0 1 1
AX BX
8B C3
CKV
Exercise Problems
Find the instruction codes for the following cases
MOD-R/M Table
MOV BX, [2067h]
REG Table
BYTE 1 BYTE 2
1 0 0 0 1 0 1 1 0 0 0 1 1 1 1 0
Direct Address Direct Address
OPCODE D W MOD REG R/M Low Byte High Byte
BX [d16]
8B 1E 67 20
CKV
Exercise Problems
Find the instruction codes for the following cases
MOD-R/M Table
MOV [BX+SI], AX
REG Table
BYTE 1 BYTE 2
1 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0
AX [BX+SI]
89 00
CKV
Exercise Problems
Find the instruction codes for the following cases
MOD-R/M Table
MOV DL, [BX+DI+95h]
REG Table
BYTE 1 BYTE 2
1 0 0 0 1 0 1 0 0 1 0 1 0 0 0 1
DL [BX+DI+d8]
8A 51 95
CKV
Exercise Problems
Find the instruction codes for the following cases
MOD-R/M Table
MOV [BP+6745h],CX
REG Table
BYTE 1 BYTE 2
1 0 0 0 1 0 0 1 1 0 0 0 1 1 1 0
CX [BP+d16]
89 8E 45 67
CKV
Thank You
CKV
MOV [BX+SI], AX
MOV [BP+6745h],CX
CKV
MOV [BP+6745h],CX
CKV
MOD 00 01 10 11 (R)
(M) (M) (M) W=0 W=1
R/M
000 [BX+SI] [BX+SI+d8] [BX+SI+d16] AL AX
001 [BX+DI] [BX+DI+d8] [BX+DI+d16] CL CX
010 [BP+SI] [BP+SI+d8] [BP+SI+d16] DL DX
011 [BP+DI] [BP+DI+d8] [BP+DI+d16] BL BX
100 [SI] [SI+d8] [SI+d16] AH SP
101 [DI] [DI+d8] [DI+d16] CH BP
Direct Addressing 110 d16 [BP+d8] [BP+d16] DH SI
Mode
111 [BX] [BX+d8] [BX+d16] BH DI
CKV
BYTE 1 BYTE 2
1 0 0 0 1 0
MOD 00 01 10 11 (R)
(M) (M) (M) W=0 W=1
R/M
000 [BX+SI] [BX+SI+d8] [BX+SI+d16] AL AX
001 [BX+DI] [BX+DI+d8] [BX+DI+d16] CL CX
010 [BP+SI] [BP+SI+d8] [BP+SI+d16] DL DX
011 [BP+DI] [BP+DI+d8] [BP+DI+d16] BL BX
100 [SI] [SI+d8] [SI+d16] AH SP
Register Indirect 101 [DI] [DI+d8] [DI+d16] CH BP
Addressing mode 110 d16 [BP+d8] [BP+d16] DH SI
111 [BX] [BX+d8] [BX+d16] BH DI
CKV
BYTE 1 BYTE 2
1 0 0 0 1 0
MOD 00 01 10 11 (R)
(M) (M) (M) W=0 W=1
R/M
000 [BX+SI] [BX+SI+d8] [BX+SI+d16] AL AX
Base plus index 001 [BX+DI] [BX+DI+d8] [BX+DI+d16] CL CX
Addressing Mode
010 [BP+SI] [BP+SI+d8] [BP+SI+d16] DL DX
011 [BP+DI] [BP+DI+d8] [BP+DI+d16] BL BX
100 [SI] [SI+d8] [SI+d16] AH SP
101 [DI] [DI+d8] [DI+d16] CH BP
110 d16 [BP+d8] [BP+d16] DH SI
111 [BX] [BX+d8] [BX+d16] BH DI
CKV
MOD 00 01 10 11 (R)
(M) (M) (M) W=0 W=1
R/M
000 [BX+SI] [BX+SI+d8] [BX+SI+d16] AL AX
001 [BX+DI] [BX+DI+d8] [BX+DI+d16] CL CX
010 [BP+SI] [BP+SI+d8] [BP+SI+d16] DL DX
011 [BP+DI] [BP+DI+d8] [BP+DI+d16] BL BX
100 [SI] [SI+d8] [SI+d16] AH SP
Register-relative 101 [DI] [DI+d8] [DI+d16] CH BP
addressing mode 110 d16 [BP+d8] [BP+d16] DH SI
111 [BX] [BX+d8] [BX+d16] BH DI
CKV
MOD 00 01 10 11 (R)
(M) (M) (M) W=0 W=1
R/M
000 [BX+SI] [BX+SI+d8] [BX+SI+d16] AL AX
001 [BX+DI] [BX+DI+d8] [BX+DI+d16] CL CX
Base Relative plus
index addressing 010 [BP+SI] [BP+SI+d8] [BP+SI+d16] DL DX
mode 011 [BP+DI] [BP+DI+d8] [BP+DI+d16] BL BX
100 [SI] [SI+d8] [SI+d16] AH SP
101 [DI] [DI+d8] [DI+d16] CH BP
110 d16 [BP+d8] [BP+d16] DH SI
111 [BX] [BX+d8] [BX+d16] BH DI
CKV
Exercise Problems
Find the instruction codes for the following cases
MOV AX, BX
BYTE 1 BYTE 2
1 0 0 0 1 0 1 1 1 1 0 0 0 0 1 1
AX BX
8B C3
CKV
Exercise Problems
Find the instruction codes for the following cases
BYTE 1 BYTE 2
1 0 0 0 1 0 1 1 0 0 0 1 1 1 1 0
Direct Address Direct Address
OPCODE D W MOD REG R/M Low Byte High Byte
BX [d16]
8B 1E 67 20
CKV
Exercise Problems
Find the instruction codes for the following cases
MOD-R/M Table
MOV [BX+SI], AX
REG Table
BYTE 1 BYTE 2
1 0 0 0 1 0
1 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0
89 00
CKV
Exercise Problems
Find the instruction codes for the following cases
MOD-R/M Table
MOV DL, [BX+DI+95h]
REG Table
BYTE 1 BYTE 2
1 0 0 0 1 0
1 0 0 0 1 0 1 0 0 1 0 1 0 0 0 1
8A 51 95
CKV
Exercise Problems
Find the instruction codes for the following cases
MOD-R/M Table
MOV [BP+6745h],CX
REG Table
BYTE 1 BYTE 2
1 0 0 0 1 0
1 0 0 0 1 0 0 1 1 0 0 0 1 1 1 0
89 8E 45 67
CKV
1 0 1 1 1 0 1 1
BB 59 14
CKV
65000h
65001h
CKV
DI 65000h
65001h
CKV
65000h
65001h
CKV
Thank You
CKV
MOV [BX+SI], AX
MOV [BP+6745h],CX
MOV BX,1459h
CKV
MOV [BP+6745h],CX
CKV
Arithmetic Instructions
Arithmetic Instructions
SUB – Subtract
Arithmetic Instructions
Arithmetic Instructions
Register
INC Operand
Memory
INC BL ; 8-bit
INC BX ; 16-bit
CKV
Arithmetic Instructions
INC [2000h]
FFh + 1 OR 05FFh+1
Arithmetic Instructions
Register
DEC Operand
Memory
DEC BL ; 8-bit
DEC BX ; 16-bit
CKV
Arithmetic Instructions
Arithmetic Instructions
Can you add two 32-bit numbers in 8086?
1
126AB9B0h B9B0 AX
26797B31h 7B31 BX
Arithmetic Instructions
Can you add two 32-bit numbers in 8086?
11 1
126AB9B0h 126A DX B9B0 AX
26797B31h 2679 CX 7B31 BX
Arithmetic Instructions
SBB – Subtract with Borrow
ALP Example
Write an ALP to copy a block of data (e.g. 20 bytes) from one
memory area to another memory area i.e. from 32000H to
34000H Data Segment
MOV AX, 3000h
MOV DS, AX 32000h
MOV AL, [2000h] 32001h
MOV [4000h], AL
MOV AL, [2001h]
MOV [4001h], AL
AL
34000h
34001h
Will you do it for 20 times??
CKV
ALP Example
Write an ALP to copy a block of data (e.g. 20 bytes) from one
memory area to another memory area i.e. from 32000H to
34000H Data Segment
ALP Example
Write an ALP to copy a block of data (e.g. 20 bytes) from one
memory area to another memory area i.e. from 32000H to
34000H Data Segment
MOV CX, 0014h
MOV AX, 3000h
MOV DS, AX 32000h
MOV SI, 2000h SI 32001h
MOV DI, 4000h
AGAIN: MOV AL, [SI]
MOV [DI], AL Repeats but
how many AL
INC SI
INC DI times??
LOOP AGAIN
34000h
As specified DI 34001h
in CX Register
CKV
Thank You
CKV
Arithmetic Instructions
ALP Example
Write an ALP to copy a block of data (e.g. 20 bytes) from one
memory area to another memory area i.e. from 32000H to
34000H Data Segment
MOV CX, 0014h
MOV AX, 3000h
MOV DS, AX SI 32000h
MOV SI, 2000h 32001h
MOV DI, 4000h
AGAIN: MOV AL, [SI]
MOV [DI], AL AL
INC SI
INC DI
LOOP AGAIN DI 34000h
34001h
CKV
Logical Instructions
AND Destination, Source Bitwise Operations
Flags
CF and OF become zero
OR Destination, Source
PF, SF and ZF get effected
as per the result
Logical Instructions
AND Destination, Source Bitwise Operations
Flags
CF and OF become zero
OR Destination, Source
PF, SF and ZF get effected
as per the result
Logical Instructions
AND Destination, Source Bitwise Operations
Flags
CF and OF become zero
OR Destination, Source
PF, SF and ZF get effected
as per the result
Logical Instructions
CMP AX, BX
Logical Instructions
Rotate Operations
MOV CL, 04h
ROL Destination, Count ROL BL, 4 ROL BL, CL
8-bit/16-bit Immediate CF BL
register or value or CL 0 01100101
Memory Before Execution
Rotate left CF BL
0 0 1 1 0 0 1 0 1
CKV
Logical Instructions
Rotate Operations
MOV CL, 04h
ROL Destination, Count ROL BL, 4 ROL BL, CL
8-bit/16-bit Immediate CF BL
register or value or CL 0 01100101
Memory Before Execution
Rotate left CF BL
0 1 1 0 0 1 0 1 0
CKV
Logical Instructions
Rotate Operations
MOV CL, 04h
ROL Destination, Count ROL BL, 4 ROL BL, CL
8-bit/16-bit Immediate CF BL
register or value or CL 0 01100101
Memory Before Execution
Rotate left CF BL
1 1 0 0 1 0 1 0 1
CKV
Logical Instructions
Rotate Operations
MOV CL, 04h
ROL Destination, Count ROL BL, 4 ROL BL, CL
8-bit/16-bit Immediate CF BL
register or value or CL 0 01100101
Memory Before Execution
Rotate left CF BL
1 0 0 1 0 1 0 1 1
CKV
Logical Instructions
Rotate Operations
MOV CL, 04h
ROL Destination, Count ROL BL, 4 ROL BL, CL
8-bit/16-bit Immediate CF BL
register or value or CL 0 01100101
Memory Before Execution
Rotate left CF BL
0 0 1 0 1 0 1 1 0 After Execution
Logical Instructions
Rotate Operations
8-bit/16-bit Immediate
register or value or CL
Memory
Rotate Right Register/Memory CF
Logical Instructions
Rotate Operations
MOV CL, 04h
RCL Destination, Count RCL BL, 4 RCL BL, CL
8-bit/16-bit Immediate CF BL
register or value or CL 0 01100101
Memory Before Execution
Rotate left CF BL
0 0 1 1 0 0 1 0 1
CKV
Logical Instructions
Rotate Operations
MOV CL, 04h
RCL Destination, Count RCL BL, 4 RCL BL, CL
8-bit/16-bit Immediate CF BL
register or value or CL 0 01100101
Memory Before Execution
Rotate left CF BL
0 1 1 0 0 1 0 1 0
CKV
Logical Instructions
Rotate Operations
MOV CL, 04h
RCL Destination, Count RCL BL, 4 RCL BL, CL
8-bit/16-bit Immediate CF BL
register or value or CL 0 01100101
Memory Before Execution
Rotate left CF BL
1 1 0 0 1 0 1 0 0
CKV
Logical Instructions
Rotate Operations
MOV CL, 04h
RCL Destination, Count RCL BL, 4 RCL BL, CL
8-bit/16-bit Immediate CF BL
register or value or CL 0 01100101
Memory Before Execution
Rotate left CF BL
1 0 0 1 0 1 0 0 1
CKV
Logical Instructions
Rotate Operations
MOV CL, 04h
RCL Destination, Count RCL BL, 4 RCL BL, CL
8-bit/16-bit Immediate CF BL
register or value or CL 0 01100101
Memory Before Execution
Rotate left CF BL
1 0 1 0 1 0 0 1 1 After Execution
Logical Instructions
Rotate Operations
8-bit/16-bit Immediate
register or value or CL
Memory
Rotate Right Register/Memory CF
Logical Instructions
Shift Operations
Logical Instructions
Shift Operations
Logical Instructions
Shift Operations
Logical Instructions
Shift Operations
Logical Instructions
Shift Operations
Logical Instructions
Shift Operations
Thank You
CKV
Logical Instructions
AND CMP ROL SAL/SHL
If Dst > Src
OR CF=0 and ZF=0
ROR SAR
If Dst < Src
XOR CF=1 and ZF=0
RCL SHR
If Dst = Src
CF=0 and ZF=1 RCR
TEST (bitwise AND, affects flags but does not change destination)
NOT (invert bits, e.g. NOT AL, NOT AX, NOT BYTE PTR [BX] etc)
NEG (2’s complement, e.g. NEG AL, NEG AX, NEG WORD PTR [BX] etc)
CKV
Branch Instructions
Branch Instructions
Branch Instructions
If the jump destination is beyond -128 to +127
Branch Instructions
Branch Instructions
BX 3412
CS: IP Inst Code
1000:0000 B1 03 BEGIN: MOV CL, 03
1000:0002 8A D1 MOV DL, CL
1000:0004 FF E3 JMP BX
CS:IP 1000:0006
…..
…
1000:3412 New IP = BX
CKV
Branch Instructions
Branch Instructions
Thank You
CKV
Branch Instructions
Branch Instructions
Branch Instructions
If the jump destination is beyond -128 to +127
NEAR JUMP (jump with 16-bit displacement)
Addr. Inst Code
CS:IP 1000:0000 B1 03 BEGIN: MOV CL, 03
CS:IP 1000:0002 8A D1 MOV DL, CL
CS:IP 1000:0004 E9 07 01 JMP NEAR THERE JMP 010E
CS:IP 1000:0007 …….
+107 How far? …….
CS:IP 1000:010E B2 02 THERE: MOV DL, 02
CS:IP 1000:0110 B5 02 MOV CH, 02
CS:IP 1000:0112
New IP = Current IP + 16-bit Displacement
Branch Instructions
BX 3412
CS: IP Inst Code
1000:0000 B1 03 BEGIN: MOV CL, 03
1000:0002 8A D1 MOV DL, CL
1000:0004 FF E3 JMP BX
CS:IP 1000:0006
…..
…
1000:3412 New IP = BX
CKV
Branch Instructions
Branch Instructions
Branch Instructions
Branch Instructions
Branch Instructions
Branch Instructions
Branch Instructions
FAR JUMP
Destination not in current segment
Branch Instructions
FAR JUMP
Branch Instructions
Jumps to 7010h:1000h
CKV
Branch Instructions
Branch Instructions
LOOPNE/LOOPNZ
LOOPE/LOOPZ
ALP Example
ALP Example
Write a program that counts the number of 1’s in a byte
stored at data memory location whose offset is 5000h and
stores the count in BL
MOV BL, 00
MOV DL, 08
MOV AL, [5000h]
L1: ROL AL, 1
JNC L2
INC BL
L2: DEC DL
JNZ L1
CKV
Thank You
CKV
Branch Instructions
String Instructions
The x86 processor architectures are equipped with
Special Instructions to handle string operations
Operations:
Move
Scan
Compare
CKV
String Instructions
Data Segment
MOVSB/MOVSW
Copies a byte or a word from a
location in data segment to a
location in extra segment
Extra Segment
CKV
String Instructions
Data Segment
MOVSB/MOVSW
Offset address of Source byte or DS:SI
word (in data segment) must be in
SI Register
String Instructions
Data Segment
MOVSB/MOVSW
SI and DI will increment/decrement DS:SI
Lower Address
DS:SI
by 1 (for Byte transfer) or 2 (for
Word transfer) automatically
Higher Address
String Instructions
Data Segment
MOVSB/MOVSW DS:SI
SI and DI will increment/decrement DS:SI
Lower Address
STD
Higher Address
CKV
String Instructions
Data Segment
MOVSB
DS:SI
If DF = 0 DS:SI
12
Move byte from DS:SI to ES:DI
SI = SI + 1
DI = DI + 1
Extra Segment
ES:DI
ES:DI 45
CKV
String Instructions
Data Segment
MOVSB DS:SI
DS:SI
If DF = 1
12
Move byte from DS:SI to ES:DI
SI = SI – 1
DI = DI – 1
ES:DI Extra Segment
ES:DI
45
CKV
String Instructions
Data Segment
MOVSW
DS:SI
If DF = 0
12
DS:SI 89
Move word from DS:SI to ES:DI
SI = SI + 2
DI = DI + 2
Extra Segment
If DF = 1
ES:DI
Move word from DS:SI to ES:DI 45
SI = SI – 2 ES:DI 56
DI = DI – 2
CKV
String Instructions
String Instructions often used with REP instruction
Example :
REP MOVSB
String Instructions
Write ALP to move 5 bytes of data from one array whose starting
address is 5000h:1000h to another array whose starting address
is 5000h:2000h using string instructions
CLD
REP MOVSB
CKV
String Instructions
Write ALP to move 5 bytes of data from one array
(ARRAY1) to the other (ARRAY2)
.model small
.data
ARRAY1 db 11h, 22h, 33h, 44h, 55h
ARRAY2 db 0, 0, 0, 0, 0 MOV AX, DS
.code MOV ES, AX
.startup LEA SI, ARRAY1
; code here
MOV DI, Offset ARRAY2
MOV CX, 05h
.exit CLD
end REP MOVSB
CKV
String Instructions
SCASB
DI gets auto-incremented/auto-decremented by 1
depending on the value of DF
CKV
String Instructions
Data Segment
SCASB If DF = 0
Lower Address
AL 85
Higher Address
Compare
String Instructions
SCASW
DI gets auto-incremented/auto-decremented by 2
depending on the value of DF
CKV
String Instructions
Data Segment
SCASW If DF = 0
Lower Address
AX 1485
Higher Address
Compare
Thank You
CKV
String Instructions
MOVSB/MOVSW
CLD
STD
REP
SCASB/SCASW
CKV
String Instructions
Data Segment
MOVSB
DS:SI
If DF = 0 DS:SI
12
Move byte from DS:SI to ES:DI
SI = SI + 1
DI = DI + 1
Extra Segment
ES:DI
ES:DI 45
CKV
String Instructions
Data Segment
MOVSB DS:SI
DS:SI
If DF = 1
12
Move byte from DS:SI to ES:DI
SI = SI – 1
DI = DI – 1
ES:DI Extra Segment
ES:DI
45
CKV
String Instructions
Data Segment
MOVSW
DS:SI
If DF = 0
12
DS:SI 89
Move word from DS:SI to ES:DI
SI = SI + 2
DI = DI + 2
Extra Segment
If DF = 1
ES:DI
Move word from DS:SI to ES:DI 45
SI = SI – 2 ES:DI 56
DI = DI – 2
CKV
String Instructions
SCASB/SCASW
String Instructions
Data Segment
SCASB If DF = 0
Lower Address
AL 85
Higher Address
Compare
String Instructions
Data Segment
SCASW If DF = 0
Lower Address
AX 1485
Higher Address
Compare
ALP Example
Write ALP to search for a byte 35h in a given string of 50 bytes
with starting address of the string as 5000h:0000h. At the end
of the program BL should be equal to 1 if the key is found and 0
if key not found
MOV AX, 5000h
MOV ES, AX
MOV DI, 0000h
MOV CX, 32h ; 50 in hexadecimal is 32h
MOV BL, 0
CLD
MOV AL, 35h
REPNE SCASB
JNE EXIT
MOV BL, 1
EXIT: .EXIT
CKV
String Instructions
LODSB/LODSW
String Instructions
Data Segment
LODSB If DF = 0 DS:SI Extra Segment
DS:SI Lower Address
Lower32
Address
AL 85
32 45
67
79
Higher Address
Extra Segment
Higher Address
CKV
String Instructions
Data Segment
LODSW If DF = 0 DS:SI
Lower Address
Lower32
Address
DS:SI 45
AX 1485
4532
67
Higher79
Address
Extra Segment
Higher Address
CKV
String Instructions
STOSB/STOSW
String Instructions
Data Segment
STOSB If DF = 0
AL 85
Extra Segment
ES:DI Lower Address
ES:DI Lower32
85
Address
45
67
HigherAddress
Address
Higher79
CKV
String Instructions
Data Segment
STOSW If DF = 0
AX 1485
Higher
HigherAddress
Address
Extra Segment
ES:DI
Lower Address
Lower32
85
Address
ES:DI 45
14
67
79
CKV
String Instructions
Data Segment
STOSW If DF = 1
AX 1485
Higher
HigherAddress
Address
Extra Segment
ES:DI
Lower
LowerAddress
Address
67
ES:DI 79
32
85
45
14
CKV
String Instructions
CMPSB/CMPSW
String Instructions
Data Segment
CMPSB If DF = 0 DS:SI
DS:SI 32
45
67
Compare 79
Extra Segment
ES:DI Lower Address
ES:DI Lower32
Address
45
67
HigherAddress
Address
Higher85
CKV
String Instructions
Data Segment
CMPSW If DF = 0 DS:SI
32
DS:SI 45
67
Compare 79
Extra Segment
ES:DI Lower Address
Lower32
Address
ES:DI 45
67
HigherAddress
Address
Higher85
CKV
ALP Example
Write ALP to check if the string (of 20 bytes) stored at location
5000h:0000h is same as the string stored at 6000h:0000h. Store 1
in BL if strings are equal else store 0 in BL
MOV AX, 5000h
MOV DS, AX
MOV AX, 6000h
MOV ES, AX
MOV SI, 0000h
MOV DI, 0000h
MOV CX, 14h
MOV BL, 0
CLD
REPE CMPSB
JNE EXIT
MOV BL, 1
EXIT: .EXIT
CKV
Thank You
CKV
String Instructions
MOVSB/MOVSW
CLD
STD
SCASB/SCASW
REP
REPE
LODSB/LODSW
REPNE
STOSB/STOSW
CMPSB/CMPSW
CKV
String Instructions
Data Segment
DS:SI Extra Segment
LODSB If DF = 0 DS:SI Lower Address
Lower32
Address
45
AL 85
32 67
79
Higher Address
Loads AL with the data stored in the
data segment with offset address in SI
Extra Segment
register and increment SI
Equivalent to
MOV AL, [SI] Higher Address
INC SI
Except for changes in Flags
CKV
String Instructions
Data Segment
STOSB If DF = 0
AL 85
Stores contents of AL into
the extra segment with
Extra Segment
offset address in DI
ES:DI
register ES:DI
Lower Address
Lower32
85
Address
Almost Equivalent to 45
MOV ES:[DI], AL 67
MOV [DI], AL Higher
Higher79Address
Address
INC DI INC DI
Segment Override
CKV
Segment Override
Allows for the use of a different segment
register than the default
Segment Override
Allows for the use of a different segment
register than the default
Segment Override
What will be the effect on instruction codes?
Segment Override
Segment Override Prefix
0 0 1 1 1 0
SEG
MOV AL, [BX+SI] 8A 00
ES 0 0 MOV AL, SS:[BX+SI] 36 8A 00
CS 0 1
MOV AL, ES:[BX+SI] ?? 8A 00
SS 1 0
DS 1 1
CKV
Other Instructions
Most of the instructions discussed till now will
exist in some or the other form in most of the
processors
Special Instructions
DAA
Decimal Adjust AL after Addition
BCD Representation:
25d Binary: 0001 1001 19h
25d BCD: 0010 0101
CKV
Special Instructions
DAA
Decimal Addition BCD Addition
23 0010 0011
27 0010 0111
50 0100 1010 ADD
Expected Result 0000 0110 DAA
0101 0000
CKV
Special Instructions
DAA Categorized as Arithmetic Instruction
Special Instructions
DAS
Decimal Adjust AL after Subtraction
Special Instructions
XLAT
AL [BX+AL]
CKV
Special Instructions
XLAT Number ASCII
0 30h
Used for table lookup
1 31h
2 32h
3 33h
4 34h
5 35h
6 36h
7 37h
8 38h
9 39h
CKV
Special Instructions
XLAT DS:BX
2000h:1000h 30h
Used for table lookup
31h
MOV AX, 2000h 32h
MOV DS, AX 33h
+8
MOV BX, 1000h 34h
MOV AL, 8 35h
AL=38 36h
XLAT
37h
38h
39h
CKV
Special Instructions
LAHF
AH
CKV
Special Instructions
SAHF
AH
CKV
Special Instructions
Try this in emu8086
MOV AH, 0F0h
SAHF
LAHF
AH=??
CKV
Thank You
CKV
Special Instructions
DAS
XLAT
LAHF
SAHF
CKV
Special Instructions
Write an ALP to add two BCD numbers
MOV AL, 64h ;AL = 0110 0100 BCD
MOV BL, 29h ;BL = 0010 1001 BCD
ADD AL, BL ;AL = 1000 1101 (AF=0) (CF=0)
DAA ;AL = 1001 0011
CKV
Stack Instructions
What is a Stack?
Stack Instructions
PUSH
Stack Instructions
Stack Segment
PUSH Lower Address
Stack grows in
negative direction
Stack Instructions
Stack Segment
PUSH Lower Address
Stack grows in
PUSH AX negative direction
Stack Instructions
POP
Stack Instructions
Stack Segment
POP Lower Address
BX 1543
1367 Higher Address
Stack Instructions
What is the use of Stack Instructions?
Example:
AX gets changed
POP AX
Original AX value needed
CKV
Stack Instructions
Write an ALP to exchange contents of AX and BX registers
using stack instructions
Stack Segment
AX 1367 Lower Address
BX 2389
Higher Address
17
PUSH AX 12
15
SS:SP 19
67
21
13
SS:SP 83
83
CKV
Stack Instructions
Write an ALP to exchange contents of AX and BX registers
using stack instructions
Stack Segment
AX 1367 Lower Address
BX 2389
Higher Address
17
PUSH AX SS:SP 12
89
15
23
PUSH BX 67
SS:SP
13
83
83
CKV
Stack Instructions
Write an ALP to exchange contents of AX and BX registers
using stack instructions
Stack Segment
AX 2389
1367 Lower Address
BX 1367
2389
Higher Address
17
PUSH AX SS:SP 89
23
PUSH BX SS:SP 67
POP AX 13
POP BX SS:SP 83
83
CKV
Stack Instructions
Write an ALP to exchange contents of AX and BX registers
using stack instructions (Show SS and SP after each inst)
Stack Segment
AX 1367 Lower Address
BX 2389
Higher Address
17
PUSH AX 12
15
1000:2FFE 19
67
21
13
Assume SS = 1000h 1000:3000 83
SP = 3000h initially 83
CKV
Stack Instructions
Write an ALP to exchange contents of AX and BX registers
using stack instructions
Stack Segment
AX 1367 Lower Address
BX 2389
Higher Address
17
PUSH AX 1000:2FFC 12
89
15
23
PUSH BX 67
1000:2FFE
13
83
83
CKV
Stack Instructions
Write an ALP to exchange contents of AX and BX registers
using stack instructions
Stack Segment
AX 2389
1367 Lower Address
BX 1367
2389
Higher Address
17
PUSH AX 1000:2FFC 89
23
PUSH BX 1000:2FFE 67
POP AX 13
POP BX 1000:3000 83
83
CKV
Stack Instructions
PUSHF
Stores 16-bit flag register to the top of Stack
POPF
Transfers 16-bit data from top of the Stack to
the Flag register
CKV
Stack Instructions
LODSB If DF = 0
Equivalent to
MOV AL, [SI]
INC SI
Thank You
CKV
Special Instructions
PUSH
POP
PUSHF
POPF
CKV
Branch Instructions
FAR JUMP
Branch Instructions
Jumps to 7010h:1000h
CKV
Procedures
CALL
‘CALL’ instruction in the program
Procedures
RET
‘RET’ instruction in the program
Procedures
Main Program
FUNC PROC NEAR
Normal Execution
CALL FUNC
MOV AL, 12
RET
ENDP
CKV
Procedures
Main Program
FUNC2 PROC NEAR
FUNC PROC NEAR
Normal Execution
CALL FUNC2
CALL FUNC
ADD AL, 12
MOV AL, 12
RET
RET
ENDP
ENDP
CKV
Procedures
CALL and RET
Stack Segment
Near CALL (intrasegment) Direct Call Lower Address
Addr.
CS:IP 1000:0000 BEGIN: MOV CL, 03
CS:IP 1000:0002 MOV DL, CL Higher Address
CS:IP 1000:0004 E8 07 01 CALL FUNC
CS:IP 1000:0007 MOV AL, 12 17
+107 How far?
……. SS:SP 07
32
FUNC PROC NEAR 00
45
CS:IP 1000:010E MOV DL, 02 19
SS:SP
CS:IP 1000:0110 MOV CH, 02 21
CS:IP 1000:0112 RET 83
CS:IP ENDP 83
CKV
Procedures
CALL
Near CALL (intrasegment) Indirect Call
Procedures
CALL
Stack Segment
FAR CALL (intersegment) Direct Call
Addr. Inst Code
1000:0000
CS:IP B1 03 BEGIN: MOV CL, 03
CS:IP
1000:0002 8A D1 MOV DL, CL
CS:IP
1000:0004 9A 0E 01 00 20 CALL FUNC
1000:0009
CS:IP …….
FUNC PROC FAR 17
CS:IP 2000:010E B2 02 MOV DL, 02 SS:SP 32
09
CS:IP 2000:0110 Update IP RET 45
00
Update CS SS:SP 00
19
10
21
SS:SP 83
83
CKV
Procedures
CALL
Far CALL (Intersegment) Indirect Call
Thank You
CKV
Procedures
CALL Near CALL (intrasegment)
Direct Call
CALL Label
Indirect Call
Procedures
CALL and RET
Stack Segment
Near CALL (intrasegment) Direct Call Lower Address
Addr.
CS:IP 1000:0000 BEGIN: MOV CL, 03
CS:IP 1000:0002 MOV DL, CL Higher Address
CS:IP 1000:0004 E8 07 01 CALL FUNC
CS:IP 1000:0007 MOV AL, 12 17
+107 How far?
……. SS:SP 07
32
FUNC PROC NEAR 00
45
CS:IP 1000:010E MOV DL, 02 19
SS:SP
CS:IP 1000:0110 MOV CH, 02 21
Update IP 83
CS:IP 1000:0112 RET
CS:IP ENDP 83
CKV
Procedures
CALL and RET
Stack Segment
FAR CALL (intersegment) Direct Call
Addr. Inst Code
1000:0000
CS:IP B1 03 BEGIN: MOV CL, 03
CS:IP
1000:0002 8A D1 MOV DL, CL
CS:IP
1000:0004 9A 0E 01 00 20 CALL FUNC
1000:0009
CS:IP …….
FUNC PROC FAR 17
CS:IP 2000:010E B2 02 MOV DL, 02 SS:SP 32
09
CS:IP 2000:0110 Update IP RET 45
00
Update CS SS:SP 00
19
10
21
SS:SP 83
83
CKV
Procedures
CALL
Far CALL (Intersegment) Indirect Call
BX 3000h
DS:3000h 00
DS:3001h 11
Procedure address: DS:3002h 80
7080h:1100h 70
DS:3003h
CKV
Procedures
RET n
This adds the number ‘n’ to the stack pointer after the
return address is popped off the stack
For example
ALP Example
Can I access data on Stack without changing SP?
.model small
Stack Segment
.stack 100h
.data COMPUTE PROC NEAR
A DW 0987H MOV BP,SP Passing Parameters
MOV AX, [BP + 4] through Stack
B DW 678H
Result DW ? MOV BX, [BP + 2] SS:BP SS:SP 0710:00FA 10
62
.code ADD AX, BX +2 0710:00FB 17
00
RET SS:SP 0710:00FC 78
32
.startup +4 0710:00FD
PUSH A COMPUTE ENDP 06
45
END SS:SP 0710:00FE 87
19
PUSH B 0710:00FF 21
09
CALL COMPUTE SS:SP 0710:0100 83
MOV RESULT, AX ;Assume address next instruction 83
.EXIT is 0721:0010
(Note: In this example DS=0720h, CS=0721h and SS =0710h)
CKV
ALP Example
What will happen if you keep
.model small pushing variables on to stack?? Stack Segment
.stack 100h
.data COMPUTE PROC NEAR
A DW 0987H MOV BP,SP
B DW 678H MOV AX, [BP + 4]
Result DW ? MOV BX, [BP + 2] SS:BP 0710:00FA 10
.code ADD AX, BX 0710:00FB 00
.startup RET 4 SS:SP 0710:00FC 78
COMPUTE ENDP 0710:00FD 06
PUSH A 0710:00FE 87
PUSH B END
0710:00FF 09
CALL COMPUTE SS:SP 0710:0100 83
MOV RESULT, AX ;Assume address next instruction 83
.EXIT is 0721:0010 (Note: In this example DS=0720h, CS=0721h and SS =0710h)
CKV
Passing Parameters
.model small Passing parameters through Registers
.stack 100h
.data COMPUTE PROC NEAR
A DW 0987H MUL AX
B DW 678H ADD AX, BX
Result DW ? ADC DX, 0
.code RET
.startup COMPUTE ENDP
MOV AX, A END
MOV BX, B
CALL COMPUTE
MOV RESULT, AX
.EXIT
CKV
Passing Parameters
.model small Passing parameters through Memory or memory pointers
.stack 100h
.data ArrayADD PROC NEAR
N DW 05H ADD [BX], 5
Arr Db 67H, 23H, 45h, 89h, INC BX
78h RET
.code ArrayADD ENDP
.startup END
LEA BX, Arr
MOV CX, N
L1: CALL ArrayADD
LOOP L1
.EXIT
CKV
ALP Example
Write factorial of a number using recursion of procedure calls. Assume
that the input is available in the memory location and the factorial nees to
be stored in the next location.
The recursion formula for factorial is:
fact(n) = 1, if n = 0,
= n * fact(n-1), if n > 0.
CKV
Thank You
CKV
Procedures
Example:
.model small
.data
DATA1 DW 10 DUP (5) Reserve 10 words in memory with name
DATA1 and initialize all 10 words with 0005
DATA2 DW 10 DUP (?) Reserve 10 words in memory with name
DATA2. Leave the words uninitialized
(default value is 0)
CKV
Assembler Directives
Directives for Equating
Equate (EQU) directive equates a symbolic name to a value
Example:
COUNT EQU 10
CONST EQU 20H
MOV AH, 10
MOV AH, COUNT
MOV AL, CONST
MOV AL, 20H
CKV
Assembler Directives
Directives for changing memory offset
ORG (originate) statement changes the starting offset address of the data
in the data segment to a desired location
Data Segment
DATA1 DB 25 ; assume DS = 0100
DATA1 0100:0000 19h
DATA2 DB 10001011b
DATA2 0100:0001 8Bh
DATA3 DB 12h DATA3 0100:0002 12h
ORG 0010h DATA4 0100:0010 32h
COUNT EQU 32h Not stored in Memory 0100:0011 61h
ASCII Values 0100:0012 39h
DATA4 DB ‘2a91’
0100:0013 31h
ORG 0018h
DATA5 DB ? DATA5 0100:0018 00h
CKV
MACROS
A Macro is a group of instructions that perform task
All the instructions defined in the macro are inserted in the program at the
point of usage
Example:
MyMacro MACRO p1, p2, p3 ORG 100h MOV AX, 0004h
MOV AX, p1 MyMacro 4, DX, [BX] MOV BX, DX
MOV BX, p2 RET MOV CX, [BX]
MOV CX, p3
ENDM
CKV
MACROS
In-built Macros that you have been using already in the lab sessions
In EMU 8086
Other models
Medium
Compact
Large
Huge
CKV
Concept of Pseudo-instructions
Pseudo instructions are those which are not part of processor ISA but used
in Assemblers to simplify programming and debugging
Pseudo instructions get converted to actual instructions when you assemble
the code
.model small
.data ; assume Data segment starts at 0710:0000
VAR1 DB 45H, 35H, 74H
NUM DB 56h
.code
MOV BX, 0000
.startup
LEA BX, VAR1 MOV SI, 0003
LEA SI, NUM
CKV
Concept of Pseudo-instructions
Pseudo instructions are those which are not part of processor ISA but used
in Assemblers to simplify programming and debugging
Pseudo instructions get converted to actual instructions when you assemble
the code
Example:
SHR AX, 4 If v=0 then count is 1,
if v=1 then count in CL
SHR AX, 1
SHR AX, 1
SHR AX, 1
SHR AX, 1
CKV
Thank You
CKV
ALP Example
Write factorial of a number using recursion of procedure calls. Assume
that the input is available in the memory location and the factorial nees to
be stored in the next location.
The recursion formula for factorial is:
fact(n) = 1, if n = 0,
= n * fact(n-1), if n > 0.
CKV
ALP Example : Factorial using Recursion
.MODEL SMALL
.STACK
.DATA
N DW 4
RES DW 1
.CODE
.STARTUP
MOV AX, N
CALL FACT
.EXIT SP
CKV
ALP Example : Factorial using Recursion
FACT PROC NEAR
.MODEL SMALL CMP AX, 01
.STACK JNZ L1
.DATA MOV RES, 01
N DW 4 RET
RES DW 1
.CODE
.STARTUP
MOV AX, N
SP 0B
CALL FACT
00
0720:000B: .EXIT SP
CKV
ALP Example : Factorial using Recursion
FACT PROC NEAR
.MODEL SMALL CMP AX, 01
.STACK JNZ L1
.DATA MOV RES, 01
N DW 4 RET
RES DW 1 L1: PUSH AX
.CODE DEC AX ;AX=3
.STARTUP
SP 04
MOV AX, N 00
SP 0B
CALL FACT
00
0721:000B: .EXIT
CKV
ALP Example : Factorial using Recursion
FACT PROC NEAR
.MODEL SMALL CMP AX, 01
.STACK JNZ L1
.DATA MOV RES, 01
N DW 4 RET
RES DW 1 L1: PUSH AX
.CODE DEC AX ;AX=3
SP 21
.STARTUP CALL FACT 00
0721:0021:
SP 04
MOV AX, N 00
CALL FACT 0B
00
0721:000B: .EXIT
CKV
ALP Example : Factorial using Recursion
FACT PROC NEAR
.MODEL SMALL CMP AX, 01
.STACK JNZ L1
.DATA MOV RES, 01
N DW 4 RET
RES DW 1 SP 03
L1: PUSH AX
00
.CODE DEC AX ;AX=2
SP 21
.STARTUP CALL FACT 00
0721:0021 :
04
MOV AX, N 00
CALL FACT 0B
00
0721:000B: .EXIT
CKV
ALP Example : Factorial using Recursion
FACT PROC NEAR
.MODEL SMALL CMP AX, 01
.STACK JNZ L1
.DATA MOV RES, 01 21
SP
N DW 4 RET 00
RES DW 1 SP 03
L1: PUSH AX
00
.CODE DEC AX ;AX=2
21
.STARTUP CALL FACT 00
0721:0021 :
04
MOV AX, N 00
CALL FACT 0B
00
0721:000B: .EXIT
CKV
ALP Example : Factorial using Recursion
FACT PROC NEAR
.MODEL SMALL CMP AX, 01
SP 02
.STACK JNZ L1
00
.DATA MOV RES, 01 21
SP
N DW 4 RET 00
RES DW 1 03
L1: PUSH AX
00
.CODE DEC AX ;AX=1
21
.STARTUP CALL FACT 00
0721:0021 :
04
MOV AX, N 00
CALL FACT 0B
00
0721:000B: .EXIT
CKV
ALP Example : Factorial using Recursion
SP 21
FACT PROC NEAR 00
.MODEL SMALL CMP AX, 01
SP 02
.STACK JNZ L1
00
.DATA MOV RES, 01 21
N DW 4 RET 00
RES DW 1 03
L1: PUSH AX
00
.CODE DEC AX ;AX=1
21
.STARTUP CALL FACT 00
0721:0021 :
04
MOV AX, N 00
CALL FACT 0B
00
0721:000B: .EXIT
CKV
ALP Example : Factorial using Recursion
SP 21
FACT PROC NEAR 00
.MODEL SMALL CMP AX, 01
SP 02
.STACK JNZ L1
00
.DATA MOV RES, 01 21
N DW 4 RET 00
RES DW 1 03
L1: PUSH AX
00
.CODE DEC AX ;AX=1
21
.STARTUP CALL FACT 00
0721:0021 :
04
MOV AX, N 00
CALL FACT 0B
00
0721:000B: .EXIT
CKV
ALP Example : Factorial using Recursion
FACT PROC NEAR
.MODEL SMALL CMP AX, 01
SP 02
.STACK JNZ L1
00
.DATA MOV RES, 01 21
SP
N DW 4 RET 00
RES DW 1 03
L1: PUSH AX
00
.CODE DEC AX
21
.STARTUP CALL FACT 00
0721:0021 : POP AX ;AX=2
04
MUL RES ;AX = RES * 2
MOV AX, N 00
MOV RES,AX ;RES = 2
CALL FACT 0B
RET
00
0721:000B: .EXIT
CKV
ALP Example : Factorial using Recursion
FACT PROC NEAR
.MODEL SMALL CMP AX, 01
.STACK JNZ L1
.DATA MOV RES, 01 21
SP
N DW 4 RET 00
RES DW 1 SP 03
L1: PUSH AX
00
.CODE DEC AX
21
.STARTUP CALL FACT 00
0721:0021 : POP AX
04
MUL RES
MOV AX, N 00
MOV RES,AX
CALL FACT 0B
RET
00
0721:000B: .EXIT
CKV
ALP Example : Factorial using Recursion
FACT PROC NEAR
.MODEL SMALL CMP AX, 01
.STACK JNZ L1
.DATA MOV RES, 01
N DW 4 RET
RES DW 1 SP 03
L1: PUSH AX
00
.CODE DEC AX
SP 21
.STARTUP CALL FACT 00
0721:0021 : POP AX ;AX=3
04
MUL RES ;AX = RES * 3
MOV AX, N 00
MOV RES,AX ;RES = 6
CALL FACT 0B
RET
00
0721:000B: .EXIT
CKV
ALP Example : Factorial using Recursion
FACT PROC NEAR
.MODEL SMALL CMP AX, 01
.STACK JNZ L1
.DATA MOV RES, 01
N DW 4 RET
RES DW 1 L1: PUSH AX
.CODE DEC AX
SP 21
.STARTUP CALL FACT 00
0721:0021 : POP AX
SP 04
MUL RES
MOV AX, N 00
MOV RES,AX
CALL FACT 0B
RET
00
0721:000B: .EXIT
CKV
ALP Example : Factorial using Recursion
FACT PROC NEAR
.MODEL SMALL CMP AX, 01
.STACK JNZ L1
.DATA MOV RES, 01
N DW 4 RET
RES DW 1 L1: PUSH AX
.CODE DEC AX
.STARTUP CALL FACT
0721:0021 : POP AX ;AX=4 SP 04
MUL RES ;AX = RES * 4
MOV AX, N 00
MOV RES,AX ;RES = 24 SP
CALL FACT 0B
RET
00
0721:000B: .EXIT
CKV
ALP Example : Factorial using Recursion
FACT PROC NEAR
.MODEL SMALL CMP AX, 01
.STACK JNZ L1
.DATA MOV RES, 01
N DW 4 RET
RES DW 1 L1: PUSH AX
.CODE DEC AX
.STARTUP CALL FACT
0721:0021 : POP AX
MUL RES
MOV AX, N MOV RES,AX SP 0B
CALL FACT RET
00
0721:000B: .EXIT SP
CKV
Thank You