#include <algorithm>
void sort(unsigned *a, int n) {
int*p=*a; //为了寻址方便
int begin; //
int end; //
__asm
{
//;mov eax,[ebp+16] 堆栈高地址向低
//使用寄存器
mov eax,lowIndex ;index first
mov ebx,highIndex ;index last
cmp eax,ebx ;退程序
jg _exit_func
mov esi,p ;存储数组地址-1用于寻址使用
mov edx,[esi+4*eax] ;存放key
_outer:cmp eax,ebx ; 如果index first >index end
jnb _endLable ; 不符合条件退出
_find1: cmp eax,ebx ;进入第一层循环
jnb _inner1 ;条件1
;从后寻找第一个小于key的值
cmp [esi+4*ebx],edx ;从后面开始比较数组元素和edx
jb _inner1 ;找出右边第一个小于key的数组元素
sub ebx,1 ;last index -1 last=last-1
jmp _find1 ;跳转到循环头
_inner1:
mov ecx,[esi+4*ebx] ;找到小于交换值
mov [esi+4*eax],ecx
_find2: cmp eax,ebx ;进入第一层循环
jnb _inner2 ;条件1
;从后寻找第一个小于key的值
cmp [esi+4*eax],edx ;从左边找第一个大于key的元素
jg _inner2 ;针对有符号数
add eax,1 ;first index+1
jmp _find2 ;跳转到循环头
_inner2:
mov ecx,[esi+4*eax] ;将第一个大于的 和 轴交换
mov [esi+4*ebx],ecx
jmp _outer;
_endLable:
mov [esi+4*eax],edx ;轴复位
///进行递归 参数自由向左
mov begin,eax ;
mov end,ebx
}
// QuickSortAsm(arr,lowIndex,begin-1);
// QuickSortAsm(arr,begin+1,highIndex);
_asm
{
mov ecx,begin ; 递归1
sub ecx,1 ;index -1
push ecx ;
mov edx,lowIndex ;
push edx
mov eax,arr
push eax
call QuickSortAsm
add esp,0Ch ;堆栈平衡
mov ecx,highIndex ;递归2
push ecx
mov edx,begin
add edx,1
push edx
mov eax,arr
push eax
call QuickSortAsm
add esp,0Ch ;恢复堆栈
}
_exit_func:
return ;
}
| Compilation | N/A | N/A | Compile Error | Score: N/A | 显示更多 |