1

I am trying to do some basic YASM coming from TASM, and this line of code will error:

mov [var], 7

I have defined the variable like so: var db 5.
Even after trying to do var: db 5 it still errored out and said:

error: invalid size for operand 1

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
sef sf
  • 117
  • 1
  • 8

1 Answers1

5

Unlike TASM, YASM/NASM don't look at the declaration of var to decide if it is byte, word, dword, etc. The operand size needs to be specified in any instruction where it isn't implicit from the registers being used. So you must write

mov byte [var], 7

Note that

mov [var], bl

doesn't need the byte, because the 8-bit operand size is inferred from the use of the 8-bit register bl.

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
  • @sefsf: It applies to `[var]`. Though actually `mov [var], byte 7` also works - both operands to `mov` must have the same size, so it's enough if it knows the size of one of them. – Nate Eldredge Feb 10 '21 at 06:33
  • They are other possible methods how to specify the operand size: GAS uses mnemonic suffix `b`,`w`,`l` €ASM looks at the type of `var`, too, otherwise it uses either suffix or instruction modifier, e.g. `data=byte`, see https://euroassembler.eu/eadoc/#DATAeq – vitsoft Feb 10 '21 at 07:25
  • @nate-eldredge is this just for MOV or any instruction that calls for the variable? – sef sf Feb 10 '21 at 12:51
  • @sefsf: It applies to any instruction with a memory operand. You need it in particular for instructions whose other operand is an immediate (`add byte [var], 7` or `add [var], byte 7`), one-operand instructions (`neg byte [var]`), and instructions with two operands of different sizes (`movsx eax, byte [var]`). – Nate Eldredge Feb 10 '21 at 15:30
  • @nate-eldredge Thanks! – sef sf Feb 10 '21 at 15:45