This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:multiasm:papc:chapter_6_6 [2025/07/31 12:18] – [Instruction prefixes] ktokarz | en:multiasm:papc:chapter_6_6 [2025/08/01 06:59] (current) – [Scale Index Base byte] ktokarz | ||
|---|---|---|---|
| Line 199: | Line 199: | ||
| Let's look at some examples of instruction encoding. First, look at the data transfer between two registers. | Let's look at some examples of instruction encoding. First, look at the data transfer between two registers. | ||
| - | < | + | < |
| ; MOD REG R/M | ; MOD REG R/M | ||
| mov al, dl ;encoded as 0x88, 0xD0 11 010 000 | mov al, dl ;encoded as 0x88, 0xD0 11 010 000 | ||
| Line 209: | Line 209: | ||
| Now, a few examples of indirect addressing without displacement. | Now, a few examples of indirect addressing without displacement. | ||
| - | < | + | < |
| ; MOD REG R/M | ; MOD REG R/M | ||
| mov dx, | mov dx, | ||
| Line 218: | Line 218: | ||
| Now, a few examples of indirect addressing with displacement. | Now, a few examples of indirect addressing with displacement. | ||
| - | < | + | < |
| ; MOD REG R/M | ; MOD REG R/M | ||
| mov dx, | mov dx, | ||
| Line 308: | Line 308: | ||
| In the tables {{ref> | In the tables {{ref> | ||
| - | Let's look at some code examples, considering the 32-bit version first. | + | Let's look at some code examples, considering the 32-bit version first. In all instructions, |
| <code asm> | <code asm> | ||
| - | mov eax, [ebx+ecx] | + | ;MOD R/M (second byte) is 0x04 for all instructions: |
| - | mov eax, [ebx+ecx*2] | + | ; |
| - | mov eax, [ebx+ecx*4] | + | ; |
| - | mov eax, [ebx+ecx*8] | + | |
| + | ;SIB (third byte) is 0x0B, 0x4B, 0x8B or 0xCB: | ||
| + | ; | ||
| + | mov eax, [ebx+ecx] | ||
| + | mov eax, [ebx+ecx*2] | ||
| + | mov eax, [ebx+ecx*4] | ||
| + | mov eax, [ebx+ecx*8] | ||
| </ | </ | ||
| + | |||
| + | And other examples for x64 processors. The SIB byte is extended with bits from the REX prefix. We'll start with the similar examples as shown for 32-bit machines. | ||
| + | |||
| + | <code asm> | ||
| + | ;REX prefix (first byte) is 0x48 for all instructions: | ||
| + | ; | ||
| + | ; | ||
| + | ; | ||
| + | ; | ||
| + | ; | ||
| + | |||
| + | ;MOD R/M (second byte) is 0x04 for all instructions: | ||
| + | ; | ||
| + | ; | ||
| + | |||
| + | ; | ||
| + | mov rax, [rbx+rcx] | ||
| + | mov rax, [rbx+rcx*2] | ||
| + | mov rax, [rbx+rcx*4] | ||
| + | mov rax, [rbx+rcx*8] | ||
| + | </ | ||
| + | |||
| + | If any of the new registers (R8-R15) is used in the instruction, | ||
| + | |||
| + | <code asm> | ||
| + | ; | ||
| + | mov rax, [r10+rcx] | ||
| + | mov rax, [rbx+r11] | ||
| + | mov r12, [rbx+rcx] | ||
| + | |||
| + | ;Last instruction has the MOD R/M REG field extended | ||
| + | ;by the R bit from the REX prefix. | ||
| + | ; | ||
| + | ; | ||
| + | </ | ||
| + | |||
| + | Certainly, the presented examples do not exhaust all possible situations. For a more detailed explanation, | ||
| =====Displacement===== | =====Displacement===== | ||
| Displacement gives the offset for memory operands. Depending on the addressing mode, it can be the direct memory address or an additional offset added to the contents of the base, index register or both. Displacement can be 1, 2, or 4 bytes long. Some instructions allow using an 8-byte displacement. In these instructions, | Displacement gives the offset for memory operands. Depending on the addressing mode, it can be the direct memory address or an additional offset added to the contents of the base, index register or both. Displacement can be 1, 2, or 4 bytes long. Some instructions allow using an 8-byte displacement. In these instructions, | ||