You are on page 1of 50

COMSATS Institute of
Information Technology
CCS221– COMPUTER ORGANIZATION AND ASSEMBLY
LANGUAGE
Lecture # 6

Quote of the day

The weakest link in
the chain is also
the strongest. It
can break the
4/17/16

CCS221– COMPUTER ORGANIZATION AND
ASSEMBLY LANGUAGE

3

Quick recap

4/17/16

CCS221– COMPUTER ORGANIZATION AND
ASSEMBLY LANGUAGE

4

ADDRESSING MODES
• The address field in a typical instruction format are relatively small
• We would like to be able to reference a large range of locations in main memory
• To achieve this objective, a variety of addressing techniques has been employed
• They all involve some trade-off between
• Address range
• Number of memory references in the instruction
• Complexity of address calculation

• In this section, we examine the most common addressing techniques:
1.
2.
3.
4.
5.
6.
7.

Immediate
Direct
Indirect
Register
Register indirect
Displacement
Stack

4/17/16

CCS221– COMPUTER ORGANIZATION AND
ASSEMBLY LANGUAGE

5

the effective address will be either a main memory address or a register • In a virtual memory system. In a system without virtual memory. the effective address is a virtual address or a register. The actual mapping to a physical address is a function of the memory management unit (MMU) 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 6 .ADDRESSING MODES • All computer architectures provide more than one of these addressing modes • Question : how the processor can determine which address mode is being used • Different opcodes will use different addressing modes • One or more bits in the instruction format used as a mode field • The second concern the interpretation of the effective address (EA).

Immediate Addressing • The simplest form of addressing is immediate addressing. in which the operand value is present in the instruction Operand = A • This mode can be used to define and use constants or set initial values of variables • Advantage : no memory reference other than the instruction fetch is required to obtain the operand. thus saving one memory or cache cycle in the instruction cycle • Disadvantage : the size of the number is restricted to the size of the address field 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 7 .

move the constant 20 into register AL ADD AX. add constant 5 to register EAX 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 8 . 20 .Immediate Addressing Examples : MOV AL. 5 .

move the value stored in Reg_No variable to register AL 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 9 . Reg_No .Direct Addressing • The address field contains the effective address of the operand: EA = A • It requires only one memory reference and no special calculation • The obvious limitation is that it provides only a limited address space Examples : MOV AL.

Indirect Addressing • The address field refer to the address of a word in memory. an address space of 2N is now available • Disadvantage : Instruction execution requires two memory references to fetch the operand 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 10 . which in turn contains a full-length address of the operand EA = (A) the parentheses are to be interpreted as meaning contents of. • Advantage : For a word length of N.

[var_1] CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 11 .Indirect Addressing • Example : 4/17/16 Mov AL.

an address field that references registers will have from 3 to 5 bits. then register R5 is the intended address. so that a total of from 8 to 32 general-purpose registers can be referenced 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 12 .Register Addressing • Register addressing is similar to direct addressing • The only difference is that the address field refers to a register rather than a main memory address: EA = R • If the contents of a register address field in an instruction is 5. and the operand value is contained in R5 • Typically.

this implies that the processor registers will be heavily used. their use in this fashion makes sense only if they are employed efficiently 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 13 . Because of the severely limited number of registers.Register Addressing • Advantages : • Only a small address field is needed in the instruction • No time-consuming memory references are required • Disadvantage : • The address space is very limited • If register addressing is heavily used in an instruction set.

Register Addressing • Example : Mov AX.BX 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 14 .

EA = (R) • The advantages and limitations of register indirect addressing are basically the same as for indirect addressing • In both cases.Register Indirect Addressing Register indirect address. the address space limitation of the address field is overcome by having that field refer to a word length location containing an address 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 15 .

[BX] 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 16 .Register Indirect Addressing • Example : Mov AX.

at least one of which is explicit. The value contained in one address field (value = A) is used directly • The other address field refers to a register whose contents are added to A to produce the effective address 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 17 .Displacement Addressing • A mode of addressing combines the capabilities of direct addressing and register indirect addressing EA = A + (R) • Displacement addressing requires that the instruction have two address fields.

ds:[1234] 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 18 .Displacement Addressing • Example : mov al.

references to stack locations in memory are in fact register indirect addresses • The machine instructions need not include a memory reference but implicitly operate on the top of the stack 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 19 .Stack Addressing • The stack is a reserved block of locations • Associated with the stack is a pointer whose value is the address of the top of the stack • The stack pointer is maintained in a register • Thus.

Stack Addressing 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 20 .

Basic Addressing Modes 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 21 .

x86 Addressing Modes • The x86 address translation mechanism produces an address. Index register 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 22 . which holds the starting address of the corresponding segments • Associated with each user-visible segment register is a segment descriptor register. which records the access rights for the segment as well as the starting address and limit of the segment • Two registers used in constructing an address: Base register. that is an offset into a segment • The sum of the starting address of the segment and the effective address produces a linear address • The x86 is equipped with a variety of addressing modes intended to allow the efficient execution of high-level languages • The segment register determines the segment that is the subject of the reference • There are six segment registers. called a virtual or effective address. the one being used for a particular reference depends on the context of execution and the instruction • Each segment register holds an index into the segment descriptor table.

4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 23 .

