"std ; repne ; scasb\n\t"
1、std:方向位DF置位,即DI進行自減操作。
2、repne; scasb
這兩條組合指令實現循環比較。ecx初值為15*1024,al=0,di初值為&mem_map[15*1024-1],即從數組mem_map的最後壹項開始,依次與al(=0)進行比較。假設數組第i項mem_map[i]==0,則結束循環,此時ecx=i, edi=&mem_map[i-1](因為ecx初值為15*1024,di初值為數組最後壹項15*1024-1的地址)。找到空閑頁面後,將該數組項置1,即*(edi+1)=mem_map[i]=1,即語句“movb $1,1(%%edi)”實現的功能。此時,ecx即為空閑頁面索引。
幾點說明:
1、rep循環結束條件:
Repeat Prefix Termination Condition 1 Termination Condition 2
REP RCX or (E)CX = 0 None
REPE/REPZ RCX or (E)CX = 0 ZF = 0
REPNE/REPNZ RCX or (E)CX = 0 ZF = 1
2、rep循環執行順序:
WHILE CountReg ≠ 0
DO
Service pending interrupts (if any);
Execute associated string instruction; // 1、執行相關指令。例如scansb指令,除了執行al與*di的比較外,di也會被影響,即di自減1(當DF==1時)或自加1(當DF==0時)
CountReg ← (CountReg – 1); // 2、ECX自減
IF CountReg = 0 // 3、判斷ECX是否已減到0
THEN exit WHILE loop; FI;
IF (Repeat prefix is REPZ or REPE) and (ZF = 0) // 4、最後才判斷其他相關標誌。
or (Repeat prefix is REPNZ or REPNE) and (ZF = 1)
THEN exit WHILE loop; FI;
OD;
3、scasb指令對di的影響:
After the comparison, the (E)DI register is incremented or decremented automatically according to the setting of
the DF flag in the EFLAGS register. If the DF flag is 0, the (E)DI register is incremented; if the DF flag is 1, the (E)DI
register is decremented. The register is incremented or decremented by 1 for byte operations, by 2 for word operations, and by 4 for doubleword operations.
以上指令請參考《Intel 64 and IA-32 Architectures Software Developer's Manual》。