Vs Embedded Assembly ASM

Source: Internet
Author: User
Tags emit

1. Format of Embedded Assembly Language

C ++ is a superset of C language. It is an object-oriented programming language based on C language. Microsoft Visual C ++ 5.0/6.0 is a widely used development system on Windows. x platform. This section uses Visual C ++ 5.0/6.0 as an example to describe the hybrid programming of assembly language and C ++ in a 32-bit Windows 9. x environment. It is also divided into two methods: Embedded Assembly and module call.

Visual c ++ directly supports the embedded assembly method, without the need for independent assembly systems and other connection steps. Therefore, embedded assembly is simpler and more convenient than module connection. The embedded assembly method of Visual C ++ is the same as that of other C/C ++ compiling systems. Of course, some details are different. Embedded Assembly commands use the _ ASM keyword (Note that _ ASM is preceded by two underscores, but visual c ++ 5.0/6.0 also supports the _ ASM format, which is intended to be compatible with previous versions.).

The Visual C ++ Embedded Assembly format _ ASM {instruction} is in the form of an assembly language section in curly brackets, for example: // _ ASM
_ ASM
{
MoV eax, 01 H // supports the annotation format of the Assembly Language
MoV dx, 0xd007; 0xd007 = d007h, supporting data expression in C/C ++
Out dx, eax
} It also has a single assembly language instruction form: // single _ ASM assembly instruction form _ ASM mov eax, 01 H
_ ASM mov dx, 0d007h
_ ASM out Dx and eax can also use spaces to separate multiple _ ASM assembly language commands in one line: // multiple _ ASM statements in the same row, separate them with spaces: _ ASM mov eax, 01 H _ ASM mov dx, 0xd007 _ ASM out dx, and eax generate the same code in the above three forms, however, the first form has more advantages. Because it can clearly separate C ++ code from assembly code to avoid confusion. If the _ ASM command and C ++ statement are placed in the same line without parentheses, the compiler cannot tell where the Assembly Code ends and where c ++ starts. The program segment in _ ASM curly brackets does not affect the scope of the variable. The _ ASM block can be nested without affecting the scope of the variable.

2. Notes for embedding assembly languages

Of course, there are also many rules embedded in assembly languages, and many precautions:

