Lect 03 - MIT Addressing Modes PDF
Lect 03 - MIT Addressing Modes PDF
(TE-309)
Engr. Adeel Asghar
Lecture 03 Addressing Modes
Prophet Muhammad ﷺsaid,
5
Addressing modes
7
3.1 Data Addressing mode
• Register Addressing
• Immediate Addressing
• Direct Data Addressing
• Register Indirect Addressing
• Base plus Index Addressing
• Register relative Addressing
• Base relative plus index Addressing
• Scaled index Addressing
DATA-ADDRESSING MODES
9
Register Addressing
10
In 80386 & above, extended 32-bit register
names are: EAX, EBX, ECX, EDX, ESP,
EBP, EDI, and ESI.
• 64-bit mode register names are: RAX, RBX,
RCX, RDX, RSP, RBP, RDI, RSI, and R8
through R15.
• Important for instructions to use registers that
are the same size.
– never mix an 8-bit \with a 16-bit register, an 8- or
a 16-bit register with a 32-bit register
– this is not allowed by the microprocessor and
results in an error when assembled
11
Fig.3.3 The effect of executing the MOV BX, CX instruction at the point just before
the BX register changes. Note that only the rightmost 16 bits of register EBX
change.
12
Immediate Addressing
13
• Figure 3–4 The operation of the MOV EAX,3456H instruction. This
instruction copies the immediate data (13456H) into EAX.
14
• In symbolic assembly language, the symbol #
precedes immediate data in some assemblers.
– MOV AX,#3456H instruction is an example
• Most assemblers do not use the # symbol, but
represent immediate data as in the
MOV AX,3456H instruction.
– an older assembler used with some Hewlett-
Packard logic development does, as may others
15
• The symbolic assembler portrays immediate
data in many ways.
• The letter H appends hexadecimal data.
• If hexadecimal data begin with a letter, the
assembler requires the data start with a 0.
– to represent a hexadecimal F2, 0F2H is used in
assembly language
• Decimal data are represented as it is and
requires no special codes or adjustments.
– an example is the 100 decimal in the
MOV AL,100 instruction
16
An ASCII-coded character or characters may be
depicted in the immediate form if the ASCII data
are enclosed in apostrophes.
– be careful to use the apostrophe (‘) for ASCII
data and not the single quotation mark (`)
• Binary data are represented if the binary
number is followed by the letter B.
– in some assemblers, the letter Y
17
Direct Data Addressing
18
Direct Addressing
19
• This instruction transfers a copy contents of memory
location 11234H into AL.
– the effective address is formed by adding 1234H (the
offset address) and 10000H (the data segment address of
1000H times 10H) in a system operating in the real mode
20
Direct addressing
Register Indirect Addressing
22
23
Base-Plus-Index Addressing
24
25
Locating Array Data Using Base-
Plus-Index Addressing
• A major use is to address elements in a memory
array.
• To accomplish this, load the BX register (base) with
the beginning address of the array and the DI
register (index) with the element number to be
accessed.
• Figure 3–9 shows the use of BX and DI to access an
element in an array of data.
26
27
Register Relative Addressing
28
+
29
Addressing Array Data with
Register Relative
• It is possible to address array data with register
relative addressing.
– such as with base-plus-index addressing
• In Figure 3–11, register relative addressing is
illustrated with the same example as for
baseplus-index addressing.
– this shows how the displacement ARRAY adds to
index register DI to generate a reference to an array
element
30
31
Base Relative-Plus-Index
Addressing
• Similar to base-plus-index addressing.
– adds a displacement
– uses a base register and an index register to form
the memory address
• This type of addressing mode often addresses a
two-dimensional array of memory data.
32
Addressing Data with Base
Relative-Plus-Index
• Least-used addressing mode.
• Figure 3–12 shows how data are referenced if the
instruction executed by the microprocessor is MOV
AX,[BX + SI + 100H].
– displacement of 100H adds to BX and SI to form
the offset address within the data segment
• This addressing mode is too complex for frequent
use in programming.
33
34
Addressing Arrays with Base
Relative-Plus-Index
• Suppose a file of many records exists in memory,
each record with many elements.
– displacement addresses the file, base register
addresses a record, the index register addresses an
element of a record
• Figure 3–13 illustrates this very complex form of
addressing.
35
36
Scaled-Index Addressing
37
Task
• MOV [7000H],CX
• Which addressing does instruction above belong, and why?
Memory Addressing Modes
Memory addressing modes (Architectural)
Real Mode Memory addressing
Protected mode Memory Addressing
Flat Mode Memory addressing
EA (Effective Address)
20-bit (5-byte) 64 KB +
Physical Segment
16-bit
Memory address
Appended bit 0H
43
Effective Address Calculations
• EA = segment register (SR) x 10H plus offset
(a) SR: 1000H
10000 + 0023 = 10023
(b) SR: AAF0H
AAF00 + 0134 = AB034
(c) SR: 1200H
12000 + FFF0 = 21FF0
44
Defaults
• CS for program (code)
• SS for stack
• DS for data
• ES for string destination
• Default offset addresses that go with them:
Top of CS:
090F0
FFFF+
190EF
46
Segmentation: Pros and Cons
Advantages:
• Allows easy and efficient relocation of code and data
• A program can be located anywhere in memory without
any change to the program
• Program writing needs not worry about actual memory
structure of the computer used to execute it
• To relocate code or data only the segment number
needs to be changed
Disadvantages:
• Complex hardware and for address generation
• Software: Programs limited by segment size
(only 64KB with the 8086)
47
Limitations of the above real mode segmentation scheme
80286 and above use 24, 32, 36 bit addresses but still
16-bit segment registers
→ Use memory segmentation in the protected mode
48
Segment and Offset Addressing Scheme Allows
Relocation
• Segment plus offset addressing allows DOS programs to be relocated in
memory.
• A relocatable program is one that can be placed into any area of memory and
executed without change.
• Relocatable data are data that can be placed in any area of memory and used
without any change to the program.
49
Protected Mode Memory Addressing
51
The offset address is still used as previously, to find the
memory location within the segment that we are interested in.
The use of the base or the segment address has changed a bit.
The segment register now contains a selector rather than the
“actual” base address of the segment.
An additional table called a descriptor table uses the selector
as an index to provide additional information known as
descriptor.
The descriptor describes:
• location
•Length
•Access rights
For the segment of interest. 52
There are two descriptor tables. Global Descriptor table and
local descriptor table.
53
80286 Descriptor 80386 through P4 Descriptor
00000000 00000000 Base G D 0 A Limit
Access Rights Base (B23-B16) (B31-B24) V (L19-L16)
Base (B15 – B0) Access Rights Base (B23-B16)
Limit (L15 –L0) Base (B15 – B0)
Limit (L15 –L0)
54
Segment Limit – The last possible offset address of the
segment.
So if the segment begins at memory location F00000H and
ends at location F000FFH. What should be the value of the
limit register ?
57
Example:
Segment start and end if the base address is 10000000H, the
limit is 001FFH and G bit =1.
58
AV – Available
AV =1 – Available
AV =0 – Not Available.
D = Data access
D = 1 – The instructions are in 32bits and the registers are 32
bits.
D = 0 – The instructions are 16 bit instructions compatible with
8086-80286
59
Access Rights byte- Control access to protected mode memory
segment. This byte describes how the segment functions in the
system.
7 6 5 4 3 2 1 0
Selector TI RPL
64
The 64-bit flat mode memory model.
65
Memory Paging
66
It also allows memory to be placed into areas where no
memory exists.
The linear address is broken into three sections that are used
to access the page directory entry, page table entry, and page
offset address
The page directory and each page table are 4K bytes in length
32 bit paging control registers
Memory Addressing Modes(Programming
Model)
PROGRAM MEMORY ADDRESSING
MODES
• Used with the JMP (jump) and CALL instructions.
• Consist of three distinct forms:
– direct, relative, and indirect
70
Direct Program Memory Addressing
71
72
• This JMP instruction loads CS with 1000H and IP
with 0000H to jump to memory location 10000H
for the next instruction.
– an intersegment jump is a jump to any memory
location within the entire memory system
• Often called a far jump because it can jump to
any memory location for the next instruction.
– in real mode, any location within the first 1M
byte
– In protected mode operation, the far jump can
jump to any location in the 4G-byte address range
in the 80386 - Core2 microprocessors
73
• The only other instruction using direct program
addressing is the intersegment or far CALL
instruction.
• Usually, the name of a memory address, called a
label, refers to the location that is called or
jumped to instead of the actual numeric address.
• When using a label with the CALL or JMP
instruction, most assemblers select the best form
of program addressing.
74
Relative Program Memory
Addressing
• Not available in all early microprocessors, but it is
available to this family of microprocessors.
• The term relative means “relative to the
instruction pointer (IP)”.
• The JMP instruction is a 1-byte instruction, with a
1-byte or a 2-byte displacement that adds to the
instruction pointer
75
76
Indirect Program Memory
Addressing
• The microprocessor allows several forms of
program indirect memory addressing for the JMP
and CALL instructions.
• In 80386 and above, an extended register can be
used to hold the address or indirect address of a
relative JMP or CALL.
– for example, the JMP EAX jumps to the location
address by register EAX
77
• If a relative register holds the address, the
jump is considered to be an indirect jump.
• For example, JMP [BX] refers to the memory
location within the data segment at the offset
address contained in BX.
– at this offset address is a 16-bit number used as
the offset address in the intra-segment jump
– this type of jump is sometimes called an indirect-
indirect or double-indirect jump
• Figure shows a jump table that is stored,
beginning at memory location TABLE.
78
79
STACK MEMORY-ADDRESSING
MODES
• The stack plays an important role in all
microprocessors.
– holds data temporarily and stores return
addresses used by procedures
• Stack memory is LIFO (last-in, first-out) memory
– describes the way data are stored and removed
from the stack
80
• Data are placed on the stack with a PUSH
instruction; removed with a POP instruction.
• Stack memory is maintained by two registers:
– the stack pointer (SP or ESP)
– the stack segment register (SS)
• Whenever a word of data is pushed onto the
stack, the high-order 8 bits are placed in the
location addressed by SP – 1.
– low-order 8 bits are placed in the location
addressed by SP – 2
81
• The SP is decremented by 2 so the next word
is stored in the next available stack location.
– the SP/ESP register always points to an area of
memory located within the stack segment.
• In protected mode operation, the SS register
holds a selector that accesses a descriptor for
the base address of the stack segment.
• When data are popped from the stack, the low
order 8 bits are removed from the location
addressed by SP.
– high-order 8 bits are removed; the SP register is
incremented by 2
82
83
• Note that PUSH and POP store or retrieve
words of data—never bytes—in 8086 - 80286.
• 80386 and above allow words or doublewords
to be transferred to and from the stack.
• Data may be pushed onto the stack from any
16-bit register or segment register.
– in 80386 and above, from any 32-bit extended
register
• Data may be popped off the stack into any register
or any segment register except CS.
84
• PUSHA and POPA instructions push or pop all
except segment registers, on the stack.
• Not available on early 8086/8088 processors.
• 80386 and above allow extended registers to be
pushed or popped.
– 64-bit mode for Pentium and Core2 does not
contain a PUSHA or POPA instruction
85
✓Data Movement Instructions