|
Tools BCC CHMOD CL COFF2OMF COFFIMPLIB DMC DIFF DIFFDIR DUMP DUMPOBJ DUMPEXE EXE2BIN FLPYIMG GREP HC IMPLIB LIB LIBUNRES MAKE MAKEDEP ME OBJ2ASM PATCHOBJ RC RCC SC SHELL SMAKE TOUCH UNMANGLE WHEREIS Compiling Compiling Code C Implementation C++ Implementation Language Extensions Mixing Languages Assembly Language Inline Assembler Optimizing Code Numerics Programming Regular Expressions Acrtused Pragmas Precompiled Headers Predefined Macros Warning Messages Error Messages Runtime Messages Linking Optlink Switches Module Definition Files Operation and Design Error Messages Win32 Programming Win32 Programming DOS and Win16 Programming Memory Models 16 Bit Pointer Types and Type Modifiers Handle Pointers DOS DOS 32 (DOSX) Win16 Win16 DLLs Win16 Prolog/Epilog Virtual Memory For 640Kb DOS C/C++ Extensions Contract Programming __debug statement __debug declaration Dynamic Profiling Embedding C in HTML Porting to DMC++ Switching to DMC++ from Microsoft from Borland Porting Guide |
Inline AssemblerThe compiler includes a powerful inline assembler. With it, assembly language instructions can be used directly in C and C++ source programs without requiring a separate assembler program. Assembly language enables optimizing critical functions, interfacing to the BIOS, operating system and special hardware, and access capabilities of the processor that are not available from C++.It supports both 16-bit and 32-bit code generation in all memory models. What's in This Chapter
Advantages of writing inline assembly language functionsUse assembly language functions to:
The asm StatementThe asm statement invokes the assembler. Use this statement wherever a C or C++ statement is legal. You can use asm in any of three ways.The first example shows asm followed simply by an assembly instruction: asm mov AH,2 asm mov DL,7 asm int 21HThe second example shows asm followed by a set of assembly instructions enclosed by braces. An empty set of braces may follow the directive.
asm {
mov AH,2
mov DL,7
int 21H
}
Because the asm statement is a statement separator, assembly
instructions can appear on the same line:
asm mov AH,2 asm mov DL,7 asm int 21HThe three previous examples generate identical code. But enclosing assembly language in braces, as in the second example, has the advantage of setting the assembly language off from the surrounding C++ code and avoids repeating the asm statement. No assembler instruction can continue onto a second line. Use the form of the last example primarily for writing macros, which must be one line long after expansion. NoteThe Digital Mars C++ asm statement emulates the Borland asm statement. The _asm and __asm statements emulate the Microsoft _asm and __asm statements.The ASM BlockA series of assembler instructions enclosed by braces following the asm keyword are called an "ASM block." Unlike C++ blocks, ASM blocks do not affect the scope of variables.Restrictions on using C and C++ in an ASM blockAn ASM block can use the following C and C++ language elements:
NoteMicrosoft and Borland inline assemblers do not support type casts.Inline assembly instructions within C or C++ statements can refer to C or C++ variables by name. MASM-Style hexadecimal constantsSupport for MASM-style hexadecimal constants provides easy conversion of MASM-style source code. The constants take the form:
digit {hex_digit} ('H'| 'h')
You cannot use hexadecimal constants if the -A (for
ANSI compatibility) option is used.
C and C++ operators in an ASM blockAn ASM block cannot use operators specific to C and C++, such as the left-shift (<<) operator.You can use operators common to C, C++, and MASM within an ASM block but interpret them as assembly language operators. C and C++ interpret brackets ([]) as enclosing array subscripts and scale them to the size of an array element. But within an asm statement, C and C++ interpret brackets as the MASM index operator, which adds an unscaled byte offset to any adjacent operand. In Microsoft-compatible mode, the semicolon delimits comments, as in MASM. Assembly language in an asm statementIn common with other assemblers, the inline assembler accepts any instruction that is legal in MASM. The following are some of the assembly language features of the asm statement:
Other restrictions on C and C++ symbolsAn asm statement can reference any C or C++ variable name, function, or label in scope, provided those names are not symbolic constants. However, you cannot call a C++ member function from within an asm statement.Prototype the functions referenced in an asm statement before using them in programs. This lets the compiler distinguish them from names and labels. Each assembly language instruction can contain a single C or C++ symbol. C or C++ symbols within an ASM block must not have the same spelling as an asm reserved word. The inline assembler allows structure or union tags in asm statements, but only as qualifiers of references to members of the structure or union. Accessing C or C++ data in an asm statementIn general, instructions in an asm statement can reference any symbol in scope where the statement appears. The following statement loads the AX register with the value of var, a C variable in scope:asm mov AX,varAn asm statement can reference a uniquely named member of a class, structure, or union without specifying the variable name or type before the period operator. But if the member name is not unique, you must specify a variable or type name before the period operator. If two structure types have a member name in common, as in this example:
struct first_type
{
char *mold;
int common_name;
};
struct second__type
{
char *mildew;
long common_name;
int unique_name;
};
then qualify the reference to common_name with the tag name:
asm mov [bx]first_type.common_name,10You need not qualify a reference to a unique member name. In the following example, unique_name is an anonymous structure member because it is a member of first_type. asm mov [bx].unique_name,10This statement generates the same instruction whether or not a qualifying name or type is present. For more information, see the section "Making anonymous references to structure members" later in this chapter. Functions in inline assembly languageBecause ASM blocks do not require separate source file assembly steps, writing a function using ASM blocks is easier than using a separate assembler. In addition, the compiler generates function prolog and epilog code.The expon2 function is an example of a function written in inline assembly language:
int expon2(int num, int power)
{
asm
{ mov AX,num // get first argument
mov CX,power // get second argument
shl AX,CL // AX = AX * (2 to the power of CL)
}
}
An inline function refers to its arguments by name and may appear
in the same source file as the callers of the function.
Refer to Using Assembly Language Functions for a description of the register stacks used by inline assembly instructions. Making anonymous references to structure membersYou can make anonymous references to members of a given structure, as in the following:
struct x {
int i;
int j;
int k;
} foo;
You can refer to these members i, j, and k anonymously, for
example, with the assembly instruction:
asm
{ mov BX,4
mov AX,foo[BX]; Refers to member j of foo
}
Using register variablesDigital Mars C++ supports register variables. Register variables are useful with inline assembly. If asm statements place results in registers, you can use register variables to access those values.For more information see "Using Register Variables" in Using Assembly Language Functions. Using the __LOCAL_SIZE symbolWhen using the inline assembler, the special symbol __LOCAL_SIZE expands to the number of bytes used by all local symbols. __LOCAL_SIZE is useful in combination with __declspec(naked), as __LOCAL_SIZE is the amount of space to reserve on the stack.For example:
__declspec(naked) int test()
{
int x, y, z;
_asm
{ push BP
mov BP,SP
sub SP,__LOCAL_SIZE
mov BX,__LOCAL_SIZE[BP]
mov BX,__LOCAL_SIZE+2[BP]
mov AX,__LOCAL_SIZE
mov AX,__LOCAL_SIZE+2
}
_AX = x + y + z;
_asm
{
mov SP,BP
pop BP
ret
}
}
Using ASM RegistersThe asm statement alters the registers outside the programmer's explicit assembly language instructions. Registers contain whatever values the normal control flow leaves in them at the point of the asm statement.For 16-bit memory modelsYou do not need to preserve the following registers when writing inline assembly language: AX, BX, CX, DX, SI, DI, ES, and flags (other than DF).C and C++ do not expect these registers to be maintained between statements, but they do preserve the following registers: CS, DS, SS, SP and BP.
NoteThe compiler does not use registers to hold register variables for functions containing inline assembly code.For 32-bit memory modelsFunctions can change the values in the EAX, ECX, EDX, ESI, EDI registers.Functions must preserve the values in the EBX, ESI, EDI, EBP, ESP, SS, CS, DS registers (plus ES and GS for the NT memory model). Always set the direction flag to forward. To maximize speed on 32-bit buses, make sure data aligns along 32-Function return values
Function return values
Interfacing to a member functionThe easiest way to interface an assembly language routine to a class member function is to provide a C wrapper function that can be called from the assembly language routine.An alternative is to write the member function in C++ and compile it with normal out-of-line member functions. Calling C Functions from an ASM BlockC functions, including C library functions, can be called from within the asm block, as in the following example:
#include <stdio.h>
char format [] = "% s %s %s \n";
char alas[] = "Alas,";
char poor[] = "poor";
char Yorick[] = "Yorick!";
void main(void)
{
asm
{ mov AX, offset Yorick
push AX
mov AX, offset poor
push AX
mov AX, offset Alas
push AX
mov AX, offset format
push AX
call printf
}
}
Simply push the needed arguments from right to left before calling
the function, since function arguments are passed on the stack. To
print the message, the example pushes pointers to the three strings,
formats them, and then calls printf.
Calling C++ functionsAn ASM block can call only global C++ functions that are not overloaded because the types of the arguments are unknown. The compiler issues an error if an ASM block calls an overloaded global C++ function or a C member function.You can also call a function declared with extern "C" linkage from an asm statement within a C++ program, because all the standard header files declare the library functions to have extern "C" linkage. Defining ASM blocks as C macrosA C++ macro is a convenient way to insert assembly language into source code. But, because a macro expands into a single, logical line, take care when writing them.If the macro expands into multiple instructions, enclose the instructions in an ASM block. The asm statement must precede each instruction. Also separate comments from code with /**/ characters rather than //. Unless you take these precautions, the compiler can be confused by C or C++ statements to the left or right of the assembly code or interpret instructions as comments when the macro becomes a single line. Without the closing brace, the compiler cannot tell where the assembly language ends. Warning: Do not use double-slash (//) characters within a macro. The compiler terminates the macro when it sees a double-slash. An ASM block written as a macro can accept arguments but, unlike a C macro, it cannot return values. But some MASM macros can be written as macros for C. The following MASM macro sets a video page to the value specified in the argument page: findpage MACRO page mov AH, 5 MOV AL,page int 10h ENDMThe following C macro does the same thing:
#define findpage(page) asm \
{ \
asm mov AH,5 \
asm mov AL,page \
asm int 10h \
}
RegistersThe following registers are supported. Register names are in upper or lower case.
OpcodesThe following instructions are supported. Opcode names are in upper or lower case.
Pentium 4 (Prescott) Opcodes Supported
AMD Opcodes
|