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 11:09] – [Base Indexed addressing with displacement] ktokarz | en:multiasm:papc:chapter_6_5 [2026/02/27 01:43] (current) – [Base addressing] jtokarz | ||
|---|---|---|---|
| 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. | + | 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 16-bit |
| + | In 32-bit mode, the choice of the register for addressing | ||
| + | In each addressing mode, we are using 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. | ||
| Line 50: | Line 55: | ||
| ===== Indirect addressing ===== | ===== Indirect addressing ===== | ||
| - | In the x86 architecture, | + | In the x86 architecture, |
| In 16-bit processors, the base registers can be BX and BP only, while the index registers can be SI or DI. If BP is used, the processor automatically chooses the stack segment by default. For BX used as the base register, or for instructions with an index register only, the processor accesses the data segment by default. | In 16-bit processors, the base registers can be BX and BP only, while the index registers can be SI or DI. If BP is used, the processor automatically chooses the stack segment by default. For BX used as the base register, or for instructions with an index register only, the processor accesses the data segment by default. | ||
| The 32-bit architecture makes the choice of registers much more flexible, and any of the eight registers (including the stack pointer) can be used as the base register. Here, the stack segment is chosen if the base register is EBP or ESP. The index register can be any of the general-purpose registers, excluding the stack pointer. Additionally, | The 32-bit architecture makes the choice of registers much more flexible, and any of the eight registers (including the stack pointer) can be used as the base register. Here, the stack segment is chosen if the base register is EBP or ESP. The index register can be any of the general-purpose registers, excluding the stack pointer. Additionally, | ||
| Line 69: | Line 74: | ||
| The code shows other examples of base addressing. | The code shows other examples of base addressing. | ||
| <code asm> | <code asm> | ||
| - | ; 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] | ||
| - | ; copy two bytes from the data segment in memory at the address from the EBX register to AX | + | ; copy two bytes from the data segment in memory at the address from |
| + | ; the EBX register to AX | ||
| mov ax, [ebx] | mov ax, [ebx] | ||
| Line 174: | Line 181: | ||
| <code asm> | <code asm> | ||
| ; copy one byte from the data segment in the memory at the address calculated | ; 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 | + | ; 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, [bx + si] + table | ||
| Line 183: | Line 191: | ||
| </ | </ | ||
| - | Similarly, 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. The displacement can be placed at any position in the address argument expression. Some examples are shown below. | + | 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> | <code asm> | ||
| ; 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 | ||
| Line 226: | Line 234: | ||
| </ | </ | ||
| - | The scaled register is assumed as the index, the other one is the base (even if it is used first in the instruction). While segmentation is enabled, the use of EBP or ESP as a base register determines the segment register choice. | + | 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> | <code asm> | ||
| ; 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 | ||
| Line 248: | Line 256: | ||
| </ | </ | ||
| + | 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 | ||
| + | </ | ||
| Line 260: | Line 276: | ||
| </ | </ | ||
| - | The 32-bit architecture makes the choice of registers much more flexible, and any of the eight registers (including the stack pointer) can be used as the base register. Here, the stack segment is chosen if the base register is EBP or ESP. Index register can be any of the general-purpose registers except | + | The 32-bit architecture makes the choice of registers much more flexible, and any of the eight registers (including the stack pointer) can be used as the base register. Here, the stack segment is chosen if the base register is EBP or ESP. The index register can be any of the general-purpose registers except |
| In the 64-bit architecture, | In the 64-bit architecture, | ||
| Line 267: | Line 283: | ||
| <figure effectiveIA32> | <figure effectiveIA32> | ||
| - | {{ : | + | {{ : |
| < | < | ||
| + | </ | ||
| + | |||
| + | In x64, new R8 - R16 registers can also be used for address calculation, | ||
| + | |||
| + | <figure effectivex64> | ||
| + | {{ : | ||
| + | < | ||
| </ | </ | ||