之前研究过变参函数的实现,再看看具体printf函数的压栈顺序。
看看之前写的汇编中利用call printf来完成字符的输出
.section .data
output:
.asciz "the processor vendor ID is '%s'\n"
.section .bss
.lcomm buffer,12
.section .text
.globl _start
_start:
movl $0,%eax
cpuid
movl $buffer,%edi
movl %ebx,(%edi)
movl %edx,4(%edi)
movl %ecx,8(%edi)
pushl $buffer
pushl $output
call printf
addl $8,%esp
pushl $0
call exit
看看压栈的顺序,print出来的顺序正好符合压栈出栈的顺序
再看看c代码
#include <stdio.h>
int main()
{
int a = 3;
printf("%d,%d\n",a++,a++);
printf("\n%d",a);
return 0;
}
在vs2008运行
4 3
5
这说明printf先执行的是右边的a++,
实际就是先对右边的a++进行压栈,再对左边压栈,这样输出是先输出左边的值后输出右边的值