x86 Addressing Modes 9 addressing modes available • • • • • • • • • 4/17/16 Immediate Register operand Displacement Base Base with displacement Scaled index with displacement Base with index and displacement Base scaled index with displacement Relative CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 24 .

g. the operand is included in the instruction Operand = address field e. 50 is operand • No memory reference to fetch data • Fast • Limited range 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 25 . PUSH 50 . Immediate Addressing • For the immediate mode.1.

the operand is located in a register • For general instructions. and logical instructions. the operand can be one of the general registers • Limited number of registers • Very small address field needed • Shorter instructions • Faster operand fetch • No memory access • Very fast execution • Very limited address space 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 26 . arithmetic.2. such as data transfer. Register operand • For register operand mode.

3. 16-. Displacement • In the displacement mode. or 32bit displacement EA = A + (R) • Address field hold two values • A = base value • R = register that holds displacement 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 27 . the operand’s offset is contained as part of the instruction as an 8-.

or 32-bit registers contains the effective address LA = (SR) + (B) LA = Linear address SR = Segment register B = Base register 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 28 .4. in the sense that the address portion of the instruction tells the processor where to look to find the address • Base mode specifies that one of the 8-. 16-. Base • The remaining addressing modes are indirect.

which contains the local variables for the corresponding procedure 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 29 .5. Base with displacement • The instruction includes a displacement to be added to a base register. which may be any of the general-purpose registers LA = (SR) + (B) + A Examples: • The base register could point to the beginning of a stack frame.

4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 30 .5. Base with displacement • Sums the contents of • The base register • A displacement • To form the effective address.

Base with displacement • The base register can be any general-purpose register 31 .5.

Segment Register B = ESI= 367 SS Index Register GS FS Memory ES CS Scale DS = 540 + A = 520 Descriptor Register Access Right Limit Base Address 4/17/16 367+520 = 887 + 540+ 887 = 1427 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 32 .

6. and then added to a displacement LA = (SR) + (I) * S + A I = index register S = scaling factor 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 33 . Scaled index with displacement • In the scaled index with displacement mode. which is generally used for stack processing • In calculating the effective address. 2. or 8. the instruction includes a displacement to be added to an index register • The index register may be any of the general-purpose registers except the one called ESP. 4. the contents of the index register are multiplied by a scaling factor of 1.

6. Scaled index with displacement • Sums the contents of • The index register multiplied by a scaling factor • A displacement • To form the effective address 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 34 .

6. Scaled index with displacement • The index register can be any general-purpose register except ESP 35 .

Segment Register Base Register SS GS I = ESI= 700 FS ES Memory 4*700= 2800 CS Scale = 4 DS = 350 + A = 256 Descriptor Register Access Right Limit Base Address 4/17/16 2800+256 = 3156 + 350+ 3156 = 3406 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 36 .

in this case. and each register handles one dimension of the array 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 37 .7. the displacement points to the beginning of the array. This mode used to support a two-dimensional array. and a displacement to form the effective address LA = (SR) + (B) + (I) + A I = index register B = base register A = contents of an address field SR = segment register • Example. Base with index and displacement • The base with index and displacement mode sums the contents of the base register. the index register.

Base with index and displacement • Sums the contents of • The base register • The index register • A displacement 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 38 .7.

7. Base with index and displacement • The base register can be any general-purpose register • The index register can be any general-purpose register except ESP 39 .

Segment Register B = ESI= 367 SS I = EDI= 345 GS FS Memory ES CS Scale DS = 540 + A = 520 Descriptor Register Access Right Limit Base Address 4/17/16 367+345+520 = 1232 + 540+ 1232 = 1772 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 40 .

or 8 bytes each in length • This mode also provides efficient indexing of a two-dimensional array when the array elements are 2. Based scaled index with displacement • The based scaled index with displacement mode sums the contents of the index register multiplied by a scaling factor. or 8 bytes in length 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 41 . the array elements would be 2. and the displacement LA = (SR) + (I) * S + (B) + A I = index register B = base register A = contents of an address field SR = segment register S = Scaling factor Examples : • This is useful if an array is stored in a stack frame. 4. 4.8. in this case. the contents of the base register.

Based scaled index with displacement • The base register can be any general-purpose register • The index register can be any general-purpose register except ESP 42 .8.

Based scaled index with displacement • Sums the contents of • The index register multiplied by a scaling factor • The contents of the base register • A displacement • To form the effective address 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 43 .8.

Segment Register B = ESI= 40 CS = 120 SS = 1300 I = EDI= 20 DS = 2600 ES = 3900 Memory 4*20= 80 FS = 5200 Scale = 4 GS = 6500 + A = 100 Descriptor Register Access Right Limit Base Address 4/17/16 40 + 80 + 100 = 220 + 120 + 220 = 340 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 44 .

word.9. the displacement is treated as a signed byte. Relative addressing • Relative addressing can be used in transfer-of-control instructions • A displacement is added to the value of the program counter. and that value either increases or decreases the address in the program counter 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 45 . which points to the next instruction LA = (PC) + A • In this case. or double word value.

X86 Addressing Modes 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 46 .

Summary 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 47 .

NINTH EDITION By William Stallings 4/17/16 CCS221– COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE 48 .Reading Material • Chapter # 13 COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE.