Using Masm
by Jacqueline A. Jones
Installing the Software from the Book
Create a new folder called Masm614 on your C: drive and copy all the files and folders from the CD to that folder.
Put the Program in the Correct Folder
To run any of the programs from the text, you must copy them into the folder C:\Masm614\software, which is where ml.exe and link.exe are stored. When you write your programs, you will save them in C:\Masm614\software.
Get the I/O Files from my Web Site
On my CIS 3310 Web site, I have posted two files from Detmer's other assembler book (we are using them with his permission). Copy these files into the Masm614\code folder.
Installing the Software from my Website
Go to the CISC 3310 Website and then to the Assembler Materials page. Download Software.zip. Extract the files to a folder which you can call Masm or whatever you want. There will be no need for subfolders. If you do this, substitute the name of your folder in place of Masm614\software in the lines below.
Decide on an Editor
Normally, you write assembler using any text editor you want, but I am asking you to use Notepad because if we all use the same editor, the columns will be aligned correctly.
Open a Command Prompt
Once you have copied a program to the folder (or written and saved a program with a .asm extension), open a command prompt. (See instructions below to create a custom command prompt.) To open the command prompt:
In Windows XP:
Select Start, then Run, then type "cmd" (without quotes) in the box and click OK.
In Vista or Windows 7:
Click Start. In the Search field, type "cmd" (without quotes) or go to
Start | All Programs| Accessories, and click on the "Command Prompt" icon.
Each of these will open a Command window which will show a prompt, but the prompt is pointing to the wrong folder for our purposes. On my machine, the prompts is this: C:\Documents and Settings\Jackie>, but yours will be different.
Change to the Correct Directory (Folder)
At the prompt, change to the appropriate folder by typing cd (which means Change Directory) followed by a space, backslash, and the full pathname to the directory you want, and press <Enter>. To get to the Masm614 folder, software subfolder, type this:
cd \Masm614\software
This will give this new prompt:
C:\Masm614\software>
Changing to the new directory can be also done in two steps. First type
cd \
which takes you to the C: prompt, and then type
cd Masm614\Software
Assembling, Linking, and Running the Program to Do Console I/O
Note that these lines do NOT include the switches (/Zi in the ml step and /debug in the link step) to run the program in the debugger. If you intend to use the debugger, use the instructions in the last section of this document.
Insert an extra line in the program to include io.h (lines below show where the line goes).
ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD
Include io.h ; io.h is header file for input/output
To assemble a program called prog1.asm), type the following (this line omits the switches used for the debugger:
ml /c /coff /Fl prog1.asm ; (that switch is F-ell, not F-one)
If this is successful, there will be no error messages. Two files will be created: an object file called prog1.obj and a listing file prog1.lst.
Next type the following to link the object code prog1.obj with object libraries (including io.obj) to produce the executable file prog1.exe. (This line, when typed, will run over onto a second line.)
link /subsystem:console /entry:start /out:prog1.exe prog1.obj io.obj kernel32.lib
To run the program, type its name at the prompt (or find the prog1.exe icon and click on it):
prog1
Saving Your Output to a File
To save the output to a file which you can attach to an email or print, do the following:
a. Right-click in the top bar of the output window and choose "Edit | Select All".
b. Then right click in the bar again and choose "Edit | Copy".
c. Open Notepad (right click on the desktop and select "New | Text document").
d. In the new window, right-click and select "Paste".
e. Then select "File | Save as" to save and name the file.
i. In the box labeled “File name,” type the name of the file. It will automatically be given the
extension .txt (for example, prog1txt).
ii. Click “Save”.
Using Arrow Keys
When you are at the command prompt, you can use the arrow keys to edit the command line (use the left arrow to go back without deleting). More important, you can use the up and down arrow keys to review and retrieve and edit commands you have already used. You can also paste into the cmd window. Press Shift and an options menu will appear that allows pasting.
Creating a Custom Command Prompt
Going to the command prompt and switching to the correct folder can waste a lot of time. You can create your own command prompt that will open right in the folder you want to use. To create your own command prompt, do this:
a. Click Start | All programs | Accessories
b. In the Accessories folder, find Command Prompt.
c. Right click on Command Prompt and select Copy
d. Paste it to the desktop
e. Next, right click on the new icon and and select Properties.
f. In the Shortcut tab, change the "Start in: " field. Set it to the path of the software folder that you copied from the CD. (The folder is C:\Detmer\software if you followed the instructions above. You can also set it to whatever you called the folder.)
g. Click OK.
Now, whenever you want to use ml and link, all you have to do is click on the newly created command prompt.
Assembling, Linking, and Running the Program in the Debugger
Note that these lines do NOT include io.obj; if you intend to print or read in values, you must use the instructions in the previous section or add io.obj to the list of object files.
At the prompt, type this to assemble a file called prog2.asm:
ml /c /coff /Fl /Zi prog2.asm //assembles (that switch is F-ell, not F-one)
If this is successful, there will be no error messages. Two files will be created: an object file called prog2.obj and a listing file prog2.lst.
Next type the following to link the object code prog2.obj with object libraries to produce the executable file prog2.exe. (This line, when typed, will run over onto a second line.)
link /debug /subsystem:console /entry:start /out:prog2.exe prog2.obj kernel32.lib
Run Using the Windows Debugger
Open the Windows Debugger by typing this:
windbg
Now prepare to run the program. Open an executable file (File | Open executable), then select prog2.exe.
Prepare to trace the program by clicking "Trace Into," which is an icon like this: { –> } (curly braces with an arrow pointing BETWEEN them)
You will see a box with "No symbolic information for Debugger"; click OK and your program's source code listing file will appear behind a white command window. Close or move the white command window to view the source code.
Next open a window that shows the registers (View | registers). Make it smaller and move it to the right side of the source code. Then open a window that shows memory (View | memory). In prog2, the first variable is called number, so type &number in the "Address Expression" box. The window will open with the view of memory starting at the variable number. Make the window smaller, leaving the top few lines visible, and move it below the source code.
Execute your program one step at a time by clicking the "Trace Into" icon. Each time you click the icon, the line highlighted in yellow will execute. Watch as register eax changes with the first two instructions, and as sum in memory (which follows number) changes with the third instruction. When you have executed the last instruction in the program, you will not be able to click the icon again.
Use PrintScreen to copy the window. Open a window in a program like Word, paste the image into the window, and print it.