Author Topic: NASM Size Instruction Format  (Read 8228 times)

Offline Logman

  • Jr. Member
  • *
  • Posts: 16
  • Country: us
NASM Size Instruction Format
« 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

Offline encryptor256

  • Full Member
  • **
  • Posts: 250
  • Country: lv
  • Win64 .
    • On Youtube: encryptor256
Re: NASM Size Instruction Format
« Reply #1 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.
« Last Edit: October 27, 2013, 05:05:20 PM by encryptor256 »
Encryptor256's Investigation \ Research Department.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: NASM Size Instruction Format
« Reply #2 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


Offline Logman

  • Jr. Member
  • *
  • Posts: 16
  • Country: us
Re: NASM Size Instruction Format
« Reply #3 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
« Last Edit: October 27, 2013, 07:17:53 PM by Logman »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: NASM Size Instruction Format
« Reply #4 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


Offline Logman

  • Jr. Member
  • *
  • Posts: 16
  • Country: us
Re: NASM Size Instruction Format
« Reply #5 on: October 27, 2013, 08:32:48 PM »
Thanks for the follow-up. I will do as you suggest--makes sense.

Logman