Search code examples
pointersassemblyx86masm32

PTR Directive in ASM, how does it work?


I have this block of ASM code with a few variables and 1 instruction:

.data
g BYTE 32h
a DWORD 11111111h
h BYTE 64h
.code
mov ebx, DWORD PTR g

Could anyone explain why the value of ebx is not 11 11 11 32 instead of 00 00 00 32 or at least how does PTR work?

I thought that the PTR directive would represent the operand as a 32-bit operand ?

Thanks in advance.


Solution

  • See @Jester's comment if your code really looks like what you've posted.


    But judging by your question I'm guessing that your code actually contains this line instead:

    mov ebx, DWORD PTR g
    

    I thought that the PTR directive would represent the operand as a 32-bit operand ?

    That depends on what you mean by that. DWORD PTR would be used as a size specifier when the size is ambiguous.
    For example, the instruction mov [eax], 0 would be ambiguous because the assembler has no idea of knowing if you meant to write a byte, a word, a dword, etc. So in that case you could use DWORD PTR to state that you want to write a DWORD to memory: mov DWORD PTR [eax], 0.

    If you want to read a byte from memory and convert it to a DWORD you need to use movzx or movsx:

    movzx ebx, BYTE PTR g   ; if g should be treated as unsigned
    movsx ebx, BYTE PTR g   ; if g should be treated as signed