Copyright (C) 2000-2012 |
GNU Info (nasm.info)Section 2.2.22.2.2. NASM Requires Square Brackets For Memory References ---------------------------------------------------------- NASM was designed with simplicity of syntax in mind. One of the design goals of NASM is that it should be possible, as far as is practical, for the user to look at a single line of NASM code and tell what opcode is generated by it. You can't do this in MASM: if you declare, for example, foo equ 1 bar dw 2 then the two lines of code mov ax,foo mov ax,bar generate completely different opcodes, despite having identical-looking syntaxes. NASM avoids this undesirable situation by having a much simpler syntax for memory references. The rule is simply that any access to the _contents_ of a memory location requires square brackets around the address, and any access to the _address_ of a variable doesn't. So an instruction of the form `mov ax,foo' will _always_ refer to a compile-time constant, whether it's an `EQU' or the address of a variable; and to access the _contents_ of the variable `bar', you must code `mov ax,[bar]'. This also means that NASM has no need for MASM's `OFFSET' keyword, since the MASM code `mov ax,offset bar' means exactly the same thing as NASM's `mov ax,bar'. If you're trying to get large amounts of MASM code to assemble sensibly under NASM, you can always code `%idefine offset' to make the preprocessor treat the `OFFSET' keyword as a no-op. This issue is even more confusing in `a86', where declaring a label with a trailing colon defines it to be a `label' as opposed to a `variable' and causes `a86' to adopt NASM-style semantics; so in `a86', `mov ax,var' has different behaviour depending on whether `var' was declared as `var: dw 0' (a label) or `var dw 0' (a word-size variable). NASM is very simple by comparison: _everything_ is a label. NASM, in the interests of simplicity, also does not support the hybrid syntaxes supported by MASM and its clones, such as `mov ax,table[bx]', where a memory reference is denoted by one portion outside square brackets and another portion inside. The correct syntax for the above is `mov ax,[table+bx]'. Likewise, `mov ax,es:[di]' is wrong and `mov ax,[es:di]' is right. automatically generated by info2www version 1.2.2.9 |