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

Bru*_*uce 3 c x86 assembly

#include <stdio.h>
int main(int argc, char * argv[])
{
 argv[1][2] = 'A';
 return 0;
}
Run Code Online (Sandbox Code Playgroud)

以下是GCC针对32位Intel架构的相应汇编代码.我无法完全理解发生了什么.

main:
        leal    4(%esp), %ecx  - Add 4 to esp and store the address in ecx
        andl    $-16, %esp  - Store first 28 bits from esp's address into esp??
        pushl   -4(%ecx)  - Push the old esp on stack
        pushl   %ebp         - Preamble
        movl    %esp, %ebp
        pushl   %ecx          - push old esp + 4 on stack
        movl    4(%ecx), %eax   - move ecx + 4 to eax. this is the address of argv. argc stored at (%ecx).
        addl    $4, %eax - argv[1]
        movl    (%eax), %eax - argv[1][0]
        addl    $2, %eax  - argv[1][2]
        movb    $65, (%eax) - move 'A'
        movl    $0, %eax - move return value (0)
        popl    %ecx - get old value of ecx
        leave
        leal    -4(%ecx), %esp  - restore esp
        ret
Run Code Online (Sandbox Code Playgroud)

在序言之前的代码开头发生了什么?根据以下代码,argv存储在哪里?在堆栈上?

Nec*_*lis 5

您看到的有趣代码(前两行)是堆栈对齐为16个字节(与x -16相同~15,并将x & ~15x舍入为16的倍数).

argv将被存储在ESP + 8进入功能时,什么leal 4(%esp), %ecx作用是创建一个指针,指向包含一个伪结构argcargv,那么它前进到从那里访问它们.从这个伪结构movl 4(%ecx), %eax访问argv.