【汇编程序】实现判断输入的年份是否为闰年

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

程序需求:输入一个年份,判断其是否是闰年。
编程思路:被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
	

编译运行后结果如下:

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值