源代码是C语言的,我使用禁用优化(- 0)的gcc 11.1编译它。该程序集的链接在这里,因此您可以自己查看。
我用我认为正在发生的事情和"??"注释了程序集。对于我不太确定的行
现在,我只关心main()
和someAlgebra()
,因此,我只在程序集清单中注意到这些位。
C源
#include <stdio.h>
const char *MY_LIST[] = {
"of",
"the",
"four",
"green",
"houses",
"one",
"hides",
"five",
"amazing",
"secrets"
};
int someAlgebra(int x, int y)
{
int a = 4;
int b = 3;
return 2*x + 3*y + a - b;
}
void printAll(const char *the_list[], unsigned length)
{
for(unsigned i = 0; i < length; i++) {
puts(the_list[i]);
}
}
int main(int argc, char *argv[])
{
int k = someAlgebra(3, 5);
// printf("Size of [int] (bytes): %un", sizeof(int));
// printf("Size of [int *] (bytes): %un", sizeof(int *));
return 0;
}
<<p>装配/strong>.LC0:
.string "of"
.LC1:
.string "the"
.LC2:
.string "four"
.LC3:
.string "green"
.LC4:
.string "houses"
.LC5:
.string "one"
.LC6:
.string "hides"
.LC7:
.string "five"
.LC8:
.string "amazing"
.LC9:
.string "secrets"
MY_LIST:
.quad .LC0
.quad .LC1
.quad .LC2
.quad .LC3
.quad .LC4
.quad .LC5
.quad .LC6
.quad .LC7
.quad .LC8
.quad .LC9
someAlgebra:
push rbp ;save caller frame pointer
mov rbp, rsp ;set frame pointer for this procedure
mov DWORD PTR [rbp-20], edi ;store param #1 (3)
mov DWORD PTR [rbp-24], esi ;store param #2 (5)
mov DWORD PTR [rbp-4], 4 ;store int a (local var)
mov DWORD PTR [rbp-8], 3 ;store int b (local var)
mov eax, DWORD PTR [rbp-20] ;Math in function body
lea ecx, [rax+rax] ;Math in function body
mov edx, DWORD PTR [rbp-24] ;Math in function body
mov eax, edx ;Math in function body
add eax, eax ;Math in function body
add eax, edx ;Math in function body
lea edx, [rcx+rax] ;Math in function body
mov eax, DWORD PTR [rbp-4] ;Math in function body
add eax, edx ;Math in function body
sub eax, DWORD PTR [rbp-8] ;Math in function body
pop rbp ;restore caller frame pointer
ret ;pop return address into the PC
printAll:
push rbp
mov rbp, rsp
sub rsp, 32
mov QWORD PTR [rbp-24], rdi
mov DWORD PTR [rbp-28], esi
mov DWORD PTR [rbp-4], 0
jmp .L4
.L5:
mov eax, DWORD PTR [rbp-4]
lea rdx, [0+rax*8]
mov rax, QWORD PTR [rbp-24]
add rax, rdx
mov rax, QWORD PTR [rax]
mov rdi, rax
call puts
add DWORD PTR [rbp-4], 1
.L4:
mov eax, DWORD PTR [rbp-4]
cmp eax, DWORD PTR [rbp-28]
jb .L5
nop
nop
leave
ret
main:
push rbp ;save contents of rbp to stack
mov rbp, rsp ;set frame pointer
sub rsp, 32 ;reserve 32 bytes for local vars ??
mov DWORD PTR [rbp-20], edi ;??
mov QWORD PTR [rbp-32], rsi ;??
mov esi, 5 ;param #2 for someAlgebra()
mov edi, 3 ;param #1 for someAlgebra()
call someAlgebra ;push return address to stack
mov DWORD PTR [rbp-4], eax ;get return value from someAlgebra()
mov eax, 0
leave
ret
关于你的??S,当优化关闭时,GCC确保每个局部变量都存在内存中,其中包括函数参数。因此,参数argc, argv
,尽管它们在寄存器edi, rsi
中传递给main
,需要存储在内存中堆栈上的适当位置。
当然,这对于代码的执行是完全无用的,因为这些值永远不会被加载回来,这些参数也根本不会被使用。所以编译器可以删除这些代码——但你猜怎么着,这将是一个优化,你告诉编译器不要做任何这些。
虽然你可能不这么认为,但在大多数情况下,阅读优化后的代码比阅读未优化的代码更有教育意义,更少令人困惑。