I am working on a project in Assembly in which a certain weird property of offset
instruction appeared. I wrote down the following instruction:
mov ax, offset offset variable
When I executed the program, I expected an error, but the program ran without showing any errors and the line mov ax, offset offset variable
appeared in the debugging tool as mov ax, 0784
, which is the same way that the instruction mov ax, offset variable
appears (0784 is the offset of variable in the data segment). After being confused for a moment, I executed this instruction: mov ax, offset offset offset offset offset offset variable
(yes, a bit funny) and it had the same effect and the same result (ax value became the offset of the variable)
My questions are why is the value stored in register AX is the offset of the variable (can I be sure that always offset offset = offset?), why those lines do not produce any errors? And, if there is a meaning for offset of offset, what is the meaning of it? (the meaning which assembler follows in these examples)
Thanks. I am using assembly x86, TASM, DOSBox emulator, and Turbo Debugger if it is relevant :)
The address of variable
isn't itself stored in memory anywhere, adding more offset
keywords isn't going to create pointer objects and give you the address of a pointer to a pointer to variable.
It's clearly just the same as using offset
once. That's presumably a quirk of the parser in TASM: I'd guess that the parsing logic sees offset
and sets a flag internally, then starts parsing again until it gets to a variable name. If it sees offset
again, it just sets the same flag again and keeps going.
There's no reason or benefit to write code this way, so don't do it intentionally. Possibly it's convenient for making macros that work regardless of specifying offset
or not when you call it? Beyond that, keep your source simple.
(Fun fact: the GNU assembler also doesn't reject this, in .intel_syntax noprefix
mode. I still doubt this is an intentional feature, just an undocumented quirk.)