Let's assume we are writing assembly code using MASM 6.1 / 16 bit / huge data model, and we have a variable (label) named MY_VAR, a segment named MY_SEG and a segment group named MY_GROUP. Let's assume MY_VAR is placed in MY_SEG, and MY_SEG belongs to MY_GROUP.
Then, what is the difference between the following two statements:
1) MOV AX, MY_SEG
2) MOV AX, SEG MY_SEG:MY_VAR
Furthermore, what is the difference between the following two statements:
1) MOV AX, MY_GROUP
2) MOV AX, SEG MY_GROUP:MY_VAR
Note: MASM happily processes all of these statements. As expected (in my case), the results of each 1) and 2) are the same. But I would like to know for sure ...
Thank you very much,
Binarus
In MASM the label
MY_VARtranslates to the offset part of the address of MY_VAR relative to the segment it was declared in (if you use it likemov ax, MY_VAR) or relative the to segment you have assumed for the segment register you are using to access it (if you use it likemov ax, WORD PTR [MY_VAR]).As you know a given variable (in general a linear address) has multiple logical address, for example the variable at
8000hlinear can be accessed as0800h:0000hor0700h:1000hand so so.The form
MY_SEG:MY_VARtell the assembler to compute the offset relative to the segmentMY_SEG. So if MY_SEG starts at7000hlinear,MY_SEG2starts at6000hlinear then for a var MY_VAR at8000hlinearMY_SEG:MY_VARis 1000h andMY_SEG2:MY_VARis 2000h.The
SEGdirective compute the segment part of the logical address instead of the offset, it is the segment MASM used (again by the rules given above) to compute the offset.In your first instructions you are telling MASM to put the address (let's leaving relocation behind) of the segment
MY_SEGin AX (so if the segment starts at 5000h the value in AX is 500h).In your second instructions your are explicit telling MASM to use the segment MY_SEG in computing the offset of
MY_VARand then, by theSEGdirective, telling it to return the segment part instead, that isMY_SEG.So they are the same but the second one is redundant.