I've just started assembly programming, and I understand almost all of the concepts, but I am a little confused as to when I should use the offset operator, square brackets, or neither. In other words, I don't understand the difference between the following lines of code:

mov bx, var1
mov bx,
mov bx, offset var1

I would greatly appreciate it if someone could explain the difference.

Thank you!
Posted on 2008-08-12 16:04:52 by MattMik
hi MattMik,

mov bx,   the square brackets indicate that you want to access the contents located at the memory address labeled   
                        var1 and in this case move whatever value is there into the bx reg.

mov bx, var1    would move the address of 'var1' into bx and...

mov bx, offset var1  is the same thing as  mov bx, var1 in nasm.

anyway check out the nasm doc under "offset" for the real info

i'm a newbie so take with grain of salt and verify all :)
Posted on 2008-08-12 17:36:49 by scurvydog
Well, things depend a bit on the assembler you use.

For NASM, are required when you do indirection (ie., you want the contents of a variable and not it's address), and the offset operator isn't necessary.

For MASM, the offset operator is required when you want a variable's address, but you don't need brackets to specify indirection. In any cases, x86 only supports one level of indirection, so you can't do a thing like "mov eax, []" :)

Also, for LOCAL variables, you can't use offset since local variables are ferenced relative to ESP (or EBP, if you use that for stack frames) - if you need to offset of a local variable, you'll need to LEA instead.

Hope that was of some help :)
Posted on 2008-08-12 17:44:05 by f0dder
Great thanks! I think I will be switching to NASM from MASM, as my experiences with MASM haven't been that great. And from what I've read about NASM, it seems to fit my programming preferences a lot better.

Is there any difference when you use OFFSET in MASM from when you don't?
And how does NASM compare to Gas, the GNU assembler?

Posted on 2008-08-12 18:54:05 by MattMik
For MASM, the OFFSET implies that the address is hardcoded into your program, for example the label of some data or code ... but it won't work for addresses that are NOT hardcoded into the program.
An example of that is a LOCAL variable in a procedure.
To address a LOCAL variable, you would normally use the LEA instruction.

The following example is not meant to be cool or elegant, it's simply meant to show you how to use LEA (Load Effective Address) in order to address locals...also note (in regards to locals) that the square brackets can be used to denote an ARRAY, in this case, 256 elements of 'BYTE' size (simply 256 bytes).

FormatString db "Hello, %s. Your number is %lu",0
TitleString db "Read this",0

MyFunction proc uses ecx pStudentName:ptr BYTE, dStudentNumber:dword
local buf[256]:BYTE

lea ecx,buf
push ecx
invoke wsprintf, ecx, offset FormatString, pStudentName, dStudentNumber
pop ecx
invoke MessageBox,0,ecx,offset TitleString, MB_OK
MyFunction endp

Posted on 2008-08-12 22:11:34 by Homer

how does NASM compare to Gas, the GNU assembler?

here's an explanation of syntactic and semantic differences, between the two.

Posted on 2008-08-13 06:32:10 by avi