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
?
The
segmentdirective 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 aprocedurefrom a different code segment, the assembler can generate thefar callinstruction instead of thenear call. AFAIR, for that you actually need to mark theprocedureitself asfar(and that will turn all plainretsintofar retsin 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
ATthing basically overlays one object on top of the other. Somov ax, BootSegshould get youax= 0x7c0 just as with any other segment, except here the segment is known at “compile” time.Use a debugger, experiment.