gpt4 book ai didi

c - gcc 版本 4.6.3 上的 c 程序汇编版本

转载 作者:行者123 更新时间:2023-11-30 15:12:35 25 4
gpt4 key购买 nike

我用c语言编写了一个小型数组程序

#include <stdio.h>
int main()
{
int arr[]={1,2,3,4,5};//int size is 4, elements 5 so size of array = 4*5 = 20
printf("%d\n", sizeof(arr));
return 0;
}

我用

编译了这个
gcc -O2 -fverbose-asm -S -c arr_n_pointer_confusion.c 

我明白了,

    .section    .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "%d\n"
.section .text.startup,"ax",@progbits
.p2align 4,,15
.globl main
.type main, @function
main:
.LFB22:
.cfi_startproc
pushl %ebp #
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp #,
.cfi_def_cfa_register 5
andl $-16, %esp #,
subl $16, %esp #,
movl $20, 8(%esp) #,
movl $.LC0, 4(%esp) #,
movl $1, (%esp) #,
call __printf_chk #
xorl %eax, %eax #
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
.LFE22:
.size main, .-main
.ident "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3"
.section .note.GNU-stack,"",@progbits

任何人都可以将 C 语言中的汇编步骤联系起来吗?我之所以尝试这样做是为了了解一点汇编代码,以便我能够理解从指针到数组的区别。

最佳答案

基本上,因为编译器在编译时就知道数组的内容,所以它可以删除数组,只需将 sizeof(array) 替换为 20,而不必在实际初始化数组运行时。

.cfi_startproc
pushl %ebp # Save the value of ebp (points to the base of the stack) in the stack
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp #, Set the value of the base of the stack to the top of it
.cfi_def_cfa_register 5
andl $-16, %esp # Align the stack with a 16-byte boundary, used for SIMD instructions
subl $16, %esp # Subtract 16 from the value of the stack pointer (reserving 16 bytes of space for the stack)
movl $20, 8(%esp) # Set the memory 8 bytes above the stack as '20'
movl $.LC0, 4(%esp) # Move the string "%d\n" 4 bytes above the stack
movl $1, (%esp) # Set the flag for __printf_chk() to 1, enabling stack overflow checks
call __printf_chk # Print our string
xorl %eax, %eax # Zero out the eax register (i.e. store the return code in eax, which is 0)

。因此传递给 __printf_chk 的参数是 (1, "%d\n", 20)

关于c - gcc 版本 4.6.3 上的 c 程序汇编版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34979827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com