2012-12-07 33 views
1

的C代碼是:

int rSum(int *Start, int Count) 
{ 
     if (Count <= 0) 
     return 0; 
     return *Start + rSum(Start+1, Count-1); 
} 

相應的組裝代碼是:

.file "test98.c" 
.text 
.globl rSum 
.type rSum, @function 
rSum: 
pushl %ebp 
movl %esp, %ebp 
pushl %ebx 
subl $20, %esp 
cmpl $0, 12(%ebp) 
jg .L2 
movl $0, %eax 
jmp .L3 
.L2: 
movl 8(%ebp), %eax 
movl (%eax), %ebx 
movl 12(%ebp), %eax 
leal -1(%eax), %edx 
movl 8(%ebp), %eax 
addl $4, %eax 
movl %edx, 4(%esp) 
movl %eax, (%esp) 
call rSum 
leal (%ebx,%eax), %eax 
.L3: 
addl $20, %esp 
popl %ebx 
popl %ebp 
ret 
.size rSum, .-rSum 
.ident "GCC: (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5" 
.section .note.GNU-stack,"",@progbits 

我不明白這個Instructor作業 「subl $ 20%ESP」,以及爲什麼是$ 20〜

回答

7

subl $20, %esp從堆棧指針中減去20(esp)。這將在堆棧中分配20個字節的空間,用於局部變量。

+0

謝謝,爲什麼是20美元,而不是30美元,40美元? – lxgeek

+0

@lxgeek:如果你問爲什麼是32,那是因爲編譯器決定需要多少空間來存儲局部變量。 – icktoofay

+4

對於GAS/AT&T,'$'表示它是立即值20,而不是地址20的值。它不是十六進制。這20個字節的空間將是8個字節,用於調用'rsum'所需的參數,而不是局部變量。其餘的12個字節是不必要的,但編譯器可能會這樣做,只是爲了維護一個64字節對齊的棧(在任何SSE的情況下)。 – Brendan