3

The following two statements for initializing the data segment register

    mov AX, @Data  
    mov DS, AX

but the actual data segment address is known only when the loader loads the program in memory.
- What does the assembler puts in place of @data?
- When the loader gets the actual data segment base address does it replace the instruction mov AX, @data with the mov AX, Actual Base Address?
- Why it is necessary to write this statement when the actual base address is not known before the loading stage?

2
  • The assembler will likely note that mov AX, @Data requires a fixup. I believe the linker will fill @Data in with the segment relative to the beginning of the executable and mark it for fixup by the DOS loader (there is a fix up table in the header of the executable). When you run the program the DOS program loader will read the DOS header from the executable and add the segment where DOS physically loaded the program with the segment placed in the instruction by the linker. That will yield a run-time value for @Data. Commented Sep 30, 2016 at 21:04
  • The reason all this is done is because DOS programs can be loaded into memory in different locations. So these fixups have to be provided. Without doing mov AX, @Data mov DS, AX in your code, the DS (Data segment) will not be known at runtime by the program itself. Without setting up DS you likely won't read and write variables in memory where they expect to be read and written from and your program just won't work as expected. Commented Sep 30, 2016 at 21:08

1 Answer 1

1

Each time you write an instruction like mov AX, @Data the compiler/assembler inserts 3 bytes in your program:

  • The 1st byte is the opcode, in case of AX it will be 0B8h
  • The 2nd and 3rd bytes together represent a number.
  • What does the assembler puts in place of @data?

This number represents the distance between the start of the executable (when loaded in memory) and the start of the data section. This number is expressed in paragraphs aka chunks of 16 bytes.

  • When the loader gets the actual data segment base address does it replace the instruction mov AX, @data with the mov AX, Actual Base Address?

The loader only updates the 2nd and 3rd bytes. It never touches the 1st byte!

  • Why it is necessary to write this statement when the actual base address is not known before the loading stage?

Everywhere your write these instructions (there can be many of these), you provide DOS with placeholders where the DOS loader can insert the correct addresses.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.