NASM - The Netwide Assembler

NASM Forum => Programming with NASM => Topic started by: Logman on October 27, 2013, 04:57:28 PM

Title: NASM Size Instruction Format
Post by: Logman on October 27, 2013, 04:57:28 PM
All:

Which format is correct for using a BYTE, WORD, etc. size instruction:

MOV EAX, WORD [myMemoryVariable]

or

MOV WORD EAX, [myMemoryVariable]

Somehow, both seem to work, but there must be a preferred style in NASM.

Thanks, LOGMAN
Title: Re: NASM Size Instruction Format
Post by: encryptor256 on October 27, 2013, 05:02:57 PM
Hi!

This is more correct: MOV EAX, WORD [myMemoryVariable]

But..

EAX is 32 bit register.

Like this would be perfect:

MOV AX, WORD [myMemoryVariable] => Move word from this memory location into this 16 bit register

AX = 16 Bit, WORD = 16 bit

The same for 32 bit.

MOV EAX, DWORD [myMemoryVariable]

EAX = 32bit, DWORD = 32 bit.
Title: Re: NASM Size Instruction Format
Post by: Frank Kotler on October 27, 2013, 05:25:09 PM
Code: [Select]
movzx eax, word [myMemoryVariable]
would be correct.
Code: [Select]
movzx word eax, [myMemoryVariable]
won't work, and
Code: [Select]
mov eax, word [myMemoryVariable]
is just plain wrong.

Best,
Frank

Title: Re: NASM Size Instruction Format
Post by: Logman on October 27, 2013, 07:06:32 PM
Frank Kotler:

Thanks for the reply. I've been curious about this for a long time. Your explanation clears it up, but I think I probably used a bad example.

My curiosity was based on an example in Paul Carter's Book where he posted an example like this:

    mov eax, dword [esi + 4*ecx]

Then I remember seeing another follow-up where the author used the size instruction like this:

    mov dword eax, [esi + 4*ecx]

and then I read this example in a user manual for inline assembly code:

    add [esi], dword 2

just when I thought it was more appropriate to write:

    add dword [esi], 2

and it got me to thinking about the proper placement of size instructions in NASM.

Logman
Title: Re: NASM Size Instruction Format
Post by: Frank Kotler on October 27, 2013, 07:47:17 PM
I usually put the size specifier first - "add dword [esi], 2" - but Nasm doesn't care. In the case where the size of the register determines the size of the operation, you don't need it at all, and I usually don't use it (just more typin'). However, as noted, after movzx/sx, Nasm wants it after the comma. So if you wanted to do it the same way every time, after the comma might be better. Programmer's choice. (It is good to be programmer!)

Best,
Frank

Title: Re: NASM Size Instruction Format
Post by: Logman on October 27, 2013, 08:32:48 PM
Thanks for the follow-up. I will do as you suggest--makes sense.

Logman