2014-11-01 62 views
0
pushl %ebp 
movl %esp, %ebp 
subl $36, %esp    //allocate 36 btyes for local vars 
movl 8(%ebp), %eax   // eax = n 
andl $1, %eax    //how can u andl a parameter? parameter can be greater than 1 
testl %eax, %eax 
jmp .L4 
cmpl $2, 8(%ebp) 
jne .L6 

.L4: 
movl 8(%ebp), %eax   //eax = n 
movl %eax, -28(%ebp)   //x = eax 
movl $1431655766, -32(%ebp) //y = 1431655766 
movl -32(%ebp), %eax   //eax = y 
imull -28(%ebp)    //edx = x * eax 
movl %edx, %ecx    //ecx = edx 
movl -28(%ebp), %eax   //eax = x 
sarl $31, %eax    //eax = eax >> 31 
movl %ecx, %edx    //edx = ecx 
subl %eax, %edx    //edx = edx - eax 
movl %edx, -24(%ebp)   //z = edx 
movl -24(%ebp), %eax   //eax = z 
addl %eax, %eax    //eax = eax+eax 
addl -24(%ebp), %eax   //eax = z+eax 
movl -28(%ebp), %ecx   //ecx = x 
subl %eax, %ecx    //ecx = ecx-eax 
movl %ecx, -24(%ebp)   //z = ecx 
cmpl $0, -24(%ebp)   //compare z and 0 
jne .L7       //if not equal jmp to .L7 
cmpl $3, 8(%ebp)    //compare n and 3 
jne .L6       //if not equal jmp .L6 

好的我有這個程序集片段,我想知道你怎麼能夠和$ 1和一個參數,參數可以大於1。此外,testl似乎沒用,因爲jmp跳轉不管。有什麼想法嗎?謝謝。andl帶參數? x86程序集

編輯:我更新了代碼,不確定是否所有的正確推理。非常困惑imull -28(%ebp)

+0

編譯是否進行優化? – 2014-11-01 19:11:24

+0

這應該是沒有優化。 – gensou 2014-11-01 19:19:46

+3

在未優化的裝配轉儲中偶爾發現無用的指令時不要感到驚訝。 – 2014-11-01 19:21:33

回答

0

AND可以採取8或32位立即,所以沒有什麼阻止你取與EAX與爲0x1

TEST設置標誌供以後使用,你不必使用它的權利遠。你不提供代碼L4,但可能有一些指令檢查
編輯:由於上述是一個未優化的版本,不要指望它是有效的。

IMUL帶有一個參數和後綴l將該參數乘以EAX。如果你想處理彙編,你需要閱讀x86指令。

+0

theres實際上在該片段之後還有2行:'cmpl $ 2,8(ebp)'jne .L6' – gensou 2014-11-01 18:10:01

0

$1不帶前導零的常數(數字)的簡寫形式。在32位環境(%eax!)中,它相當於0x00000001,意思是:將除最右邊的所有位設置爲零,讓最右邊的位不變。這裏可能用於隔離布爾值(0 = false或1 = true)或確定奇數(0 =偶數,1 =奇數)。