Author Topic: mov instruction problem!  (Read 16191 times)

nobody

  • Guest
mov instruction problem!
« on: May 25, 2009, 03:33:40 PM »
hello

I am a simple question:

is this code wrong?

yyy  resd  1

mov yyy, eax

nasm print this error:     invalid combination of opcode and operands
I know that the only limitation on mov instruction is mov from mem to mem, but why this is error?

please help me!

mene mene tekel

  • Guest
Re: mov instruction problem!
« Reply #1 on: May 25, 2009, 04:17:11 PM »
Recommand: set default bits to 32
Define uninitialized data in a .bss segment
Give nasm instruction about how many bytes to write a memory location
Then it looks like:

bits  32;32bit code

mene mene tekel

  • Guest
Re: mov instruction problem!
« Reply #2 on: May 25, 2009, 04:19:46 PM »
Recommand: set default bits to 32
Define uninitialized data in a .bss segment
Give nasm instruction about how many bytes to write a memory location
Then it looks like:

bits 32    ;32bit code
segment .bss
yyy  resd   1
segment .code
mov   dword [yyy], eax

Martin

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: mov instruction problem!
« Reply #3 on: May 25, 2009, 05:27:55 PM »
> I am a simple question:

I am a simple answer. :)

> is this code wrong?

Yes.

>      yyy  resd  1
>
>      mov yyy, eax

Nasm evaluates "yyy" as some number... possibly zero, possibly 0x400???, possibly 0x8049???, or so. So, in Nasm syntax, you're trying to move the contents of eax into some number. Masm/Tasm syntax would mean "into memory location ...", but Nasm syntax requires "republican parentheses" ("[...]") to indicate a memory location. (Masm/Tasm requires "offset" to mean the "plain number" or "immediate addressing mode" - "mov eax, yyy" means the [contents] of memory at yyy in Masm/Tasm syntax, but the number "yyy" in Nasm syntax - either is legal, in that direction)

mov [yyy], eax

will do what you want.

Martin makes a good point that you may need to specify "bits 32" ("use 32" in section/segment declarations, actually), although it's perfectly legal in 16-bit code as well. Depends on what OS you're targetting, and which of Nasm's output formats you're using. Usually, when they don't specify an OS, it means Windows. In that case, you'd want either "-f obj" or "-f win32". "-f obj" defaults to 16 bit code, and *will* need to be told you want 32 bits! "-f win32" defaults to 32 bits, and doesn't need it.

Martin also mentions that "resd" (and its friends "resb", "resw", "resq", "resy", "reso", and "rest") want to be in "section .bss", while the code wants to be in "section .text" ("segment" is an alias for "section"). Section/segment names are meaningless in "-f obj" output mode, and "res?" in an initialized section/segment will be silently zeroed, other output formats warn and zero... but "do it right", you'll be glad for the habit.

There's a nice manual available online or for download at:

http://www.nasm.us

if all else fails. :)

But feel free to ask, if you've got other questions (tell us what OS!).

Best,
Frank