This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:multiasm:papc:chapter_6_5 [2025/05/29 07:14] – [Addressing Modes in Instructions] ktokarz | en:multiasm:papc:chapter_6_5 [2025/08/20 09:55] (current) – [Addressing Modes in Instructions] ktokarz | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Addressing Modes in Instructions ====== | ====== Addressing Modes in Instructions ====== | ||
| - | Addressing mode specifies how the processor reaches the data in the memory. x86 architecture implements immediate, direct and indirect memory addressing. Indirect addressing can use a single or two registers and a constant to calculate the final address. The addressing mode takes the name of the registers used. The 32-bit mode makes the choice of the register for addressing more flexible and enhances addressing with the possibility of scaling: multiplying one register by a small constant. In 64-bit mode, addressing relative to the instruction pointer was added for easy relocation of programs in memory. In this chapter, we will focus on the details of all addressing modes in 16, 32 and 64-bit processors. | + | Addressing mode specifies how the processor reaches the data in the memory. |
| In each addressing mode, we are using the simple examples with the mov instruction. The move instruction copies data from the source operand to the destination operand. The order of the operands in instructions is similar to that of high-level languages. The left operand is the destination, | In each addressing mode, we are using the simple examples with the mov instruction. The move instruction copies data from the source operand to the destination operand. The order of the operands in instructions is similar to that of high-level languages. The left operand is the destination, | ||
| <code asm> | <code asm> | ||
| mov destination, | mov destination, | ||
| </ | </ | ||
| + | < | ||
| + | Calculating the addresses for control transfer instructions, | ||
| + | </ | ||
| ===== Immediate addressing ===== | ===== Immediate addressing ===== | ||
| The immediate argument is a constant encoded as part of the instruction. This means that this value is encoded in a code section of the program and can't be modified during program execution. | The immediate argument is a constant encoded as part of the instruction. This means that this value is encoded in a code section of the program and can't be modified during program execution. | ||
| In 16-bit mode, the size of the constant can be 8 or 16 bits, and in 32- and 64-bit mode, it can be up to 32 bits. The use of the immediate operand depends on the instruction. It can be, for example, a numerical constant, an offset of the address or additional displacement used for efficient address calculation, | In 16-bit mode, the size of the constant can be 8 or 16 bits, and in 32- and 64-bit mode, it can be up to 32 bits. The use of the immediate operand depends on the instruction. It can be, for example, a numerical constant, an offset of the address or additional displacement used for efficient address calculation, | ||
| The examples of instructions using immediate addressing | The examples of instructions using immediate addressing | ||
| - | < | + | < |
| mov ax, 10 ; move the constant value of 10 to the accumulator AX | mov ax, 10 ; move the constant value of 10 to the accumulator AX | ||
| mov ebx, 35 ; move the constant 35 to the EBX register | mov ebx, 35 ; move the constant 35 to the EBX register | ||
| Line 16: | Line 19: | ||
| ===== Direct addressing ===== | ===== Direct addressing ===== | ||
| In direct addressing mode, the data is reached by specifying the target offset (displacement) as a constant. The processor uses this offset together with the appropriate segment register to access the byte in memory. The displacement can be specified in the instruction as a number, a previously defined constant or a constant expression. With segmentation enabled, it is possible to use the segment prefix to select the segment register we want to use. The example instructions which use numbers or a variable name as the displacement are shown in the following code and presented in figure {{ref> | In direct addressing mode, the data is reached by specifying the target offset (displacement) as a constant. The processor uses this offset together with the appropriate segment register to access the byte in memory. The displacement can be specified in the instruction as a number, a previously defined constant or a constant expression. With segmentation enabled, it is possible to use the segment prefix to select the segment register we want to use. The example instructions which use numbers or a variable name as the displacement are shown in the following code and presented in figure {{ref> | ||
| - | < | + | < |
| ; copy one byte from the BL register to memory address 0800h in the data segment | ; copy one byte from the BL register to memory address 0800h in the data segment | ||
| mov ds:[0800h], bl | mov ds:[0800h], bl | ||
| Line 68: | Line 71: | ||
| The code shows other examples of base addressing. | The code shows other examples of base addressing. | ||
| - | < | + | < |
| ; copy one byte from the data segment in memory at the address from the BX register to AL | ; copy one byte from the data segment in memory at the address from the BX register to AL | ||
| mov al, [bx] | mov al, [bx] | ||
| Line 89: | Line 92: | ||
| Some code examples are shown below. The number represents the previously defined constant. MASM accepts various syntaxes of base plus displacement address notation. | Some code examples are shown below. The number represents the previously defined constant. MASM accepts various syntaxes of base plus displacement address notation. | ||
| - | < | + | < |
| ; copy one byte from the data segment in memory at the address from the BX register | ; copy one byte from the data segment in memory at the address from the BX register | ||
| ; increased by " | ; increased by " | ||
| Line 109: | Line 112: | ||
| Some code examples are shown below. The table represents the previously defined table in memory. MASM accepts various syntaxes of index plus displacement address notation. | Some code examples are shown below. The table represents the previously defined table in memory. MASM accepts various syntaxes of index plus displacement address notation. | ||
| - | < | + | < |
| ; copy one byte from the data segment in memory at the address calculated | ; copy one byte from the data segment in memory at the address calculated | ||
| ; as the sum of the table increased by index from DI to AL | ; as the sum of the table increased by index from DI to AL | ||
| Line 139: | Line 142: | ||
| In x86, there are only four possible combinations of base and index registers. They are depicted in the code. | In x86, there are only four possible combinations of base and index registers. They are depicted in the code. | ||
| - | < | + | < |
| ; copy one byte from the data or stack segment in memory at the address calculated | ; copy one byte from the data or stack segment in memory at the address calculated | ||
| ; as the sum of the base register and index register to AL | ; as the sum of the base register and index register to AL | ||
| Line 150: | Line 153: | ||
| In 32- or 64-bit processors, the first register used in the instruction is the base register, and the second is the index register. | In 32- or 64-bit processors, the first register used in the instruction is the base register, and the second is the index register. | ||
| While segmentation is enabled use of EBP or ESP as a base register determines the segment register choice. Notice that it is possible to use the same register as base and index in one instruction. | While segmentation is enabled use of EBP or ESP as a base register determines the segment register choice. Notice that it is possible to use the same register as base and index in one instruction. | ||
| - | < | + | < |
| ; copy one byte from the data or stack segment in memory at the address calculated | ; copy one byte from the data or stack segment in memory at the address calculated | ||
| ; as the sum of the base register and index register to AL | ; as the sum of the base register and index register to AL | ||
| Line 170: | Line 173: | ||
| < | < | ||
| </ | </ | ||
| + | |||
| + | MASM assembler accepts different notations of the base + index + displacement, | ||
| + | <code asm> | ||
| + | ; copy one byte from the data segment in the memory at the address calculated | ||
| + | ; as the sum of the base (BX) register, the index (SI) register and a displacement to AL | ||
| + | mov al, [bx] + [si] + table | ||
| + | mov al, [bx + si] + table | ||
| + | mov al, [bx] [si] + table | ||
| + | mov al, table [si] [bx] | ||
| + | mov al, table [si] + [bx] | ||
| + | mov al, table [si + bx] | ||
| + | </ | ||
| + | |||
| + | In 32- or 64-bit processors, the first register used in the instruction is the base register, and the second is the index register. While segmentation is enabled, the use of EBP or ESP as a base register determines the segment register choice. The displacement can be placed at any position in the address argument expression. Some examples are shown below. | ||
| + | <code asm> | ||
| + | ; copy one byte from the data or stack segment in memory at the address calculated | ||
| + | ; as the sum of the base, index and displacement (table) to AL | ||
| + | mov al, [eax] + [esi] + table ; data segment | ||
| + | mov al, table + [ebx] + [edi] ; data segment | ||
| + | mov al, table [ecx] [esi] ; data segment | ||
| + | mov al, [edx] [edi] + table ; data segment | ||
| + | mov al, table + [ebp] + [esi] ; stack segment | ||
| + | mov al, [esp] + [edi] + table ; stack segment | ||
| + | </ | ||
| + | |||
| + | |||
| ===== Index addressing with scaling ===== | ===== Index addressing with scaling ===== | ||
| Index addressing mode with scaling uses the index register multiplied by a simple constant of 1, 2, 4 or 8. This addressing mode is available for 32- or 64-bit processors and can use any general-purpose register except of stack pointer. | Index addressing mode with scaling uses the index register multiplied by a simple constant of 1, 2, 4 or 8. This addressing mode is available for 32- or 64-bit processors and can use any general-purpose register except of stack pointer. | ||
| Line 178: | Line 207: | ||
| < | < | ||
| </ | </ | ||
| + | |||
| + | Because in these instructions, | ||
| + | <code asm> | ||
| + | ; copy one byte from the data or stack segment in memory at the address calculated | ||
| + | ; as the multiplication of the index register by a constant to AL | ||
| + | mov al, [eax * 2] ; data segment | ||
| + | mov al, [ebx * 4] ; data segment | ||
| + | mov al, [ecx * 8] ; data segment | ||
| + | mov al, [edx * 2] ; data segment | ||
| + | mov al, [esi * 4] ; data segment | ||
| + | mov al, [edi * 8] ; data segment | ||
| + | mov al, [ebp * 2] ; data segment | ||
| + | </ | ||
| ===== Base Indexed addressing with scaling ===== | ===== Base Indexed addressing with scaling ===== | ||
| Base indexed addressing mode with scaling uses the sum of the base register with the content of the index register multiplied by a simple constant of 1, 2, 4 or 8. This addressing mode is available for 32- or 64-bit processors and can use any general-purpose register as base and almost any general-purpose register as index, except of stack pointer. | Base indexed addressing mode with scaling uses the sum of the base register with the content of the index register multiplied by a simple constant of 1, 2, 4 or 8. This addressing mode is available for 32- or 64-bit processors and can use any general-purpose register as base and almost any general-purpose register as index, except of stack pointer. | ||
| Line 187: | Line 229: | ||
| </ | </ | ||
| + | The scaled register is assumed as the index, the other one is the base (even if it is not used first in the instruction). While segmentation is enabled, the use of EBP or ESP as a base register determines the segment register choice. | ||
| + | <code asm> | ||
| + | ; copy one byte from the data or stack segment in memory at the address calculated | ||
| + | ; as the sum of the base register and index register to AL | ||
| + | mov al, [eax] + [esi * 2] ; data segment | ||
| + | mov al, [ebx] + [edi * 4] ; data segment | ||
| + | mov al, [ecx] + [eax * 8] ; data segment | ||
| + | mov al, [edx] + [ecx * 2] ; data segment | ||
| + | mov al, [esi] + [edx * 4] ; data segment | ||
| + | mov al, [edi] + [edi * 8] ; data segment | ||
| + | mov al, [ebp] + [esi * 2] ; stack segment | ||
| + | mov al, [esp] + [edi * 4] ; stack segment | ||
| + | </ | ||
| ===== Base Indexed addressing with displacement and scaling ===== | ===== Base Indexed addressing with displacement and scaling ===== | ||
| Base indexed addressing mode with displacement and scaling uses the sum of the base register, the content of the index register multiplied by a simple constant of 1, 2, 4 or 8, and an additional constant. This addressing mode is available for 32- or 64-bit processors and can use any general-purpose register as base and almost any general-purpose register as index, except of stack pointer. The displacement can be up to a 32-bit signed value, even in a 64-bit processor. | Base indexed addressing mode with displacement and scaling uses the sum of the base register, the content of the index register multiplied by a simple constant of 1, 2, 4 or 8, and an additional constant. This addressing mode is available for 32- or 64-bit processors and can use any general-purpose register as base and almost any general-purpose register as index, except of stack pointer. The displacement can be up to a 32-bit signed value, even in a 64-bit processor. | ||
| Line 196: | Line 251: | ||
| </ | </ | ||
| + | As in the base indexed mode with scaling without displacement, | ||
| + | <code asm> | ||
| + | ; copy one byte from the data or stack segment in memory at the address calculated | ||
| + | ; as the sum of the base, scaled index and displacement (table) to AL | ||
| + | mov al, [eax] + [esi * 2] + table ; data segment | ||
| + | mov al, table + [ebx] + [edi * 4] ; data segment | ||
| + | mov al, table + [ebp] + [esi * 2] ; stack segment | ||
| + | mov al, [esp] + [edi * 4] + table ; stack segment | ||
| + | </ | ||