Search code examples
assemblycompiler-constructionx86

Assembly correct usage of word/byte/word ptr


I'm writing a little machine code generator for assembly. I have a question regarding immediate to memory instructions:

Let's take these 3 scenarios:

add [ebx+04], 0x1
add [ebx+04], 0x4040
add [ebx+04], 0x401000

What I do is going by the immediate constant, I check what is the smallest number of bytes it can fit in then I assume whether its byte ptr, word ptr, dword ptr

First one can fit in 1 byte so I assume it to be:

add byte ptr [ebx+04], 0x1

Second can fit in 2 bytes so I assume it to be:

add word ptr [ebx+04], 0x4040

third fits in 4 bytes so I assume it to be:

add dword ptr [ebx+04], 0x401000

Is this correct?


Solution

  • You most definitely need to accept size tags on these kind of instructions.

    But I wouln't dismiss the code to see what numbers fit, because you will need a way to decide on the possibiliy to use a smaller encoding based on sign extending a byte:

    add dword ptr [ebx+4], 1
    

    This can be encoded with a 4 byte immediate, but also more interestingly with a 1 byte immediate.