VC inline ASM Assembly Learning notes

Source: Internet
Author: User

Reprinted: http://old.blog.edu.cn/user2/44558/archives/2006/1209858.shtml
VC inline ASM Assembly Learning notes

Objective: To learn how to design the ASM assembly language program in VC to improve the underlying application capability.

Compilation in VC does not require additional compilers and connectors, and can handle some things that cannot be handled in VC, and can be used in C variables, so it is very convenient. however, it does not support all MASM macros and data indicators.

The following three methods can be basically used in VC:

_ ASM
{
MoV Al, 2
MoV dx, 0xd007
Out Al, DX
}

_ ASM mov Al, 2
_ ASM mov dx, 0xd007
_ ASM out Al, DX

_ ASM mov Al, 2 _ ASM mov dx, 0xd007 _ ASM out Al, DX

Obviously, the best is the first one.

_ Emit is equivalent to DB in MASM.

Although inline assembly can use variables in C/C ++, it cannot define variables by itself.

You can use even and align.

The MASM macro cannot be used.

You must use registers to describe segments. The spans must be explicitly stated, for example, ES: [BX].

Type and variable size in inline assembly:

We can use length to obtain the number of elements in the array in C/C ++. If it is not an array, the result is. use size to obtain the variable size in C/C ++. The size of a variable is the product of length and type. type is used to obtain the size of a variable. If it is an array, it obtains the size of a single element in an array.

MMX commands can be used in VC.

The inline ASM code is not easy to transplant, so avoid it as much as possible.
Elements that can be used in the _ ASM block:

1. the symbol includes the function name.

2. Constant and enum type

3. Macro and preprocessing members

4. Notes

5. type name in MASM

6. typedef, PTR, Type

Register:

Generally, when the _ ASM block starts, the register is empty. The register value cannot be saved between the two _ ASM blocks.

If a function is declared as _ fastcall, the parameter will be placed in the register, which will cause problems to register management. therefore, to declare a function as _ fastcall, you must save the ECX register. to avoid the preceding conflicts, do not include _ ASM blocks in functions declared as _ fastcall. if the/GR compilation option is used (it becomes _ fastcall globally), each function is declared as _ cdecl or _ stdcall. This attribute tells the compiler to use the traditional C method.

You do not need to save the eax, EBX, ECx, EDX, ESI, or EDI registers. However, you need to save the DS, SS, SP, BP, and Mark registers.

If the program changes the direction sign for STD and CLD, you must restore it to the original value.

Jump:

You can use Goto and ASM to specify to jump to a program segment Other than label or _ ASM. For example:

Void func (void)
{
Goto c_dest;/* Legal: correct case */
Goto c_dest;/* error: Incorrect case */

Goto a_dest;/* Legal: correct case */
Goto a_dest;/* Legal: Incorrect case */

_ ASM
{
JMP c_dest; Legal: correct case
JMP c_dest; Legal: Incorrect case

JMP a_dest; Legal: correct case
JMP a_dest; Legal: Incorrect case

A_dest:; _ ASM label
}

C_dest:/* C label */
Return;
}

Do not use the function name as the label. Otherwise, the function will be executed rather than the label, as shown below:

; Bad technique: using library function name as label
JNE exit
.
.
.
Exit:
; More _ ASM Code follows

Dollar sign $ is used to specify the current position. It is usually used for conditional jump as follows:

JNE $ + 5; next instruction is 5 bytes long
JMP farlabel
; $ + 5
.
.
.
Farlabel:
Call functions in C:

The following is an example:

# Include

Char format [] = "% S % s/n ";
Char Hello [] = "hello ";
Char world [] = "world ";
Void main (void)
{
_ ASM
{
MoV eax, offset world
Push eax
MoV eax, offset hello
Push eax
MoV eax, offset format
Push eax
Call printf
// Clean up the stack so that main can exit cleanly
// Use the unused register EBX to do the cleanup
Pop EBX
Pop EBX
Pop EBX
}
}

Note: The function parameters are Stack pressure from right to left.

Cannot access class member functions in C ++. You can access the extern "C" function.

VC will not optimize the code in the _ ASM block.

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.