Search code examples
assemblynasmmasm

MASM syntax for mov [string], WORD PTR 'HE'


I follow this tutorial on assembly: https://www.tutorialspoint.com/assembly_programming/assembly_addressing_modes.htm They use NASM. I use MASM.

I have this string:

string db 'hello string from asm here'

I want to modify the string in the program. I want to replace the first 2 letters.

mov [name],  dword 'Nuha'

So i try:

mov [string], WORD 'HE' 

Which trows error a2070. It can do this:

mov [string], 'H'   

Output: Hello string from asm here Then i found this:

mov word ptr [string], 'HE' 

it compiles, but reads it from the back to the front Output: EHllo string from asm here I want: HEllo string from asm here

So how do i move a word value into a string in MASM?

PS. Does anyone have a better source, tutorial for MASM and not NASM?

Thanks in advance,

Cody


Solution

  • HE as a word is worth '0x4845'. When you put it in memory, it is serialized as little endian (since you're using an intel x86 or x64 processor) => Bytes are swapped: hex dump: 0x45 0x48

    You have the behaviour you're expecting if you do:

     mov [string] WORD PTR, 'EH'
    

    (you could have figured out yourself but now you know the reason)

    I checked NASM documentation and they do a special thing with strings, probably because it is more convenient:

      dw    0x1234              ; 0x34 0x12 
      dw    'a'                 ; 0x61 0x00 (it's just a number) 
      dw    'ab'                ; 0x61 0x62 (character constant) 
      dw    'abc'               ; 0x61 0x62 0x63 0x00 (string) 
    

    note that dw "swaps" strings word/longs but not hex values.

    MASM is more "basic" and this swap is not done, which explains the "standard" behaviour you're experiencing.

    Note: it's a kind of a hack to do it that way. Calling a byte-to-byte copy routine is better and more generic. Porting such a code from MASM to NASM (or the other way round) could have catastrophic results.