-2
section .data 

var dd 10

section .text 

add [var] , eax

for above code nasm gives error operation size not specified,

but if we reverse it add eax, [var] it doesn't gives error.
why error for only first and not for second type ?

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
ryan
  • 21
  • 3
  • Both versions compile fine with my NASM 2.11.06, what version do you have? You can check it with `nasm -v` – Margaret Bloom Sep 10 '16 at 14:10
  • @MargaretBloom 2.12.11 compiling using command `nasm filename.asm -f elf32 -g` – ryan Sep 10 '16 at 14:27
  • WAG: Try `add dword [var], eax`. Rationale: The assembler needs to disambiguate. – Frank C. Sep 10 '16 at 14:32
  • 2
    @FrankC. NASM should be able to disambiguate by seeing that the source is a 32-bit register (EAX) and be able to move it to the destination as 32-bits. If it were moving an immediate value to a memory address then I can see the necessity to disambiguate. I've been using NASM for well over a decade and I do not recall a time where this wouldn't assemble. – Michael Petch Sep 10 '16 at 17:11
  • 2
    @ryan, are you absolutely sure you got an error with `add [var] , eax` . I am wondering if you tried to do something like `add [var] , 1` (adding an immediate value to the value at a memory location). That should give you the error `operation size not specified`. If moving an immediate value to memory you need to specify the size with something like `add [var] , dword 1` . This says that the value 1 should be treated/encoded as a 32-bit _DWORD_ and stored in the destination memory address as a 32-bit value. – Michael Petch Sep 10 '16 at 17:19
  • In my comment to Frank I meant to say _If it were ADDING an immediate value to a memory address_ – Michael Petch Sep 10 '16 at 18:58
  • 1
    Downvoted for invalid premise, since it's attracted two bogus answers already. If you were really using `add [var] , 1`, then edit the question to that. – Peter Cordes Dec 14 '16 at 20:56
  • Closing as a duplicate of a question with a proper explanation, even though in this current stat it's not really a dup. – Peter Cordes Jun 21 '21 at 15:35

2 Answers2

-2

You need specify size like this:

add dword [var],eax
  • 1
    No you don't. In NASM, the register operand implies the operand size. Comments on the question point out that other people can assemble both ways just fine with NASM. – Peter Cordes Dec 14 '16 at 20:54
-4

Because the first operand is the destination operand and needs to be a register so instead just move it back :

add eax, [var]
mov [var], eax
Hen3
  • 175
  • 8
  • 4
    The [_ADD_](http://www.felixcloutier.com/x86/ADD.html) instruction supports the destination being a memory address and the source being a register. In this case `add [var], eax` should add the value of _EAX_ to the data at the address associated with the label `var`. From the x86 instruction set `ADD r/m32, r32 - Add r32 to r/m32.` – Michael Petch Sep 10 '16 at 17:14