2016-11-06 53 views
3
int sarl_n(int x, char n){ 
    x <<= 2; 
    x >>= n; 
    return x; 

}這個組裝怎麼可能?

當我和 「GCC -m32 -S sarl_n.c」 組裝,它發射這個代碼:

.cfi_startproc 
movl 4(%esp), %eax 
movsbl 8(%esp), %ecx 
sall $2, %eax 
sarl %cl, %eax #This is the part which I don't understand 
ret 
.cfi_endproc 

爲什麼是gcc使用 「迷你寄存器」 %cl代替大一號%ecx

編輯:我用O2選項以獲得更短的彙編代碼

+0

這對優化的目的我想:8位長CL%是更好的存儲小的結果比16位%ECX。 –

+0

嘗試用'-O2'選項組裝它以接收更明智的輸出。 – zx485

+0

@kiner_shah:我懷疑它是爲了優化目的_,因爲沒有'-Ox'選項,它不會得到優化(很多)。我想這是一些轉換塊的原始輸出。 – zx485

回答

10

之所以討論以下行(以前的版本)

sarl %cl, 8(%ebp) #This is the part which I don't understand 

或(當前版本)

sarl %cl, %eax  #This is the part which I don't understand 

使用的是%cl而不是%ecxSAR操作碼只支持%cl regi ster作爲可變算術移位的輸入(通過%cl次)。

here,我引用:

SAR r/m32, CL  MC Valid Valid Signed divide* r/m32 by 2, CL times. 
+0

我會明白,如果r/m32的部分是r/m8 ...因爲r/m32意味着32位的寄存器,對吧? – Bechma

+1

是的。 'r/m32'確實意味着32位的內存地址(例如你之前版本中的'8(%ebp)')或32位寄存器(例如當前版本中的'%eax')。對於_AT&T語法_'%cl','CL'是_Intel語法。 _AT&T_語法也會反轉源/目標順序。因此,_AT&T_語法中的%c1,%eax'在_Intel語法_中是'SAR EAX,CL'。 – zx485

+0

現在一切都變得更加清晰了,謝謝你,先生! – Bechma