A Word About the Visual C Inline Assembler

[Previous] [Next]

Before I jump into the assembly-language instructions, I want to talk for a bit about the inline assembler in Visual C++. Like most professional C++ compilers, the Visual C++ compiler allows you to embed assembly-language instructions directly in line with your C and C++ code. Although using inline assembly language generally isn't recommended because it restricts your code's portability, it's sometimes the only way to accomplish a task. In Chapters 12 and 14, I'll show you how to hook imported functions by using inline assembly language.

At the beginning of the chapter, I said that you don't need to know how to write your programs in assembly language, and I'm not contradicting myself. Learning to use the inline assembler isn't the same as learning to write an entire program in MASM—your C/C++ program still provides the application infrastructure. You can think of the inline assembler as the programming equivalent of a Zoom feature. When you create a bitmap, for example, you start out by painting with broad strokes; when it comes time to put on the finishing touches, you zoom in so that you can control the individual pixels. In the same way, the inline assembler lets you "paint" your program in broad C/C++ strokes but allows you to zoom in when you need to control the individual assembly-language instructions. I want to show you how to use the inline assembler because inline assembly language will make the examples easier to understand. Additionally, you can use the inline assembler to play around with the instructions I show you in this chapter so that you can see how they behave.

To show you the format for inline assembly language, I'll need to introduce your first instruction:

NOP No operation

NOP is the instruction that does nothing. The compiler sometimes uses NOP for padding inside functions to keep functions aligned on proper memory reference boundaries.

The inline assembler keyword is __asm. After __asm, you enter the assembly-language instruction you want to execute. If you want to enter multiple instructions, use __asm and enclose within braces as many assembly-language instructions as you'd like. The following two routines show you the format of inline assembly-language instructions. These routines are functionally equivalent.

void NOPFuncOne ( void ) { __asm NOP __asm NOP } void NOPFuncTwo ( void ) { __asm { NOP NOP } }

Throughout the chapter, I'll use the inline assembler to illustrate assembly-language operations, such as parameter and variable access. If you want to see how each instruction operates, open the ASMer sample program on the companion CD. This sample program contains all the assembly-language examples that follow.



Debugging Applications
Debugging Applications for MicrosoftВ® .NET and Microsoft WindowsВ® (Pro-Developer)
ISBN: 0735615365
EAN: 2147483647
Year: 2000
Pages: 122
Authors: John Robbins

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net