user1013944
user1013944

Reputation:

What are the rules of specification of size?

Even though i already specified the size of a variable, i don't understand why i should do it again when using an operation.

segment .bss
_n resd 1
_m resd 1

segment .text
mov **dword**[_n], 10 

Would you give me a hand?

Upvotes: 0

Views: 415

Answers (2)

James Holderness
James Holderness

Reputation: 23001

In many other assemblers you wouldn't have to specify the size again. This is a design choice of NASM. See section 2.2.3 of the manual:

NASM, by design, chooses not to remember the types of variables you declare. Whereas MASM will remember, on seeing var dw 0, that you declared var as a word-size variable, and will then be able to fill in the ambiguity in the size of the instruction mov var,2, NASM will deliberately remember nothing about the symbol var except where it begins, and so you must explicitly code mov word [var],2.

The reason for this, is one of their design goals was that you should be able to look at a single line of NASM code and still tell exactly what machine code it would generate (where possible). If you just did mov [_n],10 without a type specifier, you wouldn't know what that was going to generate without referring back to where _n was originally defined.

Upvotes: 1

ady
ady

Reputation: 173

mov **dword**[_n], 10

The assembler needs to know the amount of memory to reserve for an operation and the left hand operand is used

mov al,[_n] should be ok even if [_n] is a dword

but

mov [_n], 10

tells the assembler nothing about size unless dword is used

After a while it becomes second nature to specify size in an asm program

Can also be useful for chopping up dwords into words and bytes, al will be loaded with the lowest byte in dword [_n]

You could also do mov word [_n],500000 and get a weird number because it's too big

but mov word [_n],eax would fail because eax is regarded as a dword

asm can be a bit quirky

Upvotes: 0

Related Questions