1. Notes for using the Assembly Language in _ ASMEmbedded Assembly Code supports 80486 of all command systems. Visual c ++ 5.0/6.0 also supports MMX instruction sets. For commands that are not supported yet, Visual C ++ provides the _ emit pseudocommand for extension. _ Emit pseudo commands are similar to DB pseudo commands in masm. They can be used to define a byte content and can only be used for program code segments. For example: # define CPU-ID _ ASM _ emit 0x0f _ ASM _ emit 0xa2 // define the macro _ ASM {CPU-ID} of the assembly instruction code // use the C ++ although macro-embedded assembly code can use C ++ data types and data objects, however, the pseudo commands and operators of MASM cannot be used to define data. Programmers cannot use the pseudo commands dB/DW/DD/DQ/DT/DF and the DUP/this operator; nor can we use MASM structures and records (without pseudo commands struct, record, width, and mask ). Visual c ++ does not support macro pseudocommands (such as macro, endm, repeat/For/Forc) and macro operators (such!, &, %, Etc ). Although Embedded Assembly does not support most of the MASM pseudo commands, it supports even and align. These commands place NOP commands in assembly code to align the boundary. For some processors, this can read commands more effectively. Embedded Assembly Code can use the length, size, and type operators to obtain the size of C ++ variables and types. Length is used to return the number of array elements. The return value for non-array variables is 1; Type returns the C ++ type or variable size. If the variable is an array, it returns the size of a single element in the array. Size returns the size of the C ++ variable, that is, the product of length and type. For example, if the data int iarray [8] (INT type is 32-bit, 4 bytes), the length iarray returns 8 (equivalent to the sizeof (iarray) of C ++) /sizeof (iarray [0]); Type iarray returns 4 (equivalent to the sizeof (iarray [0]) of C ++); size iarray returns 32 (equivalent to the sizeof (iarray) of C ++ )). Functions written in assembly languages do not have to store eax, EBX, ECx, EDX, ESI, and EDI registers; however, other registers (such as DS, SS, ESP, EBP, and integer sign registers) used in the function must be saved ). For example, if STD and CLD are used to change the direction flag, the value of the Flag register must be saved. When the Embedded Assembly reference segment is used, it should pass through the register rather than the segment name; when the segment is exceeded, it must be clearly described using the segment register, such as es: [EBX].
The modifier must be fully written and cannot be abbreviated for compilation. For example
_ ASM {mov [EDI], 0x065b42e9} it generally seems that the DWORD of mov is actually not. The disassembly shows that this command is mov byte PTR [EDI], 0xe9 is byte by default, so you must write a full modifier.
Correct: _ ASM {mov dword ptr [EDI], 0x065b42e9}
Also, you must add 0x before the hexadecimal number, for example, mov dword ptr [EDI + 0xc], 0xf9a4aee9 2. Notes for using C ++ in _ ASMEmbedded Assembly Code can use the following elements of C ++: Symbols (including labels, variables, function names), constants (including symbol constants, enumeration members), macro and preprocessing commands, annotations (/**/and //, you can also use the annotation style of the assembly language), type names and structures, and associated members. Embedded Assembly statements use the C ++ symbol. Each assembly language statement contains only one C ++ symbol (multiple symbols can only be used by length, type, and size expressions ); _ before referencing a function in ASM, you must explain its prototype in the Program (otherwise, the compiled program cannot identify whether it is a function name or a number ); _ ASM cannot use the C ++ symbol that is the same as the reserved words of MASM (for example, the instruction mnemonic and register name), nor can it recognize the structure and Union keyword. In Embedded Assembly Language statements, Integer constants (such as 378 h) can be expressed in the Assembly Language format, or in the C ++ format (such as 0x378 ). Embedded Assembly Language statements cannot use special operators of C ++, such as <; operators of both languages are used as assembly language operators in Assembly statements, such as * and []. For example, in the int array [6]; // C ++ statement, [] indicates an element of the array _ ASM mov array [6], in the Bx // assembly language, [] represents the byte offset of the distance identifier. In Embedded Assembly, any symbol (including the variable name) that contains the range of _ ASM can be referenced ), it references the C ++ variable by using the variable name. For example, if VaR is an integer int variable in C ++, you can use the following statement: _ ASM mov eax. var: if the class, structure, and union member names are unique, in _ ASM, the member name can be referenced without specifying the variable or type name; otherwise, it must be specified. Example: struct first_type
{
Char * carray;
Int same_name;
};
Struct second_type
{
Int Ivar;
Long same_name;
};
Struct first_type FTYPE;
Struct second_type stype;
_ ASM
{
MoV EBX, offset FTYPE
MoV ECx, [EBX] FTYPE. same_name // FTYPE is required
MoV ESI, [EBX]. carray // you can use FTYPE instead of FTYPE)
} # Define portio _ ASM \
/* Port output */\
{\
_ ASM mov eax, 01 H \
_ ASM mov dx, 0xd007 \
_ ASM out dx, eax \
} The macro is opened as a logical line (where "\" is the continued line character): _ ASM/* port output */{__ ASM mov eax, the labels defined in 01 H _ ASM mov dx, 0xd007 _ ASM out dx, eax} _ ASM are not case sensitive, the labels in the assembly language commands jump to C ++ are case-insensitive. labels in C ++ are case-sensitive only when the GOTO statement is used.

In the value assignment operation in vs2012, you must note that you cannot directly assign values so that the corresponding values are not obtained, for example:

int nStatus;__asm{mov nStatus,dword ptr [0x49419C]}

The compiled result is mov dword ptr [ebp-0xC], 0x49419c and the expected result does not conform to the writing needs to be converted

Write as follows:

int nStatus;    __asm    {        push eax        mov eax,dword ptr [0x49419C]        mov nStatus,eax                               pop eax    }

3. Example of embedding Assembly Language

Use _ ASM to compile Functions

Embedded Assembly can not only write C/C ++ functions, but also call C functions (including C library functions) and non-overloaded global C ++ functions, you can also call any function described in "extern" C ", but cannot call a member function of C ++. Because all the standard header files use the extern "C" to describe the library functions, the embedded assembly in the C ++ program can call the C library functions.
 
Example 7.13: Embedded compilation Functions
 
// C ++ program: lt713.cpp
# Include
Int power2 (INT, INT );
Void main (void)
{
Cout <"6 to the power of 2 multiplied by 5 equals to: \ t ";
Cout <}
Int power2 (INT num, int power)
{
_ ASM
{
MoV eax, num; obtain the first parameter
MoV ECx, power; second parameter
SHL eax, Cl; Calculate eax = eax × (2 ^ cl)
} // The returned value is stored in eax
}
 
The Assembly statement can reference the parameter through the parameter name, and return the exit Parameter Using return. In this example, although the return statement is not used, the return value is still returned, but a warning may be generated during compilation (when the warning level is set to 2 or higher ). The Convention for returning values is as follows: for data smaller than or equal to 32 bits, it is extended to 32 bits and stored in the eax register and returned; 4 ~ The 8-byte return values are stored in the edX. eax register and returned in pairs. Larger bytes of data are stored in eax and returned.
In the developer Studio development system, create a Win32 console program project, create the source program, and add it to the project. Then, compile the connection to generate an executable file. The program is displayed as follows:
The 6th power of 2 is multiplied by 5 to 320
In the developer Studio development system, you can use the link tag settings of the projects menu settings command to add debugging information (I .e., the/Zi option). Embedded Assembly can be debugged at the source program level; you can also select listing files in the C/C ++ label to output a list of output programs (I .e., the options of/FA,/FAC,/Fas, And/FACS ).

4. Call specifications

The parameter transfer of C/C ++ and assembly language mixed programming usually uses the stack. The call specification determines the method and naming convention of using the stack. The two must be consistent, for example, the _ cdecl call specification of Visual C ++ and the C language type of MASM.

The Visual C ++ language has three call specifications (calling convntions): _ cdecl, _ stdcall, and _ fastcall. Visual c ++ uses the _ cdecl call specification by default. It automatically adds an underline before the name, and pushes the real parameters into the stack from right to left. The calling program balances the stack. The Windows Graphical User Interface process and API functions use the _ stdcall call specification. It automatically adds an underline before the name, followed by the @ and the decimal value indicating the number of bytes occupied by the parameter, press the real parameters into the stack from right to left, and the called program balances the stack. The _ fastcall call specification of Visual C ++ adds @ before and after the name, followed by a decimal value indicating the number of bytes occupied by the parameter; the first two double-character parameters are passed using the ECX and EDX registers, and other parameters are passed through the stack (from right to left) to be called to balance the stack. Do not use the _ fastcall specification for mixed programming with other languages.

The MASM assembly language uses the language type to determine call specifications and naming conventions. supported language types include C, syscall, stdcall, Pascal, basic, and FORTRAN. For example, we usually adopt the C language specification: it automatically adds an underline before the identifier, and pushes the call parameter into the stack in the order from right to left, and the call program balances the stack.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.