我对这个程序集的理解是否正确?

Is my understanding of this assembly correct?

源代码在 C 中,我使用禁用优化 (-O0) 的 gcc 11.1 编译它。程序集的 link 是 here 所以你可以自己看看。

我已经用我认为正在发生的事情和“??”对程序集进行了注释。对于我不太确定的台词。

现在,我只关心 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): %u\n", sizeof(int));
    // printf("Size of [int *] (bytes): %u\n", sizeof(int *));
    return 0;
}

程序集

.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,但仍需要存储在内存中堆栈上适当的位置。

当然这对代码的执行完全没有用,因为这些值永远不会被加载回来,实际上也根本不会使用那些参数。所以编译器可以删除该代码 - 但你猜怎么着,那是一种优化,而你告诉编译器不要做任何这些。

尽管您可能不这么认为,但在大多数情况下,阅读优化后的代码比未优化的代码更有教育意义,也更少混淆。