Invoking MASM and LINK
Assembling, Linking, and Debugging
By now, we have seen that it's fairly easy to assemble and run short programs with Debug. You should have read the handout on Writing Assembly Language Programs We will now begin using an assembler; a utility program that converts a source program into an object file, and a linker; a program that converts object files into executable programs. We woll also use a loader when we invoke the command at the MS-DOS prompt.
One major advantage to using an assembler is that source programs can be more easily modified with a text editor than with DEBUG. Another is that you can use symbolic names for variables, rather than hard-coded numeric addresses. The linker has one primary advantage- programs can take advantage of existing libraries full of useful subroutines; the subroutines are "attached" to our programs by the linker.
Assembly and Linking is A Two-Stage Process
FIGURE 1
Figure 1 shows the stages a program goes through from source code creation through the creation of the executable file. There are a number of individual steps involved.
The programmer uses a text editor to create the source file in ASCII text. The file is saved with an ASM extension as an ASCII text file.
Invoke MASM to create an object file (OBJ) and using options, a list (LST) file.The assembler program reads the source file and produces an object file, a machine-language translation of the program. The object file may contain calls to subroutines in an external link library.
Invoke the linker. The linker then copies the needed subroutines from the link library into the object file, creates a special header record at the beginning of the program, and produces an executable program. If other OBJ files are linked, they are merged together. The OBJ files may be created with the assembler by assembling it separately, or with a high level language compiler.
The linker created an EXE or COM file, which may be executed from the command prompt by typing the name of the program. When we run the program by typing its name on the DOS command line, the DOS loader decodes the header record of the executable program and loads it into memory. The CPU begins executing the program.
The assembler produces an optional listing file (filename.lst) when we use the /Fl option. This is a copy of the program's source file (suitable for printing) with line numbers and translated machine code. If there are any errors or warnings, the assembler places an appropriate error message on the line after the instruction. It would look like this:
mov ax,bl
error.asm(10) : error A2070: invalid instruction operands
The reason for the error is we are trying to copy byte size data (8-bits of data from the BL register) into a word size register (the 16-bit AX register). Remember, the destination and source operands must have the same boundary (data) size.
The linker produces an optional map file, which contains information about the program's code, data, and stack segments. A link library is a file containing subroutines that are already compiled into machine language. Any procedures called by your program are copied into the executable program during the link step.
The primary file produced by the assembly step is hello.obj. The assembler distinguishes between error messages and warning messages. A program with warning messages will still assemble, but the object file may have errors. In general, it is best to fix such errors before linking the program.
Syntax Errors
We were fortunate to have not made any mistakes when writing this program. But if we had, the assembler would have displayed the line with the mistake, along with an explanation. For example, value! was incorrectly spelled as value!
Microsoft Assembler (MASM)
The Microsoft Assembler package contains the ML.EXE program, which assembles and links one or more assembly language source files, producing an object file (extension .obj) and an executable file (.exe). The basic syntax is:
ML options filename.asm
The switch or option is case sensitive while the rest of the statement is not. For example, the following command assembles and links hello.asm {with CodeView debugging information), producing hello.obj and hello.exe:
ml /Zi hello.asm
The following assembles and links hello.asm producing a listing file called hello.lst as well as hello.obj, hello.exe:
ml /Fl hello.asm
The following assembles, links, and produces a map file called hello.map during the link step. It still produces hello.obj and hello.exe:
ml /Fm hello.asm
You can use as many options in the command line as you wish.
With MASM 6.11, the ML command assemble and link a .ASM file This invokes both the assembler and linker in a single command line.
ml hello.asm
It produces a .OBJ file and a .EXE file
Option /Zi produces debugging info for CV
Option /Fl produces a source listing
Option /Fm produces a map file
To produce all of these, type (with spaces)
ml /Zi /Fl /Fm hello.asm
Examining hello.lst
The beginning of the CODE segment in the lst file looks like the following:
0000 .CODE
.startup
; Now we should load the Data Segment register
0017 B8 ---- R mov ax,@data
001A 8E D8 mov ds,ax
The R suffix associated with the address indicates that reference, in this case @DATA, is relocatable (resolved at loading time by the OS).
Examining hello.map
The map file is generated when we assemble a program with the /Fm command. The map file contains information about the beginning and end addresses and size of all the segments in our program. Hello/map looks like the following:
Start Stop Length Name Class
00000H 0002CH 0002DH _TEXT CODE
0002EH 0003CH 0000FH _DATA DATA
00040H 0203FH 02000H STACK STACK
Origin Group
0002:0 DGROUP
Address Publics by Name
Address Publics by Value
Program entry point at 0000:0000
With the .model small directive, MASM aligns the segments in memory in the following way:
the stack segment is align at the next available paragraph boundary (ie: at a physical address divisible by 10h)
the code and data segments are aligned at the next available word boundary (ie: at a physical address divisible by 2)
The (relative) starting physical address of the segments are found in the hello.map file
By default MASM only enables the assembly of the 8086 instructions. If we wish to use the more powerful 80386 instruction set (to move data as doublewords or use the registers for 32-bit operands) we need to enable these instructions by invoking the .386 directive. TO do this, place this directive just after .model directive like this:
.model small
.386
The offset address of the first data is generally not 0 if the data segment is loaded on a word boundary If the code part of hello.asm takes 11h bytes and starts at 30000h. The first data will start at 30012h and DS will contain 3001h.
.data
message db "Hello, world!",0dh,0ah,'$’
A program that accesses data or libraries that are defined in another file must use the EXTRN directive. A link library is a file containing assembled or complied procedures. elec242.lib contains procedures for performing I/O and string-to-number conversion. You will find this file on the hard drives in ATC 105, or you may download the file from the ELEC242 web site. For example, we wish to use a procedure defined in ELEC242.LIB, the program that calls that procedure must have the following statement in the .code section:
.code
extrn Readint:proc, Writeint_signed:proc
In the above line, the procedures:
Readint : reads a signed decimal string from the keyboard and stores the corresponding 16-bit signed number into AX
Writeint_signed : displays the signed decimal string representing the signed number in AX
To use these procedures, include the extern directive in an asm file claaled filename.asm, assemble the file using the command line
ml /Fl elec242.lib filename.asm
I have created a batch file called m.bat. The file contains the following command line:
ml /Fl elec242.lib %1.asm
To assemble and link your program, enter the command
m filename
where filename is the name of the file. Both m.bat and elec242.lib must be in the same directory as the asm files. You may use this command line even if we do not use any of the library functions.
Also make sure to modify the path statement in AUTOEXEC.BAT. This file is in the root directory on drive C: Open AUTOEXEC.BAT in a text editor. Move to the end of the PATH statement and add the following:
; c:\masm611\bin;
and reboot.
For externally defined variables we use either byte, word or dword as follows
extrn byteVar:byte, wordVar:word
Page 1