;输入4位数字,判断是否是闰年
data SEGMENT
enter DB 0ah,0dh,'$'
noleap DB 'the year is not leap year!$'
leap DB 'the year is leap year!$'
no DB 'please input number between 0-9!$'
tip DB 'please input the number between 1000~9999:$'
data ENDS
code segment
assume cs:code,ds:data
main proc far
repeat: call input ;调用输入的子程序
LEA DX,enter ;换行
MOV AH,09h
INT 21h
call judge ;调用判断闰年的子程序
LEA DX,enter ;换行
MOV AH,09h
INT 21h
jmp repeat
main endp
input proc near
mov bx,0 ;初始化BX,BX用来存放输入的4位数
mov cx,4 ;cx计数,表示输入4个数字
MOV AX,data
MOV DS,AX
LEA DX,tip ;显示输入信息提示
MOV AH,09h
INT 21h
LEA DX,enter ;换行
MOV AH,09h
INT 21h
newchar:
mov ah,1
int 21h
CMP AL,71h
JZ exitdos ;当输入的是q时退出演示
sub al,30h
jnl noless ;满足大于等于0就跳转
LEA DX,enter ;换行
MOV AH,09h
INT 21h
lea dx,no ;小于0就输出提示信息
MOV AH,09h
INT 21h
LEA DX,enter ;换行
MOV AH,09h
INT 21h
jnz newchar ;继续输入数字
noless:
cmp al,9d ;比较是否小于9
jng nogreater ;满足数字条件就跳转
LEA DX,enter ;换行
MOV AH,09h
INT 21h
lea dx,no ;不满足就输出提示信息
MOV AH,09h
INT 21h
LEA DX,enter ;换行
MOV AH,09h
INT 21h
jnz newchar
nogreater: ;把输入的内容存入bx中保存
cbw
xchg ax,bx
mov dx,10d
mul dx
xchg ax,bx
add bx,ax
dec cx ;每输入完一个就减去1
jnz newchar
exit:
ret
exitdos:
MOV AH,4ch ;返回dos
INT 21h
input endp
judge proc near
mov ax,bx
cwd ;转换为双字节
mov cx,04d
div cx ;先除以4
cmp dx,0
jz goon ;若可以被4整除,则跳转,看是否被100整除
LEA DX,noleap ;若不能被4整除,则不是闰年
MOV AH,09h
INT 21h
ret
goon:
mov ax,bx
cwd
mov cx,100d
div cx ;除以100
cmp dx,0
jz go ;若可以被100整除,则跳转,看是否被400整除
LEA DX,leap ;没跳说明可以被4整除,但是不能被100整除,为闰年
MOV AH,09h
INT 21h
ret
go:
mov ax,bx
cwd
mov cx,400d
div cx ;除以400
cmp dx,0
jnz gogo ; 若不能整除,则跳转,输出不是闰年
LEA DX,leap ;不跳转,说明能被400整除,是闰年
MOV AH,09h
INT 21h
gogo:
LEA DX,noleap
MOV AH,09h
INT 21h
ret
judge endp
code ENDS
END main