This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| en:multiasm:papc:chapter_6_5 [2026/02/27 01:43] – [Base addressing] jtokarz | en:multiasm:papc:chapter_6_5 [2026/06/22 12:14] (current) – pczekalski | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Addressing Modes in Instructions ====== | ====== Addressing Modes in Instructions ====== | ||
| - | Addressing mode specifies how the processor | + | Addressing mode specifies how the processor |
| In 16-bit mode, only four registers can be used for indirect addressing: BX, BP, SI and DI. | In 16-bit mode, only four registers can be used for indirect addressing: BX, BP, SI and DI. | ||
| - | In 32-bit mode, the choice of the register for addressing is much more flexible and addressing is enhanced 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. | + | In 32-bit mode, the choice of the register for addressing is much more flexible and addressing is enhanced with the possibility of scaling: multiplying one register by a small constant. In 64-bit mode, addressing relative to the instruction pointer was added to facilitate the 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. |
| 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, | 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> | ||
| Line 8: | Line 8: | ||
| </ | </ | ||
| < | < | ||
| - | Calculating the addresses for control transfer instructions, | + | The calculation of addresses for control-transfer instructions, |
| </ | </ | ||
| ===== Immediate addressing ===== | ===== Immediate addressing ===== | ||
| - | The immediate argument is a constant encoded | + | The immediate argument is a constant encoded |
| - | 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 | + | 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 address |
| The examples of instructions using immediate addressing | The examples of instructions using immediate addressing | ||
| <code asm> | <code asm> | ||
| Line 20: | Line 20: | ||
| </ | </ | ||
| ===== 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 |
| <code asm> | <code asm> | ||
| ; 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 | ||
| Line 38: | Line 38: | ||
| ===== x64 RIP-relative direct addressing ===== | ===== x64 RIP-relative direct addressing ===== | ||
| - | 64-bit processors have some specific addressing modes. The default | + | 64-bit processors have some specific addressing modes. The default direct addressing mode in the x64 architecture is relative to the RIP register. If there is no base or index register in the instruction, |
| In figure {{ref> | In figure {{ref> | ||
| Line 47: | Line 47: | ||
| ===== x64 32-bit direct addressing mode ===== | ===== x64 32-bit direct addressing mode ===== | ||
| - | In this mode, the instruction holds the 32-bit signed value, which is sign-extended to 64 bits by the processor. This limits the addressing space to two areas. The first region starts | + | In this mode, the instruction holds the 32-bit signed value, which is sign-extended to 64 bits by the processor. This limits the addressing space to two areas. The first region starts |
| ===== x64 64-bit direct addressing ===== | ===== x64 64-bit direct addressing ===== | ||
| - | In this mode, the address is a 64-bit unsigned value. As in general, | + | In this mode, the address is a 64-bit unsigned value. As the arguments of the instruction are generally |
| MASM assembler does not use this mode. | MASM assembler does not use this mode. | ||
| ===== Indirect addressing ===== | ===== Indirect addressing ===== | ||
| - | In the x86 architecture, | + | In the x86 architecture, |
| - | In 16-bit processors, the base registers | + | In 16-bit processors, the base registers |
| 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, | ||
| - | In the 64-bit architecture, | + | In the 64-bit architecture, |
| Line 87: | Line 87: | ||
| ===== Base addressing with displacement ===== | ===== Base addressing with displacement ===== | ||
| - | Base addressing mode with displacement uses the base register | + | Base addressing mode with displacement uses the base register |
| In figure {{ref> | In figure {{ref> | ||
| Line 107: | Line 107: | ||
| </ | </ | ||
| ===== Index addressing with displacement ===== | ===== Index addressing with displacement ===== | ||
| - | Index addressing mode with displacement uses the index register with an additional constant added. So the final effective address is a sum of the content | + | Index addressing mode with displacement uses the index register with an additional constant added. So the final effective address is the sum of the index register's contents |
| In figure {{ref> | In figure {{ref> | ||
| Line 135: | Line 135: | ||
| </ | </ | ||
| - | MASM assembler accepts different notations | + | The MASM assembler accepts different notations |
| <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 | ||
| Line 156: | Line 156: | ||
| 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 | + | While segmentation is enabled, the use of EBP or ESP as the base register determines the choice of segment register. Notice that it is possible to use the same register as base and index in one instruction. |
| <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 170: | Line 170: | ||
| </ | </ | ||
| ===== Base Indexed addressing with displacement ===== | ===== Base Indexed addressing with displacement ===== | ||
| - | In this addressing mode, the combination of two registers with an additional constant is used. In a 16-bit processor, the base register BX or BP, and the index register SI or DI. The constant can be encoded as an 8- or 16-bit value. In such a processor, this is the most complex mode available. In a 32- or 64-bit processor, any register can be the base, and all except the stack pointer can be the index. The constant is up to a 32-bit signed value. The final effective address is the sum of the contents of two registers and the displacement. | + | In this addressing mode, the combination of two registers with an additional constant is used. In a 16-bit processor, the base register |
| - | In figure {{ref> | + | In figure {{ref> |
| <figure baseindexdispx86> | <figure baseindexdispx86> | ||
| Line 178: | Line 178: | ||
| </ | </ | ||
| - | MASM assembler accepts different notations of the base + index + displacement, | + | MASM assembler accepts different notations of the base + index + displacement, |
| <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 | ||
| Line 191: | Line 191: | ||
| </ | </ | ||
| - | 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. |
| <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 205: | Line 205: | ||
| ===== 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 | + | 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 |
| In figure {{ref> | In figure {{ref> | ||
| Line 213: | Line 213: | ||
| </ | </ | ||
| - | Because | + | Because these instructions |
| <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 226: | ||
| </ | </ | ||
| ===== Base Indexed addressing with scaling ===== | ===== Base Indexed addressing with scaling ===== | ||
| - | Base indexed addressing mode with scaling uses the sum of the base register | + | Base-indexed addressing mode with scaling uses the sum of the base register |
| - | The figure {{ref> | + | The figure {{ref> |
| <figure baseindexscaleIA32> | <figure baseindexscaleIA32> | ||
| Line 234: | Line 234: | ||
| </ | </ | ||
| - | The scaled register is assumed | + | The scaled register is assumed |
| <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 248: | ||
| </ | </ | ||
| ===== 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 | + | 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 the base and almost any general-purpose register as the index, except |
| - | The figure {{ref> | + | The figure {{ref> |
| <figure baseindexdispscaleIA32> | <figure baseindexdispscaleIA32> | ||
| Line 256: | Line 256: | ||
| </ | </ | ||
| - | As in the base indexed mode with scaling without displacement, | + | As in the base indexed mode with scaling without displacement, |
| <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 269: | Line 269: | ||
| ===== Summary for indirect addressing ===== | ===== Summary for indirect addressing ===== | ||
| - | In 16-bit processors, the base registers can be BX and BP only. The first one is used to access the data segment, the second one automatically chooses the stack segment by default. The additional offset can be unused or can be encoded as an 8 or 16-bit signed value. The schematic of the x86 effective address calculation for indirect address generation is shown in figure {{ref> | + | In 16-bit processors, the base registers can be BX and BP only. The first one is used to access the data segment, |
| <figure effectivex86> | <figure effectivex86> | ||
| - | {{ : | + | {{ : |
| < | < | ||
| </ | </ | ||
| - | 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 the stack pointer. The index register can be scaled by a factor of 1, 2, 4 or 8. Additional displacement can be unused or can be encoded as an 8, 16 or 32-bit signed value. | + | 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 the stack pointer. The index register can be scaled by a factor of 1, 2, 4 or 8. Additional displacement can be unused or encoded as an 8-, 16-, or 32-bit signed value. |
| - | In the 64-bit architecture, | + | In the 64-bit architecture, |
| In figure {{ref> | In figure {{ref> | ||