我在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中的汇编步骤联系起来。为什么我要这样做是为了理解汇编代码,以便我能理解指针到数组的区别。
答案 0 :(得分:1)
基本上,因为编译器在编译时知道数组的内容,所以它可以删除数组,只需用20替换sizeof(array)
,而不必在运行时实际初始化数组。
.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)
。
答案 1 :(得分:0)
与gcc -g -fverbose-asm -S arr_n_pointer_confusion.c
汇编。然后编译器添加.loc
伪语句,这些语句引用源文件中的行号。然后,您可以轻松地将汇编程序源与C代码关联起来。