#include <stdio.h>
int main(int argc, char * argv[])
{
argv[1][2] = 'A';
return 0;
}
以下是来自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
前导码之前的代码开始时发生了什么?根据以下代码,argv存储在哪里?在堆栈上?
它可能会帮助你编译时没有优化,'-O0' – IanNorton 2012-02-27 21:54:52
'和l -16,%esp'←在我看来像是将堆栈对齐到16个字节 – ninjalj 2012-02-27 21:57:49
这可能有助于思考'andl $ -16 ,X'为'X&0xFFFFFFF0'。 – user7116 2012-02-27 21:59:04