Privatization of Education in Pakistan
Getting Started with MASM | | |Updated 3/15/2008 | |If you’ve recently purchased Assembly Language for Intel-Based Computers, 5th edition, you probably want to get the software set | |up so you can start working. This tutorial should make the process easier. If you’re in a hurry to get started, you only need to | |read Item 1. |Required setup for 32-bit applications | |Building 16-bit applications (Chapters 12-16) | |Project properties settings | |Creating a project from scratch | |Generating a source listing file | |Using the Visual Studio 2005 debugger | |MASM syntax highlighting | |Assembling, linking, and debugging with a batch file | |Found an error in this document? Please email me immediately. Except where noted, all instructions in this document apply equally| |to Visual Studio 2005 and Visual C++ 2005 Express. | |[pic] | |Required Setup for 32-bit Applications | |If you are using Visual Studio 2005 Professional or Team Suite, these products already contain the Microsoft Assembler 8. 0. You | |can skip Steps 1 and 2 and go directly to Step 3. |Step 1: Install Visual C++ 2005 Express Edition | |You may have received a CDROM accompanying this book depending on which edition your bookstore ordered. The CDROM contains Visual| |C++ 2005 Express Edition. You will use it to write, edit, compile, debug, and run your assembly language programs. If you did not| |receive a CDROM, download the software now from Microsoft. | |Note: Microsoft has indicated an interest in bundling MASM with Visual C++ 2008 Express. We hope that will happen soon! | |When you run the Visual C++ setup program, make a note of the location where the C++ compiler is installed. This information will| |be useful to you later. | |From now on, we will leave the “2005” out of the name Visual C++ Express. |Step 2: Download and Install the Microsoft Assembler | |Visit Microsoft’s MASM 8. 0 download site. | |Follow the download and installation instructions on the Microsoft page. If the link is broken, please let us know by email. | |Step 3: Installing the Book’s Example Programs | |Click this link to get the latest copy of the book’s link libraries and example programs. The examples are stored in a | |self-extracting archive file that automatically extracts to the c:Irvine folder.
Unless you have some objection to using that | |location, do not alter the path. (Lab managers: you can designate c:Irvine directory as read-only. ) If you plan to change the | |installation location, read our instructions relating to changing project properties. | |The folllowing files will be copied into the c:Irvine directory: | |Filename | |Description | | | |GraphWin. nc | |Include file for writing Windows applications | | | |Irvine16. inc | |Include file used with the Irvine16 link library (16-bit applications) | | | |Irvine16. lib | |16-bit link function library used with this book | | | |Irvine32. nc | |Include file used with the Irvine32 link library (32-bit applications) | | | |Link16. exe | |16-bit linker | | | |Irvine32. lib | |32-bit link function library used with this book | | | |Macros. nc | |Include file containing macros (explained in Chapter 10) | | | |SmallWin. inc | |Small-sized include file, used by Irvine32. inc | | | |User32. lib | |Link library with Windows functions. | | | |make16. at | |Batch file for building 16-bit applications | | | |VirtualKeys. inc | |Keyboard code definitions file, used by Irvine32. inc | | | |A subdirectory named Examples will contain all the example programs shown in the book. | |Step 4: Building a Sample Assembly Language Program | |Preliminary Step: Set Tab Size to 5 | |Start Visual C++ Express, and select Options from the Tools menu.
Select Text Editor, Select All Languages, and select Tabs: | |[pic] | |Set the Tab Size and Indent Size to 5. | |Opening a Project | |Visual Studio and Visual C++ Express require assembly language source files to belong to a project, which is a kind of container. | |A project holds configuration information such as the locations of the assembler, linker, and required libraries. A project has | |its own folder, and it holds the names and locations of all files belonging to it. We have created a sample project folder in the| |c:IrvineExamples directory, and its name is Project_Sample. |Do the following steps, in order: | |Start Visual Studio or Visual C++ Express. | |If you’re using Visual Studio, select Open Project from the File menu. Or, if you’re using Visual C++ Express, select Open, and | |select Project/Solution. | |Navigate to the c:IrvineExamplesProject_Sample folder and open the file named Project. sln. | |In the Solution Explorer window, click the + symbol next to the item named Project to expand it. Double-click the file named | |main. asm to open it in the editing window. (Visual Studio users may see a popup dialog asking for the encoding method used in the| |asm file. just click the OK button to continue. | |Tip: If the Solution Explorer window is not visible, select Solution Explorer from the View menu. Also, if you do not see | |main. asm in the Solution Explorer window, look at the tabs along the bottom of the window. Click the Solution Explorer tab. | | | |You should see the following program in the editor window: | |TITLE MASM Template (main. asm) | | |; Description: | |; | |; Revision date: | | | |INCLUDE Irvine32. inc | | | |. data | |myMessage BYTE “MASM program example”,0dh,0ah,0 | | | |. ode | |main PROC | |call Clrscr | | | |mov edx,OFFSET myMessage | |call WriteString | | | |exit | |main ENDP | | | |END main | | | |Later, we’ll show you how to copy this program and use it as a starting point to write your own programs. | |Build the Program | |Next, you will build (assemble and link) the sample program: | |If you’re using Visual C++ Express, select Build Solution from the Build menu. |If you’re using Visual Studio, select Build Project from the Build menu. | |In the output window at the bottom of the screen, you should see messages similar to the following, indicating the build | |progress: | |1>—— Build started: Project: Project, Configuration: Debug Win32 —— | |1>Assembling… | |1>Assembling: . main. asm | |1>Linking… | |1>Embedding manifest… | |1>Build log was saved at “file://g:masmProject_sampleDebugBuildLog. tm” | |1>Project – 0 error(s), 0 warning(s) | |========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ========== | | | |If you do not see these messages, the project has probably not been modified since it was last built. No problem–just add a | |space somewhere in the document, save it, and try the Build command again. | |Run the Program | |Select Start without Debugging from the Debug menu.
The following console window should appear, although your window will be | |larger than the one shown here: | |[pic] | |The “Press any key to continue… ” message is automatically generated by Visual C++ Express. | |Congratulations, you have just run your first Assembly Language program. | |Press any key to close the Console window. | |When you assembled and linked the project, a file named Project. exe was created inside the project’s Debug folder. This is the | |file that executes when you run the project. You can execute Project. xe by double-clicking its name inside Windows Explorer, but| |it will just flash on the screen and disappear. That is because Windows Explorer does not pause the display before closing the | |command window. | | | |Creating New Projects of Your Own | |Before long, you will want to create your own projects. The easiest way to do this is to copy the entire | |c:IrvineExamplesProject_Sample folder to a new location. Copy it to a folder in which you have read/write permissions. (If | |you’re working in a college computer lab, a useful location is a portable USB drive. Then you can modify the program, build, and | |run it again. |Step 5: Running the Sample Program in Debug Mode | |In this step, you will set a breakpoint inside the sample program. Then you will use the Visual C++ debugger to step through the | |program’s execution one statement at a time. | |To begin stepping through your program in Debug mode, press the F10 key. | |A yellow arrow will appear next to the first program statement (call Clrscr). The arrow indicates that the statement is next to be| |executed. | |Press the F10 key (called Step Over) to execute the current statement. Continue pressing F10 until the program is about to | |execute the exit statement. | |A small black window icon should appear on your Windows status bar.
Open it and look at the contents of the Command window. You | |should see the words “MASM program example” in the window. | |Press F10 one more time to end the program. | |[pic] | |Registers | |If you want to display the CPU registers, do the following: Start debugging the program, then select Windows from the Debug menu. | |Select Registers from the drop-down list. The bottom window will display the register contents. Right click this window and check| |the item Flags to enable the display of conditional flags. |You can interrupt a debugging session at any time by selecting Stop Debugging from the Debug menu. You can do the same by | |clicking the blue square button on the toolbar. To remove a breakpoint from the program, click on the red dot so that it | |disappears. | |Setting a BreakPoint | |If you set a breakpoint in a program, you can use the debugger to execute the program a full speed (more or less) until it | |reaches the breakpoint. At that point, the debugger drops into single-step mode. | |Click the mouse along the border to the left of the call WriteString statement. A large red dot should appear in the margin. | |Select Start Debugging from the Debug menu.
The program should run, and pause on the line with the breakpoint, showing the same | |Yellow arrow as before. | |Press F10 until the program finishes. | |You can remove a breakpoint by clicking its red dot with the mouse. Take a few minutes to experiment with the Debug menu | |commands. Set more breakpoints and run the program again. For the time being, you can use the F11 key to step through the program| |in the same way the F10 key did. | |Building and Running Other Programs | |Suppose you want to run another example program, or possibly create your own program.
You can either edit and modify main. asm, or| |you can remove main. asm from the project and insert some other . asm file into the project. | |To remove a program from a project without deleting the file, right-click its name in the Solution Explorer window. In the | |context menu, select Exclude from Project. If you change your mind and decide to add it back to the project, right-click in the | |same window, select Add, select Existing item, and select the file you want to add. | |To remove a program from a project and delete the source code file, select the file with the mouse and press the Del key. Or, you| |can right-click the file name and select Remove. |Adding a File to a Project | |The easiest way to add an assembly language source file to an open project is to drag its filename with the mouse from a Windows | |Explorer window onto the name of your project in the Solution Explorer window. A reference to the file (not a copy) will be | |inserted in your project’s directory. Try this now: | |Remove the main. asm file from your project. | |Add a reference to the file c:IrvineExamplesch03AddSub. asm to the project. | |Build and run the project. |Here is what you should see in the Console window, except that only your EAX register will have the same value as ours: | |[pic] | |When you press a key, the console window will close. | |Copying a source file | |If you want to make a copy of an existing file, use Windows Explorer to copy the file into your project directory. Then, | |right-click the project name in Solution Explorer, select Add, select Existing Item, and select the filename. | |Return to top or read about Project Properties settings. | | | | | | | | | | |[pic] | |Building 16-bit Applications (Chapters 12-16) | |Only Chapters 12 through 16 require the building of 16-bit applications. Except for a few exceptions, which are noted in the | |book, your 16-bit applications will run under Windows XP and Windows Vista. | |If you plan to build 16-bit applications, you need to add two new commands to the Tools menu in Visual C++ Express (or Visual | |Studio). To add a command, select External Tools from the Tools menu.
The following dialog will appear, although many of the | |items in your list on the left side will be missing: | |[pic] | | | |Step 1: Create the Build 16-bit ASM Command | |Click the Add button and fill in the Title, Command, Arguments, and Initial directory fields as shown in the screen snapshot. If | |you click the buttons with arrows on the right side of the Arguments and Initial directory fields, a convenient list appears. You| |can select an item without having to worry about spelling: | |[pic] | |Click the Apply button to save the command. | | |Step 2: Create the Run 16-bit ASM Command | |Click the Add button again, and create a new command named Run 16-bit ASM: | |[pic] | |Click the OK button to save the command and close the External Tools dialog. | |Testing Your new 16-Bit Commands | |To test your new 16-bit commands, open the file named 16-bit. asm from the ch03 folder in the book’s example programs. Select | |Build 16-bit ASM from the Tools menu.
The following command window should appear, showing the successful execution of the | |assembler and linker, followed by a listing of all files related to this program: | |[pic] | |Press a key to close the window. Next, you will run the program. Select Run 16-bit ASM from the Tools menu. The following window | |will appear, although the contents of all registers except EAX will be different: | |[pic] | |Press a key to close the window. | |You have completed the setup for building and running 16-bit assembly language programs. |Return to top | | | | | |Project Properties Settings (Optional Topic) | |We thought you might like to know more about how Visual C++ projects are set up for assembly language programs. If so, we will | |walk you through the important parts of the project configuration. | |Assuming that our sample project is still open, select Project Properties from the Project menu. Expand the entry under | |Configuration Properties. Then expand the entry named Microsoft Macro Assembler 8. 0.
This is what you should see: | |[pic] | |Click the entry named General under Microsoft Macro Assembler 8. 0. Notice that the Include Paths option has been set to the | |c:Irvine directory. This tells the assembler where to find files having a filename extension of “. inc”. Here is a sample: | |[pic] | |Find the Linker entry under Configuration Properties. Select the Input entry, and notice that two filenames have been added to | |the Additional Dependencies entry. The user32. ib file is a standard MS-Windows file. The irvine32. lib file is the link library | |file supplied with this book. There must be at least one space separating the file names: | |[pic] | |Next, select Linker under Configuration Properties, and then select General. The Additional Library Directories option equals | |c:Irvine, so the linker can find the Irvine32. lib library file: | |[pic] | |Select Linker under the Configuration Properties and select Debugging.
Notice that the Generate Debug Info option is set to Yes: | |[pic] | |Select System under the Linker entry. Notice that the SubSystem option has been set to Console: | |[pic] | |We use the Console setting because it’s easy for assembly language programs write output to a text console (Command) window. This| |is the window you see when running cmd. exe from the Start > Run menu in Windows. | |Click the OK button to close the Project Property Pages window. |Return to top | | | | | | | |[pic] | |Generating a Source Listing File | |Prior to 7/26/06, the sample Visual Express projects in the book’s download file do not generate source listing files. Here’s how| |to change that behavior in a single project: | |Open the project. From the menu, select Project, select Project Properties. In the list box, select Microsoft Macro Assembler, | |then select Listing File. Set the Assembled Code Listing file option to $(InputName). lst . |Return to top | | | | | | | |[pic] | |Creating a Project from Scratch | |You do not have to create your own projects completely by yourself. Quite frankly, it’s a lot of work. We’ve placed a copy of the| |Project_sample project in each folder of the book’s example programs. You can just add your own program to one of these projects. | |But here’s how to create your own: | |You can name a project anything you want, of course, but we will assume your project is named MyProject in the following | |examples, and that you will save it in the c: emp directory.
The commands are a little different, depending on which software | |you use: | |Visual C++ Express | |1. Select New from the File menu, and select Project. | |2. In the New Project window, select General, and select Empty Project as the project type: | |[pic] | |You probably will want to leave the Create directory for solution option unchecked. | |3. Click the OK button to create the empty project. | |Visual Studio | |Select New Project from the File menu. |In the New Project dialog (shown in the image below), select Other Languages, select Visual C++, select General, and select Empty| |Project. | |Give a name to the project and select the location. Do not change any other options. | |Click the OK button. | |[pic] | |You probably will want to leave the Create directory for solution option unchecked. | |Both Visual Studio and Visual C++ Express | |Use Windows Explorer or My Computer to copy the file main. asm from the examplesProject_sample folder into the project folder | |you must created. (In our example, the folder is named MyProject). |Back in Visual Studio or Visual C++ Express, right click your project name in the Solution Explorer window. Select Add, select | |Existing Item, and select main. asm. (The file may not show up until you input *. asm in the filename box. ) Click the Add button to| |add it to your project. | |Select Custom Build Rules from the Project menu. You will see a list of Rule files, which will vary depending on which software | |you are using. Place a check next to Microsoft Macro Assembler: | |[pic] | |Click the OK button to close this window. | |7. Next, you need to add some customizations.
We will assume you installed the book’s files in the c:Irvine directory. Make all| |changes shown in the Project Properties Settings section of this document. If you installed the book’s sample programs in some | |other location than c:Irvine, you’ll need to make appropriate changes to the project properties. | |9. Select Build Solution. If your Output window is similar to the following message, you did everything right: | |1;—— Build started: Project: MyProject, Configuration: Debug Win32 | |1>Linking… | |1>Embedding manifest… | |1>Build log was saved at “file://c: empMyProjectDebugBuildLog. tm” | |1>MyProject – 0 error(s), 0 warning(s) | |========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped | |Return to top | | | | | | | |[pic] | |MASM syntax highlighting | |When a text editor uses syntax highlighting, language keywords, strings, and other elements appear in different colors.
Visual | |Studio and Visual C++ Express can highlight MASM reserved words and strings, as shown in the following example: | |[pic] | |This won’t happen automatically, but you can create a syntax definition file named Usertype. dat that contains MASM keywords. Then| |when Visual Studio (or Visual C++ Express) starts, it reads the syntax file and highlights MASM keywords. | |Here are the required steps to set up MASM syntax highlighting in Visual Studio or Visual C++ Express: | |1) Download the Usertype. dat file given here to a folder in which you have read/write permissions. If you are using Windows | |Vista, download to My Documents, or C: emp, or any folder that doesn’t have security restrictions. | |2) Copy Usertype. at to the C:Program FilesMicrosoft Visual Studio 8Common7IDE folder. If you are using Windows Vista, it | |will display a verification window before copying the file. | |3) Open Visual Studio or Visual C++ Express, select Options from the Tools menu, select Text Editor, and select File Extension. | |On the right side of the dialog (shown below), enter asm as the extension, select Microsoft Visual C++ from the Editor list, and | |click the Add button. Click the OK button to save your changes. | |[pic] | |Close Visual Studio and restart it. Open your project and display an ASM file. You should see syntax highlighting in the editor. |Return to top | | | | | | | | | | | | | |[pic] | |Assembling, Linking, and Debugging with a Batch File | |(This topic was updated 3/15/2008) | |Many people like to use a Windows batch file to assemble and link programs. A batch file is a text file containing a sequence of | |commands that execute as if they had been typed at the command prompt. In fact, they are powerful enough to contain variables, | |loops, IF statements, and so on. | |The easiest way to run a batch file is to first open a Command window and then type the name of the batch file (along with | |arguments) at the command prompt. To open a Command window, you must execute a program named cmd. exe. We will make that easy for | |you. | |Right-click here to download a zip file containing the following items: | |A shortcut to cmd. xe, which opens a Command window in the current directory | |asm32. bat, a batch file for assembling and linking programs | |main. asm, a sample assembly language program | |Extract this ZIP file into the c:IrvineExamples directory on your computer. Then do the following: | |Copy asm32. bat to any directory on your system path. If you’re using Windows XP, you can save it in the c:Windows folder. (If | |you’re using Windows 2000, save it in the c:Winnt folder. ) Having asm32. at on your system path permits asm32 to be a valid | |command whenever you type its name at the command prompt. | |Double-click the cmd. exe shortcut. A Command window should appear. | |At the command prompt in this window, execute the asm32 batch file and display its help information by typing the following | |command and pressing Enter: | |asm32 | |This file assembles, links, and debugs a single assembly language | |source file.
Before using it, install Visual Studio 2005 in the following | |directory: | | | |C:Program FilesMicrosoft Visual Studio 8 | | | | | |Next, install the Irvine 5th edition link libraires and include | |files in the following directory: C:Irvine | | | | | |Finally, copy this batch file to a location on your system path. |We recommend the following directory: | | | | | |C:Program FilesMicrosoft Visual Studio 8VCin | | | | | |Command-line syntax: | | | |asm32 [/H | /h | -H | -h] — display this help information | | | |asm32 filelist — assemble and link all files | |asm32 /D filelist — assemble, link, and debug | |asm32 /C filelist — assemble only | | | | is a list of up to 5 filenames (without extensions), | |separated by spaces. The filenames are assumed to refer to files | |having . asm extensions. Command-line switches are case-sensitive. | | | |Type the following command to assemble and link a source file named main. asm: | |asm32 main | |You should see he following messages: | |Assembling: main. asm | |The file main. obj was produced. | |……………………………. | |Linking main. obj to the Irvine32, Kernel32, and User32 libraries. | |The file main. exe was produced. | |……………………………. | | | |In fact, several files were produced. | |main. obj – the object file | |main. ilk – incremental link status file | |main. db – debug symbol file | |If there were errors in the program, you would see error messages generated by the assembler. Here is an example: | |Assembling: main. asm | |main. asm(9) : error A2008: syntax error : myMessage | |main. asm(15) : error A2006: undefined symbol : myMessage | |You would then open the main. asm file with a text editor (such as Notepad), fix the errors, and run the asm32 batch file again. |Although we used a file named main. asm in this example, the asm32. bat batch file will work for any assembly language file, | |regardless of the name. The only requirement is that your assembly language source file have a . asm filename extension. | | | |Assembling Programs in Other Directories | |No doubt, you will want to assemble programs in various different disk folders, not just the batch_sample folder used in the | |foregoing example. All you need to do is copy the cmd. exe shortcut we gave you to your working directory, where your assembly | |language source files are located.
When you double-click to run the shortcut, it will open a Command window in the current | |folder. | |Assembling, Linking, and Debugging | |In addition to assembling and linking, you can use the asm32. bat file to launch your program in the Visual Studio debugger. Try | |the following command: | |asm32 /D main | |If the program assembles and links with no errors, your program should load in Visual Studio. The first time you do this with a | |new program, the source code will not appear.
All you have to do is press the F10 key to begin debugging, and your program should| |appear with a yellow band across the first executable line: | |[pic] | |(Depending on how Visual Studio is configured, you might have to press F8 to do the same thing. ) | |From here, you can step through the program. When you get to the call to WriteString, you can even trace into its code by | |pressing the F11 key (trace to). When you finish, close Visual Studio. | |From this time on, when you load the same program in the Visual Studio debugger, your source code will appear right away. | |Assembling without Linking | |Occasionally, you may want to assemble programs but not link them.
This happens, for example, when you are creating a multimodule| |project and you want to assemble each asm file into an obj file separately before linking them into the final exe program. Or, | |you might be assembling a module to be inserted into a link library (like Irvine32. lib). | |To assemble a source file only, inser the /C option before the name of the file being assembled: | |asm32 /C main | |You should see the following output: | |Assembling: main. asm | |The file main. obj was produced. |……………………………. | |If you are interested in learning more about how batch file commands work, here are some reference links we found: | |Allenware. com: Batch file tutorial | |Microsoft TechNet article: Creating Truly Powerful Batch Files, by Brien Posey | |Microsoft TechNet article: Using Batch Files in Windows NT, by Troy Thompson | |Links go out of date quickly, but you can google for Windows batch files and get plenty of hits. | | |Return to top | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |