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/08/01 09:42] – [Scale Index Base byte] ktokarz | en:multiasm:papc:chapter_6_6 [2026/02/19 20:48] (current) – [Scale Index Base byte] ktokarz | ||
|---|---|---|---|
| Line 78: | Line 78: | ||
| The **lock prefix** is valid for instructions that work in a read-modify-write manner. An example of such an instruction can be adding a constant or register content to the variable in the memory. | The **lock prefix** is valid for instructions that work in a read-modify-write manner. An example of such an instruction can be adding a constant or register content to the variable in the memory. | ||
| - | < | + | < |
| The lock prefix appears as a single byte with the value 0x0F before the opcode. It disables DMA requests (or any other requests that gain control of the buses) during the execution of the instruction to prevent accidental modification of the memory contents at the same address by both the processor and DMA controller. | The lock prefix appears as a single byte with the value 0x0F before the opcode. It disables DMA requests (or any other requests that gain control of the buses) during the execution of the instruction to prevent accidental modification of the memory contents at the same address by both the processor and DMA controller. | ||
| Line 86: | Line 86: | ||
| <code asm> | <code asm> | ||
| mov BYTE PTR [ebx], 5 ;DS as the default segment | mov BYTE PTR [ebx], 5 ;DS as the default segment | ||
| - | mov BYTE PTR ES:[ebx], 5 ;ES segment override (results in appearance of the byte 0x26 as the prefix) | + | mov BYTE PTR ES:[ebx], 5 ;ES segment override |
| + | ;(results in appearance of the byte 0x26 as the prefix) | ||
| </ | </ | ||
| * 0x2E – CS segment override | * 0x2E – CS segment override | ||
| Line 100: | Line 101: | ||
| The **operand size** and **address size override** prefixes can change the default size of operands and addresses. For example, if the processor operates in 32-bit mode, using the 0x66 prefix changes the size of an operand to 16 bits, and using the 0x67 prefix changes the address encoding from 32 bits to 16 bits. To better understand the behaviour of prefixes, let us consider a simple instruction with different variants. Let's start with a 32-bit processor. | The **operand size** and **address size override** prefixes can change the default size of operands and addresses. For example, if the processor operates in 32-bit mode, using the 0x66 prefix changes the size of an operand to 16 bits, and using the 0x67 prefix changes the address encoding from 32 bits to 16 bits. To better understand the behaviour of prefixes, let us consider a simple instruction with different variants. Let's start with a 32-bit processor. | ||
| <code asm> | <code asm> | ||
| - | mov BYTE PTR [ebx], 0x5 ;encoded as 0xC6, 0x03, 0x05 | + | ;encoding |
| - | mov WORD PTR [ebx], 0x5 ;encoded as 0x66, 0xC7, 0x03, 0x05, 0x00 | + | mov BYTE PTR [ebx], 0x5 ; |
| - | mov DWORD PTR [ebx], 0x5 ;encoded as 0xC7, 0x03, 0x05, 0x00, 0x00, 0x00 | + | mov WORD PTR [ebx], 0x5 ; |
| + | mov DWORD PTR [ebx], 0x5 ;0xC7, 0x03, 0x05, 0x00, 0x00, 0x00 | ||
| </ | </ | ||
| - | We can notice that because the default operand size is a 32-bit doubleword, | + | We can notice that because the default operand size is a 32-bit doubleword, |
| The address override prefix (0x67) appears if we change the register to a 16-bit bx. | The address override prefix (0x67) appears if we change the register to a 16-bit bx. | ||
| <code asm> | <code asm> | ||
| - | mov BYTE PTR [bx], 0x5 ;encoded as 0x67, 0xC6, 0x07, 0x05 | + | ; |
| - | mov WORD PTR [bx], 0x5 ;encoded as 0x67, 0x66, 0xC7, 0x07, 0x05, 0x00 | + | mov BYTE PTR [bx], 0x5 ; |
| - | mov DWORD PTR [bx], 0x5 ;encoded as 0x67, 0xC7, 0x07, 0x05, 0x00, 0x00, 0x00 | + | mov WORD PTR [bx], 0x5 ; |
| + | mov DWORD PTR [bx], 0x5 ;0x67, 0xC7, 0x07, 0x05, 0x00, 0x00, 0x00 | ||
| </ | </ | ||
| The same situation can be observed if we use a 32-bit address register (ebx) and assemble the same instructions for a 64-bit processor. | The same situation can be observed if we use a 32-bit address register (ebx) and assemble the same instructions for a 64-bit processor. | ||
| <code asm> | <code asm> | ||
| - | mov BYTE PTR [ebx], 0x5 ;encoded as 0x67, 0xC6, 0x03, 0x05 | + | ;encoding |
| - | mov WORD PTR [ebx], 0x5 ;encoded as 0x67, 0x66, 0xC7, 0x03, 0x05, 0x00 | + | mov BYTE PTR [ebx], 0x5 ; |
| - | mov DWORD PTR [ebx], 0x5 ;encoded as 0x67, 0xC7, 0x03, 0x05, 0x00, 0x00, 0x00 | + | mov WORD PTR [ebx], 0x5 ; |
| + | mov DWORD PTR [ebx], 0x5 ;0x67, 0xC7, 0x03, 0x05, 0x00, 0x00, 0x00 | ||
| </ | </ | ||
| While we use a native 64-bit address register in a 64-bit processor, the address size override prefix disappears. | While we use a native 64-bit address register in a 64-bit processor, the address size override prefix disappears. | ||
| <code asm> | <code asm> | ||
| - | mov BYTE PTR [rbx], 0x5 ;encoded as 0xC6, 0x03, 0x05 | + | ;encoding |
| - | mov WORD PTR [rbx], 0x5 ;encoded as 0x66, 0xC7, 0x03, 0x05, 0x00 | + | mov BYTE PTR [rbx], 0x5 ; |
| - | mov DWORD PTR [rbx], 0x5 ;encoded as 0xC7, 0x03, 0x05, 0x00, 0x00, 0x00 | + | mov WORD PTR [rbx], 0x5 ; |
| + | mov DWORD PTR [rbx], 0x5 ;0xC7, 0x03, 0x05, 0x00, 0x00, 0x00 | ||
| </ | </ | ||
| Line 142: | Line 147: | ||
| <code asm> | <code asm> | ||
| - | mov BYTE PTR [r8], 0x5 ;encoded as 0x41, 0xC6, 0x00, 0x05 | + | ;encoding |
| - | mov BYTE PTR [r9], 0x5 ;encoded as 0x41, 0xC6, 0x01, 0x05 | + | mov BYTE PTR [r8], 0x5 ;0x41, 0xC6, 0x00, 0x05 |
| - | mov BYTE PTR [r10], 0x5 ;encoded as 0x41, 0xC6, 0x02, 0x05 | + | mov BYTE PTR [r9], 0x5 ;0x41, 0xC6, 0x01, 0x05 |
| - | mov DWORD PTR [r8], 0x5 ;encoded as 0x41, 0xC7, 0x00, 0x05, 0x00, 0x00, 0x00 | + | mov BYTE PTR [r10], 0x5 ; |
| - | mov QWORD PTR [rbx], 0x5 ;encoded as 0x48, 0xC7, 0x03, 0x05, 0x00, 0x00, 0x00 | + | mov DWORD PTR [r8], 0x5 ; |
| - | mov QWORD PTR [r8], 0x5 ;encoded as 0x49, 0xC7, 0x00, 0x05, 0x00, 0x00, 0x00 | + | mov QWORD PTR [rbx], 0x5 ;0x48, 0xC7, 0x03, 0x05, 0x00, 0x00, 0x00 |
| + | mov QWORD PTR [r8], 0x5 ; | ||
| </ | </ | ||
| The REX prefix | The REX prefix | ||
| Line 154: | Line 160: | ||
| =====Instruction opcode===== | =====Instruction opcode===== | ||
| The instruction opcode is the mandatory field in every instruction. It encodes the main function of the operation. Expanding the processor' | The instruction opcode is the mandatory field in every instruction. It encodes the main function of the operation. Expanding the processor' | ||
| - | < | + | < |
| opcode | opcode | ||
| 0x0F opcode | 0x0F opcode | ||
| Line 166: | Line 172: | ||
| * 0x8F Three-byte XOP | * 0x8F Three-byte XOP | ||
| VEX-encoded instructions are written with V at the beginning. Let's look at the example of the blending instruction. | VEX-encoded instructions are written with V at the beginning. Let's look at the example of the blending instruction. | ||
| - | < | + | < |
| - | blendvpd xmm0, xmm1 ; | + | ;encoding |
| - | vblendvpd xmm0, xmm1, xmm2, xmm3 ; encoded as 0xC4, 0xE3, 0x71, 0x4B, 0xC2, 0x30 | + | blendvpd xmm0, xmm1 ; |
| + | vblendvpd xmm0, xmm1, xmm2, xmm3 ;0xC4, 0xE3, 0x71, 0x4B, 0xC2, 0x30 | ||
| </ | </ | ||
| The first blendvpd instruction has only two arguments; in this encoding scheme is not possible to encode more. It uses the mandatory prefix 0x66 and 0x0F, 0x38 escape sequence. The second version, vblendvpd, has four arguments. It is encoded with a three-byte VEX escape sequence 0xC4, 0xE3, 0x71. | The first blendvpd instruction has only two arguments; in this encoding scheme is not possible to encode more. It uses the mandatory prefix 0x66 and 0x0F, 0x38 escape sequence. The second version, vblendvpd, has four arguments. It is encoded with a three-byte VEX escape sequence 0xC4, 0xE3, 0x71. | ||
| Line 200: | Line 207: | ||
| 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. | ||
| <code asm> | <code asm> | ||
| - | ; | + | ;encoding |
| - | mov al, dl ;encoded as 0x88, 0xD0 11 010 000 | + | mov al, dl ;0x88, 0xD0 11 010 000 |
| - | mov ax, dx ;encoded as 0x89, 0xD0 11 010 000 | + | mov ax, dx ;0x89, 0xD0 11 010 000 |
| - | mov dx, si ;encoded as 0x89, 0xF2 11 110 010 | + | mov dx, si ;0x89, 0xF2 11 110 010 |
| - | mov si, dx ;encoded as 0x89, 0xD6 11 010 110 | + | mov si, dx ;0x89, 0xD6 11 010 110 |
| </ | </ | ||
| Notice that in the first and second lines, different opcodes are used, but the MOD R/M bytes are identical. The type of instruction determines the order of data transfer. | Notice that in the first and second lines, different opcodes are used, but the MOD R/M bytes are identical. The type of instruction determines the order of data transfer. | ||
| Line 210: | Line 217: | ||
| Now, a few examples of indirect addressing without displacement. | Now, a few examples of indirect addressing without displacement. | ||
| <code asm> | <code asm> | ||
| - | | + | ;encoding |
| - | mov dx, | + | mov dx, |
| - | mov dx, | + | mov dx, |
| - | mov dx,[bx+di];encoded as 0x8B, 0x11 00 010 001 Reg. only addr. | + | mov dx,[bx+di] ;0x8B, 0x11 00 010 001 Reg. only addr. |
| - | mov cx,[bx+di];encoded as 0x8B, 0x09 00 001 001 Reg. only addr. | + | mov cx,[bx+di] ;0x8B, 0x09 00 001 001 Reg. only addr. |
| </ | </ | ||
| Now, a few examples of indirect addressing with displacement. | Now, a few examples of indirect addressing with displacement. | ||
| <code asm> | <code asm> | ||
| - | | + | ;encoding |
| - | mov dx,[bp+62];encoded as 0x8B, 0x56, 0x3E 01 010 110 | + | mov dx,[bp+62] ;0x8B, 0x56, 0x3E 01 010 110 |
| - | mov [bp+62],dx;encoded as 0x89, 0x56, 0x3E 01 010 110 | + | mov [bp+62],dx ;0x89, 0x56, 0x3E 01 010 110 |
| - | mov dx,[si+13];encoded as 0x8B, 0x54, 0x0D 01 010 100 | + | mov dx,[si+13] ;0x8B, 0x54, 0x0D 01 010 100 |
| - | mov si, | + | mov si, |
| </ | </ | ||
| - | If we look in fort two lines, we can observe that the MOD R/M bytes are identical. The only difference is the opcode, which determines the direction of the data transfer. | + | If we look at the first two lines, we can observe that the MOD R/M bytes are identical. The only difference is the opcode, which determines the direction of the data transfer. |
| Notice also that the last instruction is encoded as BP + displacement, | Notice also that the last instruction is encoded as BP + displacement, | ||
| Line 292: | Line 299: | ||
| | 64-bit index register | | 64-bit index register | ||
| || | || | ||
| - | ^ Bits B.Index | + | ^ Bits X.Index |
| | 32-bit index register | | 32-bit index register | ||
| Line 311: | Line 318: | ||
| <code asm> | <code asm> | ||
| ;MOD R/M (second byte) is 0x04 for all instructions: | ;MOD R/M (second byte) is 0x04 for all instructions: | ||
| - | ; MOD REG R/M | + | ; MOD REG R/M |
| - | | + | |
| ;SIB (third byte) is 0x0B, 0x4B, 0x8B or 0xCB: | ;SIB (third byte) is 0x0B, 0x4B, 0x8B or 0xCB: | ||
| - | ; Scale Index Base Scale Index Base | + | ; |
| - | mov eax, [ebx+ecx] | + | mov eax, [ebx+ecx] |
| - | mov eax, [ebx+ecx*2] | + | mov eax, [ebx+ecx*2] ;0x8B, 0x04, 0x4B 01 |
| - | mov eax, [ebx+ecx*4] | + | mov eax, [ebx+ecx*4] ;0x8B, 0x04, 0x8B 10 |
| - | mov eax, [ebx+ecx*8] | + | mov eax, [ebx+ecx*8] ;0x8B, 0x04, 0xCB 11 |
| </ | </ | ||
| Line 326: | Line 333: | ||
| <code asm> | <code asm> | ||
| ;REX prefix (first byte) is 0x48 for all instructions: | ;REX prefix (first byte) is 0x48 for all instructions: | ||
| - | ; | + | ; |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| ;MOD R/M (second byte) is 0x04 for all instructions: | ;MOD R/M (second byte) is 0x04 for all instructions: | ||
| - | ; MOD R.REG R/M | + | ; MOD R.REG R/M |
| - | | + | |
| - | ; Scale X.Index B.Base | + | ; |
| - | mov rax, [rbx+rcx] | + | mov rax, [rbx+rcx] |
| - | mov rax, [rbx+rcx*2] | + | mov rax, [rbx+rcx*2] ;0x48, 0x8B, 0x04, 0x4B 01 |
| - | mov rax, [rbx+rcx*4] | + | mov rax, [rbx+rcx*4] ;0x48, 0x8B, 0x04, 0x8B 10 |
| - | mov rax, [rbx+rcx*8] | + | mov rax, [rbx+rcx*8] ;0x48, 0x8B, 0x04, 0xCB 11 |
| </ | </ | ||
| Line 346: | Line 353: | ||
| <code asm> | <code asm> | ||
| - | ; Scale X.Index B.Base | + | ; |
| - | mov rax, [r10+rcx] | + | mov rax, [r10+rcx] |
| - | mov rax, [rbx+r11] | + | mov rax, [rbx+r11] |
| - | mov r12, [rbx+rcx] | + | mov r12, [rbx+rcx] |
| - | ;Last instruction has the MOD R/M REG field extended | + | ;Last instruction has the MOD R/M REG field extended |
| - | | + | |
| - | | + | |
| - | | + | |
| </ | </ | ||
| - | Certainly, the presented examples do not exhaust all possible situations. For a more detailed explanation, | + | 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, | ||