Jishan
Jishan

Reputation: 1684

Understanding PTR in context of Size Directives

I am trying to understand how PTR works in the context of Size Directive and I am quite confused/unsure if my understanding is correct.

Let me take the 3 statements from here and try to explain what I think is happening here. The statements are as follows (x86, Intel Syntax):

mov BYTE PTR [ebx], 2
; Move 2 into the single byte at the address stored in EBX. 

mov WORD PTR [ebx], 2
; Move the 16-bit integer representation of 2 into the 2 bytes starting at the address in EBX. 

mov DWORD PTR [ebx], 2     
; Move the 32-bit integer representation of 2 into the 4 bytes starting at the address in EBX. 

So for mov BYTE PTR [ebx], 2, this is what I believe is happening:

BYTE (8 Bits) → [2] (that is 2h, or am I wrong?)

consecutively:

WORD (16 Bits = BYTE + BYTE) → [0][2] (that is 0002h, or am I wrong?)
DWORD (32 Bits = WORD + WORD) → [0][0][0][2] (that is 0000,0002h, or am I wrong?)

I am choosing the end as

starting at the address in EBX.

because that's what I believe is the little-endian format in x86.

Is my understanding correct? Thanks.

Upvotes: 0

Views: 76

Answers (1)

Peter Cordes
Peter Cordes

Reputation: 364533

Yup, looks correct.

The fundamental unit is 1 byte, though. 4-bit hex digits are just a result of using that way of representing numbers in ASCII text. The machine code for a byte-store (mov r/m8, imm8) is opcode, modrm, imm8, so the 0 in the high 4 bits is explicit in the machine code created by your assembler.

Same for the other sizes, actually, because there's no form of mov that sign-extends a byte. The assembler has to encode the imm32 as 02 00 00 00 (because immediates in x86 machine code are little endian, same as data.)

You might find it informative to look at disassembly which includes a hexdump of the machine code. One key thing to understand about asm is that it's just a text syntax for expressing machine-code. What really matters is what the machine can do; once you know that you'll understand why certain limits exist. For example, the scale factor in [ebx + edi*8] is encoded as a 0..3 shift count in a 2-bit field, which is why scale factors are limited to 1,2,4,8.

Different asm syntaxes exist for describing the same machine code. In asm, you can leave out the dword ptr when it's implied by a register operand, but in machine code the operand size is always determined by the opcode + prefixes.

Upvotes: 1

Related Questions