Assembly code snippets

Back to the snippets overview

Details

TitleDoY (Day of Year)
Authormicmic
Submitted by:micmic
Date added:2002-03-18 15:31:32
Date modified:2002-03-18 16:07:54

Comments

This code calculates the DoY (Day of Year, 1-366) WITHOUT accessing any hardcoded values or variables. Might be useful for calendar applications. It takes into account leap years, according to the Gregorian calendar rules (years divisible by 4 are leap, but years divisible by 100 are not leap, unless they are also divisible by 400), so it will produce correct results for any date in the Gregorian calendar (after 1582 AD, or whatever year the Gregorian calendar was adopted in your country). Expects the year (>1581) in eax, the month (1-12) in ecx and the day (1-31) in edx. Does not check for date validity. Preserves Windows registers. Currently I've tried to use as few instructions as possible, so speed or size optimizations are possible. Returns the DoY in eax.

Snippet

      push  ebx
      dec   ecx
      mov   ebx, ecx
      imul  ecx, 30
      test  ebx, 9
      jnz   @F
      dec   ecx
@@:   shr   ebx, 1
      jnz   @F
      add   ecx, 2
@@:   add   ecx, ebx
      cmp   ecx, 32
      lea   ecx, [ecx+edx-1]
      jna   Done
      test  eax, 3
      jnz   Done
      xor   edx, edx
      mov   ebx, 100
      div   ebx
      cmp   edx, 0
      jnz   @F
      and   eax, 3
      jnz   Done
@@:   inc   ecx
Done: mov   eax, ecx
      pop   ebx