天天看点

【汇编程序】实现输出2012-2100年之间所有闰年

程序需求:能被4整除但不能被100整除,或者年被400整除的年份是闰年。编程写一个完整的程序,求出2012年~2099年中的所有闰年年份,把它们存放在数组Lyear中并输出到屏幕上。

编程思路:汇编中ESI用来做年份计数器,ECX用来做闰年个数计数器,用DIV指令来求余数。

开发环境

Win10 + VS2017

C语言代码实现如下:

#include <stdio.h>
int main()
{
	printf("Leap year is follow:\n");
	for (int i = 2012; i < 2100; i++)
	{
		if ((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0))
			printf("%d\t",i);
	}
	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
Lyear dword 25 dup(0)
msg byte 'Leap year is follow:',10,0
format byte '%d',9,0

.code
main Proc
	xor ecx,ecx
	mov esi,2012
	jmp testing
body:
	mov eax,esi
	mov ebx,4
	cdq
	div ebx
	cmp edx,0
	jne next
	mov eax,esi
	mov ebx,100
	cdq
	div ebx
	cmp edx,0
	je next
	mov dword ptr Lyear[ecx*4],esi
	inc ecx
	jmp over
next:
	mov eax,esi
	mov ebx,400
	cdq
	div ebx
	cmp edx,0
	jne over
	mov dword ptr Lyear[ecx*4],esi
	inc ecx
over:
	inc esi
testing:
	cmp esi,2100
	jl body
	
	pushad
	invoke printf,offset msg
	popad
	xor esi,esi
again:
	pushad
	invoke printf,offset format,dword ptr Lyear[esi*4]
	popad
	inc esi
	loop again

	push 0h
	call ExitProcess
main endp
end main
           

编译运行后结果如下:

【汇编程序】实现输出2012-2100年之间所有闰年

继续阅读