1.3.1 Generating and Running Executable Programs

This section briefly lists all the actions that take place during each of the assembly steps. You can change the behavior of some of these actions in various ways, for example, by using macros instead of procedures, or by using the OPTION directive or conditional assembly. The other chapters in this book discuss specific programming methods; this list simply gives you an overview.

1.3.1.1 Assembling

The ML.EXE program does two things to create an executable program. First, it assembles the source code into an intermediate object file. Second, it calls the linker, LINK.EXE, which links the object files and libraries into an executable program (usually with the .EXE extension).

At assembly time, the assembler

Evaluates conditional-assembly directives, assembling if the conditions are true.

Expands macros and macro functions.

Evaluates constant expressions such as MYFLAG AND 80H, substituting the calculated value for the expression.

Encodes instructions and nonaddress operands. For example, mov cx, 13 can be encoded at assembly time because the instruction does not access memory.

Saves memory offsets as offsets from their segment.

Passes segments and segment attributes to the object file.

Saves placeholders for offsets and segments (relocatable addresses).

Outputs a listing if requested.

Passes messages (such as INCLUDELIB and .DOSSEG) directly to the linker.

See Section 1.3.3 for information about conditional assembly; see Chapter 9 for macros. Chapters 2 and 3 give further details about segments and offsets, and Appendix C explains listing files.

1.3.1.2 Linking

Once your source code is assembled, the resulting object file is passed to the linker. At this point, the linker may combine several object files into an executable program.

At link time, the linker

Combines segments according to the instructions in the object files, rearranging the positions of segments that share the same class or group.

Fills in placeholders for offsets (relocatable addresses).

Writes relocations for segments into the header of .EXE files (but not .COM files).

Writes an executable image.

Section 2.3.4, “Defining Segment Groups,” defines classes and groups. Chapter 3, “Using Addresses and Pointers,” explains segments and offsets.

1.3.1.3 Loading

The operating system loads the file generated by the linker into memory. When the executable file is loaded into memory, DOS

Reads the program segment prefix (PSP) header into memory.

Allocates memory for the program, based on the values in the PSP.

Loads the program.

Calculates the correct values for absolute addresses from the relocation table.

Loads the segment registers SS, CS, DS, and ES with values that point to the proper areas of memory.

Loads the instruction pointer (IP) to point to the start address in the code segment and the stack pointer (SP) to point to the stack.

Begins execution of the program.

The process is similar for OS/2.

See Section 1.2.7, “Registers,” for information about segment registers, the instruction pointer (IP), and the stack pointer (SP). See MASM online help or a DOS reference for more information on the PSP.

1.3.1.4 Running

Your program is now ready to run. Some program operations cannot be handled until the program runs, such as resolving indirect memory operands. See Section 7.1.1.2, “Indirect Operands.”