MASM SEG operator

1k views Asked by At

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

1

There are 1 answers

4
AudioBubble On BEST ANSWER

In MASM the label MY_VAR translates to the offset part of the address of MY_VAR relative to the segment it was declared in (if you use it like mov 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 like mov 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 8000h linear can be accessed as 0800h:0000h or 0700h:1000h and so so.
The form MY_SEG:MY_VAR tell the assembler to compute the offset relative to the segment MY_SEG. So if MY_SEG starts at 7000h linear, MY_SEG2 starts at 6000h linear then for a var MY_VAR at 8000h linear MY_SEG:MY_VAR is 1000h and MY_SEG2:MY_VAR is 2000h.

The SEG directive 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_SEG in 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_VAR and then, by the SEG directive, telling it to return the segment part instead, that is MY_SEG.

So they are the same but the second one is redundant.