assembly - 我对这次 assembly 的理解正确吗?

标签 assembly gcc x86

源代码是 C 语言,我使用禁用优化 (-O0) 的 gcc 11.1 编译它。程序集的链接是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

最佳答案

关于您的??,当优化关闭时,gcc 确保每个局部变量都位于内存中,其中包括函数参数。因此,参数argc、argv尽管实际上是在寄存器edi、rsi中传递给main,但仍需要存储在内存中在堆栈上的适当位置。

当然,这对于代码的执行是完全没有用的,因为这些值永远不会被加载回来,实际上也根本不会使用这些参数。因此编译器可以删除该代码 - 但猜猜看,这将是一种优化,并且您告诉编译器不要执行任何这些操作。

尽管您可能没有想到,但在大多数情况下,阅读优化的代码比未优化的代码更具教育意义,并且不会那么困惑。

关于assembly - 我对这次 assembly 的理解正确吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68668555/

相关文章:

assembly - x86 中变量的大小

c - x86 E8和FF调用,如何找到E8移位地址 'on the fly'?基本的 x86 ASM 调用

c - fork 后重新创建死线程

c - 如何使用asm中变量的数字创建标签

c++ - 如何启动我的简单 hello world 程序?

c - 为什么 C 中的前缀递增 (++x) 比后缀递增 (x++) 快?

c - 使用 UNION 和 STRUCTURE

c - 分析生成的汇编代码以操纵命令行参数

c - 使用 native 编译器和交叉编译器生成相同的目标文件

linux - 包含文件存储在哪里 - Ubuntu Linux,GCC