0% found this document useful (0 votes)
0 views5 pages

Chapter Three_ Addressing Modes (4)

Uploaded by

eliasaraya142
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)
0 views5 pages

Chapter Three_ Addressing Modes (4)

Uploaded by

eliasaraya142
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/ 5

MICROPROCESSOR AND ASSEMBLY LANGUAGE

PROGRAMMING
CHAPTER THREE: ADDRESSING MODES

Learning Objectives:
Upon completion of this chapter, students will be able to:
➢ Define the concept of an "addressing mode."
➢ Identify, differentiate, and explain the various data-addressing modes.
➢ Apply the correct addressing mode to access data in registers, memory, and I/O ports.
➢ Understand how the CPU calculates effective addresses (EA) and physical addresses.
➢ Explain the program memory-addressing modes used for jump and call instructions.
➢ Describe the stack memory-addressing mode and its role in program execution.

3.1 Data-Addressing Modes


An addressing mode is a method used to specify the location of data an instruction will operate
on. The correct mode is crucial for efficient and functional assembly programming.

3.1.1 Register Addressing


Description: The operand (data) is located inside a CPU register. This is the fastest addressing
mode because no memory access is required.
Syntax: MOV Destination, Source
Example:
MOV CX, DX ; Copies the contents of register DX into register CX.
ADD AL, BH ; Adds the contents of BH to AL, result stored in AL.
Key Point: Both source and destination operands must be the same size (e.g., both 8-bit, both 16-
bit).
3.1.2 Immediate Addressing
Description: The operand is a constant value (literal) included directly in the instruction itself.
The data is part of the instruction code.
Syntax: MOV Register, ImmediateValue
Example:
MOV BL, 42h ; Loads the hexadecimal value 42 into the BL register.
MOV AX, 1000 ; Loads the decimal value 1000 into the AX register.
Key Point: The source is the immediate value; the destination cannot be an immediate value.
3.1.3 Direct Data Addressing
Description: The instruction directly contains the effective address (offset address) of the
memory location of the operand. The CPU uses the DS segment register by default and
calculates: Physical Address = (DS * 10h) + Offset.
Syntax: MOV AX, [Address]
Example:
MOV AX, [1234h] ; Copies the 16-bit value from memory at DS:1234h into AX.
MOV VALUE, AL ; (Where VALUE is a defined memory variable) Copies AL into the memory
location VALUE.
Key Point: Accesses a fixed, known memory location.

3.1.4 Register Indirect Addressing


Description: The address of the memory operand is held in a base or index register (BP, BX, DI,
or SI). It's like a pointer in high-level languages.
Registers:
BX, DI, SI default to the Data Segment (DS).
BP defaults to the Stack Segment (SS).
Syntax: MOV AX, [Register]
Example:
MOV AX, [BX] ; If BX contains 1000h, this copies the word at DS:1000h into AX.
MOV [DI], CL ; Copies CL into the byte at the memory location pointed to by DI.
Key Point: Efficient for processing arrays and data structures; the register can be incremented or
decremented to traverse memory.

3.1.5 Base-Plus-Index Addressing


Description: The effective address is the sum of a base register (BX or BP) and an index register
(DI or SI).
Base Register: Typically points to the start of an array or structure.
Index Register: Selects an element within the array.
Segment: Determined by the base register (BX=DS, BP=SS).
Syntax: MOV AX, [BaseReg + IndexReg]
Example:
MOV AX, [BX + SI] ; EA = BX + SI. Physical Address = DS * 10h + BX + SI.
Key Point: Ideal for accessing arrays where the base is fixed and the index varies.
3.1.6 Register Relative Addressing
Description: The effective address is the sum of a base/index register (BX, BP, DI, SI) and
a displacement (a fixed offset). The displacement can be a number or the name of a memory
variable.
Syntax: MOV AX, [Register + Displacement] or MOV AX, Displacement[Register]
Example:
MOV AX, [BX + 10h] ; EA = BX + 10h.
MOV AX, ARRAY[SI] ; Accesses an element in ARRAY. If SI=2, it accesses ARRAY[2].
Key Point: Used to access fields within a structure or specific elements in a stack frame.
3.1.7 Base Relative-Plus-Index Addressing
Description: The most complex form. The effective address is the sum of a base register, an index
register, and a displacement.
Syntax: MOV AX, [BaseReg + IndexReg + Displacement]
Example:
MOV AX, [BX + SI + 4] ; EA = BX + SI + 4.
MOV AX, FILE[BX+DI] ; Accesses a two-dimensional array.
Key Point: Used for complex data structures like arrays of records or two-dimensional arrays.

