Embedded assembly is the Microsoft to improve the direct operation of the program hardware capabilities, and the realization of the efficiency of the large task program, embedded in VC, he does not rely on assembler internal assembly code to assemble, these inline assembly code is implemented by the C compiler, you can make the program as if from the Assembly state. This means that if you use the inline ASM to assemble in C + + programs then it is doomed to be unable to cross-platform, for the students who do not have a compilation base is required to learn the next Wang Yu 8086 assembly programming. , because there are times when C + + programmers have to understand these things, or you never know what the compiler is doing for your function. What else do you need to optimize, whatever the purpose I feel as C + + programmers need to know.
A big brother around me told me that using the assembly is not necessarily faster than C + +, I agree. Therefore, the efficiency of the program is not only the language level of the operation of the decision, more is the program writer's Code of the quality of the high and low decision.
Inline assembly is passed in C + +
actually found the 500W data sorting results are as follows:
Algorithm name inline assembler algorithm time C + + algorithm time
Bubble sort 5W data slow to die 5W data slowly dying.
Quick sort 600ms about 500ms around
------------------Why there is a fast sorting algorithm, the compiled results are not as high as C/s + + efficiency, because I write inline assembly without compiler automatic generation of high efficiency.
That is, the quality of the code is not high. ~~
_asm
{
...
}
_asm .....
The introduction of assembly code allows the program to fall into the Assembly state, and MMX assembler instructions are used in a large number of processing media references.
The following is an inline assembler implementation function for the bubble sort generation:
Best time complexity T (n) =o (n) ~o (n*n) stability algorithm
#define SIZE ten
void sortbubble (int arr[],int sizen)
{
int *p;
p=arr-1; &a[0]-1
__asm
{
mov esi,p;
mov Ecx,sizen;
_outter:
mov edx,ecx;
_inner:
cmp edx,ecx ; remove equality
JZ _exchange_no
mov eax,[esi+ecx*4]; ; The passed array can not be obtained directly through an array subscript within the function, and can only be addressed by the pointer to
mov ebx,[esi+edx*4];
CMP EAX,EBX;
JNB _exchange_no;
mov [esi+ecx*4],ebx; ; Exchange of even elements
mov [esi+edx*4],eax;
_exchange_no:
Dec edx;
Jnz _inner;
Loop _outter;
}
}
bubble sort results as shown in figure
A fast sorting algorithm based on embedded Assembly
Memory status in stacks first deposit stack extended from high address to low address//esp-xxx//Note parameter name does not conflict with inline assembler keyword void quicksortasm (int arr[],int lowindex,int Highin Dex) { int*p=arr; //For addressing convenience int begin;// int& nbsp End __asm { //;mov eax,[ebp+16] Stack high address to low //Use registers mov eax,lowindex ; Index a mov ebx,highindex index last cmp eax,ebx jg _exit_func
mov esi,p ; storage array address-1 for addressing use mov edx,[esi+4*eax] Store key _outer:cmp eax,ebx ; If index a >index end jnb _endlable & nbsp; ; out of qualifying exit & nbsp; _find1:cmp eax,ebx Enter the first floor cycle jnb _inner1 conditions 1 searching for the first value less than key cmp [esi+4*ebx],edx ; compare array elements and edx jb _inner1 from a later start find the first array element less than key on the right Sub ebx,1 last index-1 last= Last-1 Jmp _find1 ; jump to Loop head _inner1:
mov ecx,[esi+4*ebx] ; find less than Exchange value mov [esi+4*eax],ecx _find2:cmp eax,ebx ; Enter the first floor cycle
jnb _inner2 Condition 1 ; After looking for the first value less than key cmp [esi+4* eax],edx from the left to find the first element greater than key jg _inner2 for signed numbers add eax,1 a index+1 jmp _find2 jump to the Loop head _inner2: mov ecx,[esi+4*eax] ; will be the first greater than and axis exchange
mov [esi+4*ebx],ecx jmp _outer; _endlable: mov [esi+4*eax],edx ; shaft reset ///
Make recursion parameter free left mov begin,eax; mov end,ebx }// quicksortasm (arr,lowindex
, begin-1);
quicksortasm (Arr,begin+1,highindex); _asm { mov ecx,begin ; Recursion 1 sub ecx,1 index-1 & nbsp
push ecx ; mov Edx,loWindex; push edx mov Eax,arr push eax call quicksortasm add esp,0ch Stack Balance mov ecx,highindex , recursive 2 push ecx mov edx,begin add edx,1 push edx mov eax,arr push eax call quicksortasm add
esp,0ch ; recovery stack } _exit_func: return;
}
as shown in figure
C + + implementation of the bubble sort algorithm:
Best time complexity T (n) =o (n) ~o (n*n) stability algorithm void sortbubblec (int arr[],int sizen) { &nbs P
BOOL Flag=true//If no Exchange direct exit int tem; for (int i=0;i<sizen-1&flag;i++) {
Flag=false; for (int j=0;j<sizen-i-1;j++)//internal sequence bubbling {& nbsp if (arr[j]>arr[j+1]) { & nbsp
Flag=true;
TEM=ARR[J];
arr[j]=arr[j+1];
Arr[j+1]=tem; } } }
As shown in figure
the realization of fast sorting algorithm of C + +
Fast sort
void quicksortc (int arr[],int low,int high)
{
if (Low>high) return
;
int Begin=low, End=high,key=arr[low];
while (Begin<end)
{while
(Begin<end&&arr[end]>=key)
--end;
Arr[begin]=arr[end];
while (Begin<end&&arr[begin]<=key)
++begin;
Arr[end]=arr[begin];
}
Arr[begin]=key;
QUICKSORTC (arr,0,begin-1);
QUICKSORTC (Arr,begin+1,high);
}
QQ 4223665 Technical Exchange Group 387761601
Welcome everyone to Exchange learning software technology!