This document provides instructions for installing Visual C++ and MASM on a personal computer to write and run assembly language programs. It describes downloading Visual C++ 2008 Express Edition from Microsoft, which contains MASM 9.0. It also provides details on creating and running both C++ programs with inline assembly and standalone MASM assembly programs using Visual C++ 2008.
This document provides instructions for installing Visual C++ and MASM on a personal computer to write and run assembly language programs. It describes downloading Visual C++ 2008 Express Edition from Microsoft, which contains MASM 9.0. It also provides details on creating and running both C++ programs with inline assembly and standalone MASM assembly programs using Visual C++ 2008.
If your college, university, or company already has Visual C++ and MASM installed, then their procedures should be consulted and the information contained in this appendix can be ignored. However, if one needs to download Visual C++ and MASM on their own personal computer, the following will be useful. Although 2008 or the 2010 Visual C++ Express Edition can be used to run inline assembly language programs as demonstrated in Chapter 1, at the time of this writing only the 2008 version seems to run stand-alone assembly language programs and it is needed in all subsequent chapters of this text. As a result, what follows are the directions on how to install Microsoft Visual C++ 2008 Express Edition with SP1, where SP1 is the Visual Studio 2008 Service Pack 1 that contains MASM 9.0. A.1 Directions for Installing Visual C++and MASM 1. The software used in this text is Microsoft Visual C++ 2008 Express Edition with SP1, which can be downloaded from www.microsoft.com/express/Downloads 2. At the Downloads page, click the Visual Studio 2008 Express tab which is the last of the three tabs, then click the Visual C++ 2008 Express Edition link, and then click the Free Download button. 3. Note that during setup it may be necessary to click on the Visual Studio 2008 Service Pack 1 link, and then click on the Download button. If not, proceed to step 4. As always, be sure to examine the license terms. After the service pack has been downloaded, return back to the Visual C++ 2008 Express Edition setup. If it does not continue with the setup, it might be necessary to repeat steps 1 and 2 above prior to proceeding onto step 4 below. 4. As always, be sure to examine the license terms and note that the optional products are not required prior to continuing with the installation. 5. After the software is downloaded, there will be a message reminding one to register the evaluation copy within 30 days, where once one has entered the registration key, the product is free. To register, select Register Product under the Help menu of the Express Edition software itself. 209 J.T. Streib, Guide to Assembly Language, DOI 10.1007/978-0-85729-271-1, C Springer-Verlag London Limited 2011 210 Appendix A A.2 Writing C Programs and Inline Assembly As indicated in Chapter 1, if you are already familiar with Visual C++ and C/C++ pro- gramming, a simple way to explore MASM is to rst write C/C++ code and then use inline assembly. Again, the inline assembly works ne in both the 2008 and 2010 versions of Visual C++ Express Edition, but at the time of this writing, only the 2008 version seems to work with stand-alone assembly. Since stand-alone assembly will be used in Chapter 2 and all subsequent chapters, the directions that follow will only describe entering and running programs in Visual C++ 2008 Express Edition. After installing Visual C++, invoke Visual C++ by clicking on the appropriate icon or choosing it from the list of programs. Once started, then select from the pull-down menus: le, new, and then project. Then from the Project types section on the left, expand the Visual C++ entry and click on Win32. In the Templates section under Visual Studio Installed Templates, select Win32 Console Application. Next in the name: section at the bottom of the window, type in a name for the project and click the OK button. From the window that says Welcome to the Win32 Application Wizard, click the Next button. Then on the next page concerning Application Settings, make sure that in the Application Type: section, Console Application is selected and under the Additional Options: section, check Empty project and then click the Finish button. Back at the start page in the Solution Explorer window, which is on either the right or the left side, right click Source Files, click Add, and then New Item. Then in the Visual Studio installed templates, click on C++ le. Then in the Name: section, type the name of the le to hold the program and click the Add button. Prior to trying to run inline assembly, it is best to rst insure that a simple C program works properly. Key in the following the program which merely assigns the number 5 to the variable num1, copies the contents of num1 to num2, and then outputs the contents of num2: #include <stdio.h> int main(){ int num1,num2; num1 = 5; num2 = num1; printf("%s%d\n","The answer is: ",num2); return 0; } One might notice that the above program uses the C printf for output instead of the C++ cout for output. The reason for this is that it is fairly easy to use C input/output in stand-alone assembly language as will be seen in Chapter 2. It is of course wise to be sure to save the project to insure that the code is saved. To compile and run the program, click Build from the pull-down menu and select Build Solution. In order to run the program, click Debug from the pull-down menu and select Start Without Debugging or optionally one can press Ctrl-F5. Unfortunately the pull-down selection does not seem to have made it to Visual C++ 2010 Edition, but the Ctrl-F5 option did. Appendix A 211 In order to demonstrate the transfer of num1 to num2 in assembly language, the follow- ing code segment implements the same thing as the above code segment but instead uses inline assembly. This is accomplished by including the word __asm (which it should be noticed is preceded by a double underscore, not a single underscore). Then all the assem- bly codes are bracketed by opening and closing braces as shown below. To demonstrate how inline assembly works, alter the above code segment to replace the num2=num1; line of code with the inline assembly code below. Then run the program as explained previously: #include <stdio.h> int main(){ int num1,num2; num1 = 5; __asm { mov eax,num1 mov num2,eax } printf("%s%d\n","The answer is: ",num2); return 0; } The advantage of using inline assembly code is that it is very easy to use and one can take advantage of high-level input and output. The major disadvantage is that one cannot use high-level structures utilizing .if and .while directives in assembly lan- guage. Instead, one must use compare and jump statements as discussed in Chapters 4 and 5. A.3 Writing Stand-alone MASMPrograms As stated previously, at the time of this writing, the following seems to work only with Visual C++ 2008. If one wants to write a stand-alone MASM program, then one needs to make some careful changes to the properties section of the project and later the .asm le. Everything is done as outlined in the previous section, except after one creates a new project and is ready to create a source le, one must be certain to add a .asm extension to the source le name. This will cause the MASM assembler to be invoked instead of the C++ compiler. In other words, when back at the start page and in the Solution Explorer window, which is on either the right or the left side, right click Source Files, click Add, and then New Item. Then in the Visual Studio installed templates, click on C++ le. Then in the Name: section, type the name of the le to hold the program with a .asm extension and click the Add button. Although the following is a little cumbersome, once it is typed in the rst time, it can easily be copied and pasted in other projects. In the Solutions Explorer section on the right or the left part of the screen, right click the name of the project, which is usually bolded, 212 Appendix A and select Properties. Then in the window on the left, expand Conguration Properties, then expand Linker, and then click on Input. In the Additional Dependencies area on the right, key in the following: msvcrt.lib Then again in the Solutions Explorer section on the right or the left part of the screen, right-click on the name of the source program to be assembled and select Properties. Then in the window on the right, expand Conguration Properties, then expand Custom Build Step, then expand General. Then in the Command Line area on the right, key in the following: ml -c -Zi "-Fl$(IntDir)\$(InputName).lst" "-Fo$(IntDir)\$(InputName).obj" "$(InputPath)" Note that the ml, Fl, and .lst all contain the letter l and not the number 1. Then in the Outputs section two lines down, key in the following, click Apply, and then OK when nished: $(IntDir)\$(InputName).obj Then key in the sample program below and be sure that it is saved with a .asm extension, where this program is similar to both the previous C program and the C program with inline assembly. Although much of the assembly code below might not make a lot of sense when you are rst installing MASM, it is a good program to check to make sure that MASM has been installed properly. After reading Chapters 1 and 2, most of the code below should make sense: .386 .model flat, c .stack 100h printf PROTO arg1:Ptr Byte, printlist:VARARG .data msg1fmt byte "%s%d",0Ah,0 msg1 byte "The answer is: ",0 num1 sdword ? num2 sdword ? .code main proc mov num1,5 mov eax,num1 mov num2,eax INVOKE printf, ADDR msg1fmt, ADDR msg1, num2 ret main endp end Appendix A 213 To assemble and run the program, as before click Build from the pull-down menu and select Build Solution. In order to run the program, click Debug from the pull-down menu and select Start Without Debugging or optionally one can press Ctrl-F5. One of the les generated by the build process that will be useful in various chap- ters throughout the text is the .lst le. This can be opened by rst clicking le on the pull-down menu, then clicking Open, then le. . ., then in the Object name area, type
.lst, then click on the Debug subdirectory, and then click on the le that has the same name as the .asm le. This le contains not only the macro expansions but also the machine lan- guage as discussed in Chapters 7 and 10, respectively. If one includes the .listall directive at the beginning of the program, the code generated by high-level directives can also be revealed as discussed in Chapter 4. (Note: Be very careful to return back to the .asm le before making any changes to the program, because only changes to the .asm program will be reassembled and subsequently executed.) A.4 Summary If one wants to try only a few of the features of MASM, the simplest way is to just install Visual C++ and use inline assembly as used in Chapter 1. However, the disadvantage of inline assembly is that features such as high-level directives cannot be used. Again note that inline assembly is used only in Chapter 1. If one wants to use all the features of MASM in a stand-alone program, be sure to include the changes listed in Section A.3 and save the source program with a .asm extension. Again note that stand-alone assembly is the only method used in all chapters from Chapter 2 onward.