我认为,在执行细节方面,你会再次丢失,并忘记守则应当做些什么。 我建议,你首先在C执行该守则,然后逐渐改变该守则,使之成为一种与人的安全保障相同的标准,直到你能够在阿盟充分写到该守则的时候。
请注意,从小、干净和易于理解的C条(sortC1(>
)中的执行情况向在<代码>sortAsm()中稍微大但完全等效的ASM类执行进展。 利用你有利的档案比较工具,了解执行情况之间的哪些变化。
该法典:
#include <stdio.h>
#include <string.h>
char strOriginal[11] = "8163045297";
char str[11];
void sortC1(void)
{
int outIdx, inIdx, minIdx;
char min, tmp;
for (outIdx = 0; outIdx < 10; outIdx++)
{
minIdx = outIdx;
min = str[minIdx];
for (inIdx = outIdx; inIdx < 10; inIdx++)
{
if (min > str[inIdx])
{
minIdx = inIdx;
min = str[minIdx];
}
}
tmp = str[outIdx];
str[outIdx] = min;
str[minIdx] = tmp;
}
}
void sortC2(void)
{
char *outPtr, *inPtr, *minPtr;
int outCnt, inCnt;
char min, tmp;
for (outPtr = str, outCnt = 0;
outCnt < 10;
outPtr++, outCnt++)
{
minPtr = outPtr;
min = *minPtr;
for (inPtr = outPtr, inCnt = 10 - outCnt;
inCnt > 0;
inPtr++, inCnt--)
{
if (min > *inPtr)
{
minPtr = inPtr;
min = *minPtr;
}
}
tmp = *outPtr;
*outPtr = min;
*minPtr = tmp;
}
}
void sortC3(void)
{
char *outPtr, *inPtr, *minPtr;
int outCnt, inCnt;
char min, tmp;
outPtr = str;
outCnt = 0;
while (outCnt < 10)
{
minPtr = outPtr;
min = *minPtr;
inPtr = outPtr;
inCnt = 10 - outCnt;
while (inCnt > 0)
{
if (min > *inPtr)
{
minPtr = inPtr;
min = *minPtr;
}
inPtr++;
inCnt--;
}
tmp = *outPtr;
*outPtr = min;
*minPtr = tmp;
outPtr++;
outCnt++;
}
}
void sortC4(void)
{
char *outPtr, *inPtr, *minPtr;
int outCnt, inCnt;
char min, tmp;
outPtr = str;
outCnt = 0;
outerloop:
minPtr = outPtr;
min = *minPtr;
inPtr = outPtr;
inCnt = 10 - outCnt;
innerloop:
if (min > *inPtr)
{
minPtr = inPtr;
min = *minPtr;
}
inPtr++;
inCnt--;
if (inCnt > 0)
goto innerloop;
tmp = *outPtr;
*outPtr = min;
*minPtr = tmp;
outPtr++;
outCnt++;
if (outCnt < 10)
goto outerloop;
}
void sortAsm(void)
{
char* rdi; // points to the boundary of the "outer loop"
char* rsi; // points to the boundary of the "inner loop"
char* r8; // holds the current minimum value
char r9b; // holds the current minimum value
char r10b; // temporary storage for character exchange
long long rbx; // outer loop counter
long long rax; // inner loop counter
rdi = str; // initialize outer loop pointer
rbx = 0; // initialize outer loop counter
outerloop:
r8 = rdi; // assume current element of partially sorted array is minimum,
r9b = *r8; // save its index and value
rsi = rdi; // initialize inner loop pointer
rax = 10; // initialize inner loop counter
rax -= rbx;
innerloop:
// compare the current small value with the value in [rsi]
if (r9b > *rsi)
{
r8 = rsi; // save the new small value s index
r9b = *r8; // save the new small value
}
rsi++; // move the inner loop pointer forward
rax--; // decrement the inner loop counter
if (rax > 0)
goto innerloop;
// When the inner loop is completed...
// First, do the swap
// to swap [r8] (target memory address) with [rdi] (outer array boundary)
r10b = *rdi;
*rdi = r9b;
*r8 = r10b;
rdi++; // move the outer loop pointer forward
rbx++; // increment the outer loop counter
if (rbx < 10)
goto outerloop;
}
int main(void)
{
strcpy(str, strOriginal);
printf("before sorting: %s
", str);
sortC1();
printf("after sorting : %s
", str);
strcpy(str, strOriginal);
printf("before sorting: %s
", str);
sortC2();
printf("after sorting : %s
", str);
strcpy(str, strOriginal);
printf("before sorting: %s
", str);
sortC3();
printf("after sorting : %s
", str);
strcpy(str, strOriginal);
printf("before sorting: %s
", str);
sortC4();
printf("after sorting : %s
", str);
strcpy(str, strOriginal);
printf("before sorting: %s
", str);
sortAsm();
printf("after sorting : %s
", str);
return 0;
}
The output:
before sorting: 8163045297
after sorting : 0123456789
before sorting: 8163045297
after sorting : 0123456789
before sorting: 8163045297
after sorting : 0123456789
before sorting: 8163045297
after sorting : 0123456789
before sorting: 8163045297
after sorting : 0123456789