Search code examples
assemblymasm

MASM segment directive


I am reading some assembly for MASM and I have trouble understanding the purpose of segment directive. How is it different from labels in address computation during the assembly? Assume the form directive as just name segment at addr, I dont much care for those other options it has. What is

BootSeg segment at 0x7c0
BootSeg end

good for if it has nothing within?

What value would you have in ax if you did:

mov ax, BootSeg

?


Solution

  • The segment directive is sort of multipurpose.

    The first use is to combine things into segments.

    The second use is to refer to (= calculate address of) objects in various segments properly. Depending on the segment of an object being accessed in your code, the assembler can insert appropriate segment override prefixes (es:, ss:, cs:, fs:, gs:) into the generated code. Likewise when calling a procedure from a different code segment, the assembler can generate the far call instruction instead of the near call. AFAIR, for that you actually need to mark the procedure itself as far (and that will turn all plain rets into far rets in the routine as well).

    The segments are then taken care of by the linker and turned into relocation information that's consumed by the OS.

    Why do we have these segments? Because the CPU has them and we can't always ignore their existence. There are DOS .COM programs that fit their code, data and stack into a single segment, in which case the program does not have to be complicated by the notion of segments (except those cases when it needs to access some "foreign" code/data, not from its own segment).

    And yes, the AT thing basically overlays one object on top of the other. So mov ax, BootSeg should get you ax = 0x7c0 just as with any other segment, except here the segment is known at "compile" time.

    Use a debugger, experiment.