程序需求:输入一个年份,判断其是否是闰年。
编程思路:被4整除且不被100整除,或者被400整除的年份就是闰年,C语言中通过%来进行取余运算,汇编中通过DIV指令。
开发环境
Win10 + VS
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.libINCLUDELIB ucrt.libINCLUDELIB legacy_stdio_definitions.lib.386.model flat,stdcallExitProcess PROTO,dwExitCode:DWORDprintf PROTO C : dword,:varargscanf PROTO C : dword,:vararg.datasformat byte '%d',0msg byte 'please input the year.',10,0msg1 byte '%d is leap year.',10,0msg2 byte '%d is not leap year.',10,0year dword 0.codemain Procinvoke printf,offset msginvoke scanf,offset sformat,offset yearmov eax,dword ptr yearmov ebx,4cdqdiv ebxcmp edx,0jne nextmov eax,dword ptr yearmov ebx,100cdqdiv ebxcmp edx,100je nextinvoke printf,offset msg1,dword ptr year;successjmp overnext:mov eax,dword ptr yearmov ebx,400cdqdiv ebxcmp edx,0jne next2invoke printf,offset msg1,dword ptr year;successjmp overnext2:invoke printf,offset msg2,dword ptr year;unsuccessover:push 0hcall ExitProcessmain endpend main