027 命令行变元
来源:互联网 发布:mac samba server 编辑:程序博客网 时间:2024/06/04 18:43
/**********************027 命令行变元******************************** * 使用,命令行变元写一个小程序cuntdown。 程序对来自命令行的值连续减 * 量,达到零时产生蜂鸣。 * C语言精彩编程百例 第27*///Countdown program#include<stdio.h>#include<stdlib.h>#include<ctype.h>#include<string.h>int main(int argc,char* argv[]){ int disp,count; if(argc<2) { printf("You must enter the length of the count\n"); printf("on the command line.Try again\n"); exit(1); } if (argc==3 && !strcmp(argv[1],"display")) disp = 1; else disp=0; for(count = atoi(argv[2]);count;--count) if(disp==1) printf("%d\n",count); putchar('\a'); printf("Down"); return 0;}
对应的汇编
.file "027.c" .def ___main; .scl 2; .type 32; .endef .text .align 32LC0: .ascii "You must enter the length of the count\12\0" .align 32LC1: .ascii "on the command line.Try again\12\0"LC2: .ascii "display\0"LC3: .ascii "%d\12\0"LC4: .ascii "Down\0" .align 2.globl _main .def _main; .scl 2; .type 32; .endef_main: pushl %ebp movl %esp, %ebp subl $24, %esp andl $-16, %esp movl $0, %eax movl %eax, -12(%ebp) movl -12(%ebp), %eax call __alloca call ___main cmpl $1, 8(%ebp) # if argc<2 jg L74 subl $12, %esp # printf pushl $LC0 call _printf addl $16, %esp subl $12, %esp # printf pushl $LC1 call _printf addl $16, %esp subl $12, %esp # exit(1) pushl $1 call _exitL74: cmpl $3, 8(%ebp) # if argc==3 jne L75 subl $8, %esp # 准备调用_strcmp pushl $LC2 # "display" movl 12(%ebp), %eax # eax=argv addl $4, %eax # eax=argv+4 pushl (%eax) # argv[1] call _strcmp addl $16, %esp # 调整堆栈 testl %eax, %eax # if strcmp jne L75 movl $1, -4(%ebp) # disp =1 jmp L76L75: movl $0, -4(%ebp) # disp =0L76: subl $12, %esp # 准备调用 atoi movl 12(%ebp), %eax # eax=argv addl $8, %eax # argv+8 pushl (%eax) # argv[2] call _atoi addl $16, %esp # 平衡堆栈 movl %eax, -8(%ebp) # count = atoi(argv[2])L77: cmpl $0, -8(%ebp) # if count jne L80 jmp L78L80: cmpl $1, -4(%ebp) # if disp=1 jne L79 subl $8, %esp # printf pushl -8(%ebp) pushl $LC3 call _printf addl $16, %espL79: leal -8(%ebp), %eax # &count decl (%eax) # count-- jmp L77L78: subl $12, %esp # putchar pushl $7 call _putchar addl $16, %esp subl $12, %esp pushl $LC4 call _printf # printf addl $16, %esp movl $0, %eax leave ret .def _putchar; .scl 2; .type 32; .endef .def _atoi; .scl 2; .type 32; .endef .def _strcmp; .scl 2; .type 32; .endef .def _exit; .scl 2; .type 32; .endef .def _printf; .scl 2; .type 32; .endef
- 命令行变元和函数的参数传递方式比较类似,32位编译器下,都是通过 8(%ebp),12(%ebp)传递的。
- char* argv[], 和 char ** argv 在这里应该是一样的,目前指向地址的指针还没开始研究,后面看见较深入的例子后再深入理解。
0 0
- 027 命令行变元
- 命令行变元的用法
- 27_命令行变元
- 命令行变元(argc,argv[])
- 主题:关于c语言命令行变元的一个实例
- 变元问题
- 默认的函数变元
- 变身淑女只需980元?
- Var-arg(可变变元列表)
- 具有可变变元列表的方法
- 5.2指针与函数变元
- Ural1042(高斯消元+枚举自由变元)
- 70_可变数目变元
- 高斯消元异或枚举变元(poj1681)
- HDOJ Lanterns 3364【高斯消元求自由变元】
- Python基础--不可变序列:元组
- poj 3185 高斯消元 枚举变元
- POJ3185 高斯消元 +枚举自由变元
- CString转char * ,string |wchar_t*和char*之间互相转换
- 黑马程序员-OC语言之多态
- node.js自学笔记(3)-异步I/O
- 开始使用Mac OS X
- 代码编译过程
- 027 命令行变元
- 设计模式1----Singleton(单态)
- OC SEL (@selector) 原理及使用总结
- Event Logging 技术简介
- (大型网站之Nginx)图解正向代理、反向代理、透明代理
- 善良比聪明更难
- 使用定时器显示下拉菜单
- Linux下的Root密码忘记破解方法
- iOS 关于block的简单使用