3.1.8 Scaled-Index Addressing (386 and later)


Description: A powerful enhancement where the index register can be multiplied by a scale
factor (1, 2, 4, or 8). This is perfect for indexing arrays of words (scale=2), doublewords (scale=4),
or quadwords (scale=8).
Syntax: MOV EAX, [BaseReg + Scale*IndexReg]
Example:
MOV EAX, [EBX + 4*ECX] ; Accesses a doubleword array. ECX is the index, and each element is
4 bytes, so this gets array[ECX].
Key Point: Eliminates the need to manually multiply the index by the data size, simplifying code.

3.1.9 RIP Relative Addressing (x86-64)


Description: In 64-bit mode, the instruction pointer (RIP) is used to access data relative to the
current location of the code itself. The displacement is a 32-bit signed value added to RIP.
Syntax: MOV RAX, [RIP + Displacement]
Example:
MOV RAX, [RIP + 100h] ; The data is located 100h bytes after the current RIP.
Key Point: Essential for Position-Independent Code (PIC), as it allows code to run correctly
regardless of where it is loaded in memory.

3.1.10 Data Structures


Description: Addressing modes are the building blocks for implementing data structures.
Array: Use Register Indirect or Register Relative with the index register as the array index.
Structure/Record: Use Base-Relative addressing. The base register (e.g., BX) points to the start of
the structure, and the displacement selects the specific field.
Array of Structures: Use Base-Relative-Plus-Index addressing. The base register points to the
array, the index register selects which structure, and the displacement selects the field within that
structure.

3.2 Program Memory-Addressing Modes


These modes are used by JMP and CALL instructions to transfer control to another part of the
program.

3.2.1 Direct Program Memory Addressing


Description: The target address is specified directly as a label or an address.
Example:
JMP PROG_LOOP ; Unconditionally jumps to the instruction at the label PROG_LOOP.
CMP AX, 0
JE LOCATION ; Jumps to LOCATION if AX equals zero.

3.2.2 Relative Program Memory Addressing


Description: The target address is specified relative to the current instruction pointer
(IP/EIP/RIP). The operand is a signed displacement that is added to IP.
Example:
JMP $+5 ; Jumps to the instruction 5 bytes ahead of the current IP. ($ represents the current
location).
Key Point: Used by all conditional jump instructions. Makes code relocatable.

3.2.3 Indirect Program Memory Addressing


Description: The target address is contained in a register or a memory location.
Examples:
Register Indirect: JMP BX ; Jumps to the offset address contained in BX.
Memory Indirect: JMP WORD PTR [BX] ; Jumps to the address stored in the memory word
pointed to by BX. FAR JMP DWORD PTR [SI] ; Jumps to a far address (segment:offset) stored in
memory.

3.3 Stack Memory-Addressing Modes


The stack is a LIFO (Last-In, First-Out) data structure crucial for temporary storage, procedure
calls, and saving return addresses.
Stack Pointer (SP/ESP/RSP): Always points to the top of the stack.
Stack Segment (SS): Defines the base address of the stack in memory.
Key Stack Instructions:
PUSH source

1. Decrement SP by the size of the operand (2 for 16-bit, 4 for 32-bit).


2. Copy the source operand to the memory location pointed to by SS:SP.
POP destination

1. Copy the data from the memory location SS:SP to the destination operand.
2. Increment SP by the size of the operand.
Description: Stack addressing is inherently a form of register indirect addressing using the
SP/ESP/RSP register.
Example:
PUSH AX ; Decrements SP by 2, then copies AX to the stack.
POP DI ; Copies the word from the top of the stack to DI, then increments SP by 2.
PUSH 1234h; (Immediate addressing with PUSH) Pushes an immediate value onto the stack.
PUSH [BX] ; (Direct addressing with PUSH) Pushes the contents of memory at DS:BX onto the
stack.
Summary Table: Common Data-Addressing Modes

Mode Example Operand Location Speed


Register MOV AX, BX Inside CPU Register Fastest
Immediate MOV AL, 5 In the Instruction Fast
Direct MOV AX, [1000h] At Memory Address 1000h Slow
Register Indirect MOV AX, [BX] At Memory, Address in BX Medium
Base-Plus-Index MOV AX, [BX+SI] At Memory, Address in BX+SI Medium
Register Relative MOV AX, [BP+4] At Memory, Address in BP+4 Medium

You might also like