程序需求:输入一个年份,判断其是否是闰年。
编程思路:被4整除且不被100整除,或者被400整除的年份就是闰年,C语言中通过%来进行取余运算,汇编中通过DIV指令。
开发环境
Win10 + VS2017
C语言代码实现如下:
#include <stdio.h>
int year = 0;
int main()
{
printf("please input the year.\n");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
printf("%d is leap year.\n", year);
else printf("%d is not leap year.\n", year);
return 0;
}
汇编语言代码实现如下:
INCLUDELIB kernel32.lib
INCLUDELIB ucrt.lib
INCLUDELIB legacy_stdio_definitions.lib
.386
.model flat,stdcall
ExitProcess PROTO,
dwExitCode:DWORD
printf PROTO C : dword,:vararg
scanf PROTO C : dword,:vararg
.data
sformat byte '%d',0
msg byte 'please input the year.',10,0
msg1 byte '%d is leap year.',10,0
msg2 byte '%d is not leap year.',10,0
year dword 0
.code
main Proc
invoke printf,offset msg
invoke scanf,offset sformat,offset year
mov eax,dword ptr year
mov ebx,4
cdq
div ebx
cmp edx,0
jne next
mov eax,dword ptr year
mov ebx,100
cdq
div ebx
cmp edx,100
je next
invoke printf,offset msg1,dword ptr year;success
jmp over
next:
mov eax,dword ptr year
mov ebx,400
cdq
div ebx
cmp edx,0
jne next2
invoke printf,offset msg1,dword ptr year;success
jmp over
next2:
invoke printf,offset msg2,dword ptr year;unsuccess
over:
push 0h
call ExitProcess
main endp
end main
编译运行后结果如下:

该博客介绍了如何利用汇编程序来判断输入的年份是否为闰年。根据编程思路,年份能被4整除且不被100整除,或者能被400整除的年份被视为闰年。文章提供了开发环境(Win10 + VS2017)以及C语言和汇编语言的代码实现,并展示了编译运行后的结果。
5001

被折叠的 条评论
为什么被折叠?



