汇编练习题(2)

习题2: 输入某年某月某日,判断这一天是这一年的第几天?

c++代码如下, 算法不太严谨,主要为了反汇编的练习也不讲究了

	void doFun(int year, int month, int day){

		int days =  month*30+day;
		days += month/2 + month%2;//31天的月

		if (month >= 2){
			days -= (year%4==0)?1:2;
		}

		cout << days <<endl;
	}

 

相应的汇编代码为:

Title: Hello world 
 ;Author: sld6666666@gmail.com    
 ;Data: 2012-11-29 
 ;Description: Assemble hello world
 .386                
 .model flat, stdcall    
 .stack 4096            
 option casemap:none
 


include msvcrt.inc
includelib msvcrt.lib
 
 .data                    
	year dword	2009
	month dword 5
	day dword 24
	totalDays dword 0
	
	szFmt db '%d', 0
 
 .code
     start:
  		
  		;int days =  month*30+day;
  		mov eax, 30
  		mul month
  		add eax, day
  		mov totalDays, eax
  		
  		;days += month/2 + month%2;//31ÌìµÄÔÂ
  		mov eax, month
  		cdq
  		mov ebx, 2
  		idiv ebx
  		add totalDays, ebx
  		add totalDays, edx
  		
 
 		;if (month >= 2){
		;	days -= (year%4==0)?1:2;
		;}
		
		mov  eax, month
		cmp	eax, 2
		jb printProcess
		
		mov eax, year
		cdq
		mov ebx, 4
		idiv ebx
		cmp edx, 0
		je L1
		sub totalDays, 2
		jmp printProcess
			L1:
			sub totalDays, 1
		
		printProcess:
  			invoke crt_printf, addr szFmt,totalDays
  		
        rtnProcess:
         	ret
     
     end start

  

posted @ 2012-11-30 18:03  sld666666  阅读(345)  评论(0编辑  收藏  举报