[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]
Embedded Assembly is a useful supplement to pure assembly files. There are usually the following situations used in assembly: (1) Improve code efficiency and use some special instructions on the cpu, such as mmx commands; (2) perform some special operations on the hardware, for example, Guanzhong disconnection is common in kernel-level code. However, we pay attention to some basic points of Embedded Assembly and discuss them with everyone. We suggest you and me to do experiments together and prove everything with facts.
(1) Register protection when using Embedded Assembly
Copy to clipboardprint? Int process ()
{
_ Asm
{
Push ax
Push bx
Push cx
Pop cx
Pop bx
Pop ax
}
Return 1;
}
Int process ()
{
_ Asm
{
Push ax
Push bx
Push cx
Pop cx
Pop bx
Pop ax
}
Return 1;
} (2) when calling a function, you can directly use call. The returned values are transmitted using eax. Pay attention to the stack balance.
Copy to clipboardprint? Int add (int a, int B)
{
Return a + B;
}
Int process ()
{
Int value = 0;
_ Asm
{
Push eax
Push 2
Push 3
Call add
Add esp, 8
Mov value, eax
Pop eax
}
Return 1;
}
Int add (int a, int B)
{
Return a + B;
}
Int process ()
{
Int value = 0;
_ Asm
{
Push eax
Push 2
Push 3
Call add
Add esp, 8
Mov value, eax
Pop eax
}
Return 1;
} (3) Pay attention to the difference between the pointer and the value during calculation.
Copy to clipboardprint? Void process ()
{
Int value = 0;
Int * address = & value;
_ Asm
{
Push ebx
Mov value, 1
Mov ebx, address
Mov [ebx], 2
Pop ebx
}
Assert (value = 2 );
Return;
}
Void process ()
{
Int value = 0;
Int * address = & value;
_ Asm
{
Push ebx
Mov value, 1
Mov ebx, address
Mov [ebx], 2
Pop ebx
}
Assert (value = 2 );
Return;
} (4) access to global data
Copy to clipboardprint? Static int global = 10;
Void process ()
{
_ Asm
{
Push eax
Mov eax, global
Add eax, 1
Mov global, eax
Pop eax
}
Assert (global = 11 );
Return;
}
Static int global = 10;
Void process ()
{
_ Asm
{
Push eax
Mov eax, global
Add eax, 1
Mov global, eax
Pop eax
}
Assert (global = 11 );
Return;
} (5) loop jump
Copy to clipboardprint? Void process ()
{
Int value = 0;
_ Asm
{
Push eax
Start:
Mov eax, value
Add eax, 1
Mov value, eax
Cmp value, 10
Jne start
Pop eax
}
Assert (value = 10 );
Return;
}
Void process ()
{
Int value = 0;
_ Asm
{
Push eax
Start:
Mov eax, value
Add eax, 1
Mov value, eax
Cmp value, 10
Jne start
Pop eax
}
Assert (value = 10 );
Return;
} (6) commands for Guanzhong disconnection cannot be executed in VC, and exceptions may occur.
Copy to clipboardprint? Void process ()
{
_ Asm {
Cli
}
Return;
}
Void process ()
{
_ Asm {
Cli
}
Return;
} (7) VC6.0 supports MMX instruction sets, but VS2005 supports MMX and SSE instruction sets.
(8) If the *. asm Assembly file exists in the project, perform the following steps:
A) Select project> setting]
B) Click the project on the left and select a *. asm file. The tab on the right displays two options: General and Custom Build.
C) input the corresponding Compilation instruction in Commands to the MASM or NASM type. Refer to the corresponding Compilation Manual.