[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. The following situations are usually used in assembly: (1) Improve Code For example, MMX commands; (2) perform some special operations on the hardware, such as disconnection, this is common in kernel-level code. However, we will discuss some of the basics of Embedded Assembly with you. We recommend that you do experiments with me and prove everything with facts.
(1) Register protection when using Embedded Assembly
[CPP] View plaincopy
-
- IntProcess ()
-
- {
-
- _ ASM
-
- {
- PUSH AX
-
- Push BX
-
- Push CX
-
- Pop CX
-
- Pop BX
-
- Pop ax
-
- }
-
- Return1;
-
- }
(2) When calling a function, you can directly use call. The returned values are transmitted using eax. Pay attention to the stack balance.[CPP]View plaincopy
-
- IntAdd (IntA,IntB)
-
- {
-
- ReturnA + B;
-
- }
-
-
- IntProcess ()
-
- {
- IntValue = 0;
-
- _ ASM
-
- {
-
- Push eax
-
- Push 2
-
- Push 3
-
- Call add
-
- Add ESP, 8
- MoV value, eax
-
- Pop eax
-
- }
-
- Return1;
-
- }
(3) Pay attention to the difference between pointer and value during calculation.[CPP]View plaincopy
-
- VoidProcess ()
-
- {
- IntValue = 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[CPP]View plaincopy
-
- Static IntGlobal = 10;
-
- VoidProcess ()
-
- {
-
- _ ASM
-
- {
-
- Push eax
-
- MoV eax, global
-
- Add eax, 1
-
- MoV global, eax
-
- Pop eax
- }
-
-
- Assert (Global = 11 );
-
- Return;
-
- }
(5) loop jump[CPP]View plaincopy
-
- VoidProcess ()
-
- {
-
- IntValue = 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.[CPP]View plaincopy
- void process ()
- {
- _ ASM {
- CLI
- }
-
- return ;
- }
(7) vc6.0 supports MMX instruction sets, however, vs2005 supports MMX and SSE instruction sets (8) if the project exists *. ASM Assembly file, you need to follow the steps below A) select [project]-> [setting] B) click project on the left and select *. in the ASM file, two options are displayed in the tab on the right, namely general and custom build C.) enter the corresponding compilation command in commands, so the MASM or NASM type is as follows, see the corresponding Compilation Manual.