今天老师讲到了可变形参,就man了下va_arg,看着郁闷,都是宏...照着va_arg的大体,自己写了个玩玩,第一个参数稍微做点修改,应该可以处理更多类型参数.
下面贴下代码:
- #include <stdio.h>
- void m_printf(int,...);
- int main(char argc,char **argv){
- m_printf(3,3323,23323,3453453);
- return 0;
- }
- void m_printf(int i,...){
- int c=0;
- __asm__ __volatile__("pushl %%esi;"::);
- while(i){
- int eax;
- __asm__ __volatile__("movl -12(%%ebp),%%esi;movl 12(%%ebp,%%esi),%0;"
- :"=r"(eax)
- :);
- printf("%d\n",eax);
- c+=4;
- i--;
- }
- __asm__ __volatile__("popl %%esi;"::);
- }
顺便也贴下她的汇编:
- .file "01.c"
- .text
- .globl main
- .type main, @function
- main:
- pushl %ebp
- movl %esp, %ebp
- andl $-16, %esp
- subl $32, %esp
- movl 8(%ebp), %eax
- movb %al, 28(%esp)
- movl $3453453, 12(%esp)
- movl $23323, 8(%esp)
- movl $7885558, 4(%esp)
- movl $3, (%esp)
- call m_printf
- movl $0, %eax
- leave
- ret
- .size main, .-main
- .section .rodata
- .LC0:
- .string "%d\n"
- .text
- .globl m_printf
- .type m_printf, @function
- m_printf:
- pushl %ebp
- movl %esp, %ebp
- subl $40, %esp
- movl $0, -12(%ebp)
- #APP
- # 12 "01.c" 1
- pushl %esi;
- # 0 "" 2
- #NO_APP
- jmp .L4
- .L5:
- #APP
- # 15 "01.c" 1
- movl -12(%ebp),%esi;movl 12(%ebp,%esi),%eax;
- # 0 "" 2
- #NO_APP
- movl %eax, -16(%ebp)
- movl $.LC0, %eax
- movl -16(%ebp), %edx
- movl %edx, 4(%esp)
- movl %eax, (%esp)
- call printf
- addl $4, -12(%ebp)
- subl $1, 8(%ebp)
- .L4:
- cmpl $0, 8(%ebp)
- jne .L5
- #APP
- # 22 "01.c" 1
- popl %esi;
- # 0 "" 2
- #NO_APP
- leave
- ret
- .size m_printf, .-m_printf
- .ident "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"
- .section .note.GNU-stack,"",@progbits
阅读(987) | 评论(0) | 转发(0) |