Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Assembling, Linking and Executing Programs Objective: To cover the steps in assembling, linking, and executing programs The symbolic instructions that you code in assembly language are known as the source program. You use an assembler program to translate the source program into machine code, known as the object program. Finally, you use a linker program to complete the machine addressing for the object program, generating an executable module. The following figure provides a chart of the steps required to assemble link, and execute a program. 1. The assembly step involves translating the source code into object code and generating an intermediate .OBJ (object) file, or module. One of the assembler’s tasks is to calculate the offsets for every data item in the data segment and for every instruction in the code segment. The assembler also creates a header immediately in front of the generated .OBJ module; part of the header contains information about incomplete addresses. 2. The link step involves converting the .OBJ module to an .EXE machine code module. The linker’s tasks include completing any addresses left open by the assembler and combining separately assembled programs into one executable module. 3. The last step is to load the program for execution. Because the loader knows where the program is going to load in memory, it is now able to resolve any remaining address still left incomplete in the header. The loader drops the header and creates a program segments prefix (PSP) immediately before the program loaded in memory. Assembling a Source program The assembler converts your source statements into machine code and displays any error messages on the screen. Optional output files from the assembly step are object (.OBJ), listing (.LST), and cross reference (.CRF or .SBR). You probably often request an .LST file, especially when it contains error diagnostics or you want to examine the generated machine code. The Microsoft MASM 6.1 assembler use ML command. The ML command allows you to assemble and link any number of programs into one executable module. The format for the ML command to assemble and link is ML [options] filename.ASM [[options] filename.ASM] ….. [/LINK options] Page 1 Kay C.H. Command-line options are case-sensitive and begin with a slash (/) character. /c /Fl /Fr /Sn /Zd Assemble, do not link Generate a listing (.LST) Generate a .SBR (cross-reference) file Suppress listing of the symbol table Include line number debugging information A useful command is simply ML -?, which displays the command-line syntax and options Page 2 Kay C.H. Using Conventional Segment Definitions Stack segment This definition of 32 words is a realistic size for a stack because a large program may require many interrupts for input/output and calls to subprograms, all involving use of the stack. If the stack is too small to contain all the items pushed into it, neither the assembler nor the linker warns you, and the executing program may crash in an unpredictable way. Data segment. FLDD defines a word (two bytes) initialized with decimal value 215 FLDE defines a word initialized with decimal value 125 FLDF is coded as a DW with ? in the operand to define a word with an uninitialized constant. Page 3 Kay C.H. Code Segment The assembler recognizes the reference to a segment and assumes its address. Note the machine code to the left: B8----R. The four hyphens mean that at this point the assembler cannot determine the address of DATASEG; the system determines this address only when the object program is linked and load for execution. Because the loader may locate a program anywhere in memory, the assembler leaves the address open and indicates the fact with an R (for relocatable). Although the loader automatically initializes SS and CS when it loads a program for execution, it is your responsibility to initialize DS, and ES if required. Segments and Groups table The table provides the length in bytes of each segment, the alignment(all the paragraphs), the combine type, and the class. Symbols table This table provides the names of data fields in the data segment (FLDD, FLDE, and FLDF) and the labels applied to instructions in the code segment. TYPE F PROC means far procedure. The Value column gives the offset form the beginning of the segment for names, labels, and procedures. Two-Pass Assembler Assemblers typically make two or more passes through a source program in order to resolve forward references to addresses not yet encountered in the program. During pass 1, the assembler reads the entire source program and constructs a symbol table of names and labels used in the program. Pass 1 determines the amount of code to be generated for each instruction. During pass 2, the assembler uses the symbol table that it constructed in pass 1. it knows the length and relative position of each data field and instruction, it can complete the object code for each instruction. MASM constructs object code based on what it supposes is the length of each generated machine language instruction. If there are any differences between pass 1 and pass 2 concerning instruction lengths, MASM issues an error message “Phase error between passes”. Linking an object program MASM 6.1 performs assemble and link with the ML command. The linker performs the following functions: Combines, if requested, more than one separately assembled module into one executable program, such as two or more assembly programs or an assembly program with a C program. Generate an .EXE module and initializes it with special instructions to facilitate its subsequent loading for execution. The output files from the link step are executable (.EXE), map (.MAP), and library (.LIB). Page 4 Kay C.H. EXECUTING A PROGRAM If the .EXE file is in the default drive, you could ask the loader to read it into memory by typing “A05ASM1” If you omit typing the file extension, the loader assumes it is an executable .EXE and .COM program. Run it under DEBUG and use Trace commands to step through its execution. DEBUG n:A05ASM1.EXE To view the stack segment, key in D SS:0 To view the code segment, key in D CS:0 To view the contents of the registers, press R followed by <Enter> Error Diagnostics The assembler provides diagnostics for any programming errors that violate its rules. The program in Figure 5-5 is similar to the one in Figure 5-2, except that it has a number of intentional errors inserted for illustrative purposes. Page 5 Kay C.H. Page 6 Kay C.H.