/*************************017.c exit()函数************************
* C 语言编程百例的第17个例子:
*输入月数,程序打印出1999年的该月有几天。
*/
#include<stdlib.h>
#include<stdio.h>
void main()
{
//定义变量month和day存放月数和天数
int month;
int day;
printf("please input the month number:");
scanf("%d",&month);
switch(month)
{
//当输入为1、3、5、7、8、10和12月时:
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day=31;
break; //每天天数都是31天,跳出循环
//当输入为4、6、9、和11月时
case 4:
case 6:
case 9:
case 11:
day=30;
break; //每天都是30天,跳出循环
case 2:
day=28;
break;
default:
exit(0);
}
printf("1999.%d has %d days.\n",month,day);
}
使用如下编译器:
对应的汇编
.file "017.c"
.def ___main; .scl 2; .type 32; .endef
.text
.align 32
LC0:
.ascii "please input the month number:\0"
LC1:
.ascii "%d\0"
LC2:
.ascii "1999.%d has %d days.\12\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
subl $12, %esp #printf("please input the month number:");
pushl $LC0
call _printf
addl $16, %esp
subl $8, %esp #scanf("%d",&month);
leal -4(%ebp), %eax
pushl %eax
pushl $LC1
call _scanf
addl $16, %esp
cmpl $12, -4(%ebp) # month和12 比
ja L28 # 大于调到 exit(0)
movl -4(%ebp), %eax # eax=month
sall $2, %eax # eax =eax* 4 (跳转编号4位对其)
movl L29(%eax), %eax # eax = L29:%eax
jmp *%eax
.p2align 2
.align 4
L29:
.long L28
.long L22
.long L27
.long L22
.long L26
.long L22
.long L26
.long L22
.long L22
.long L26
.long L22
.long L26
.long L22
L22:
movl $31, -8(%ebp)
jmp L15
L26:
movl $30, -8(%ebp)
jmp L15
L27:
movl $28, -8(%ebp)
jmp L15
L28:
subl $12, %esp #调用exit函数,
pushl $0
call _exit #退出程序
L15:
subl $4, %esp
pushl -8(%ebp)
pushl -4(%ebp)
pushl $LC2
call _printf
addl $16, %esp
leave
ret
.def _exit; .scl 2; .type 32; .endef
.def _scanf; .scl 2; .type 32; .endef
.def _printf; .scl 2; .type 32; .endef