【问题标题】:Is my understanding of this assembly correct?我对这个程序集的理解正确吗?
【发布时间】:2021-08-05 14:30:23
【问题描述】:

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

【问题讨论】:

    标签: assembly gcc x86


    【解决方案1】:

    关于你的 ??s,当优化关闭时,gcc 确保每个局部变量都存在于内存中,其中包括函数参数。所以参数argc, argv,尽管它们在寄存器edi, rsi中被传递给main,但需要存储在堆栈上适当位置的内存中。

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

    虽然您可能不这么认为,但在大多数情况下,阅读优化的代码比未优化的代码更具教育意义,也不会令人困惑。

    【讨论】:

    • @Mode77:是的,就他们而言。
    • 标准允许编译器通过与“高级汇编程序”一致的方式来扩展语言的语义,而不管标准是否要求他们这样做;如果有一个术语来描述那些旨在产生与这种语义一致的代码的优化,而不是那些旨在产生仅在标准规定的情况下才有意义的代码的优化,那将会很有帮助。
    猜你喜欢
    • 2021-01-13
    • 1970-01-01
    • 2020-02-09
    • 2015-04-07
    • 2012-01-27
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多