Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Peter Norton Programmer's Guide - Norton Guide http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]

  For our assembly-language example in this section, we've chosen something
  rather interesting. It's a routine used within the Norton Utility
  programs, so you'll be seeing some actual production code.

  The purpose of this routine is to calculate the day of the week for any
  day within DOS's working range, which is stated to be from Tuesday,
  January 1, 1980, through Thursday, December 31, 2099. Occasionally, it's
  valuable for a program to be able to report the day of the week, either
  for the current date or for any other date that may be in question. For
  example, DOS keeps track of the date and time each file was last changed.
  Because people often use this information to find out when they last
  worked with a file, it can be handy to know the day of the week as well.
  In fact, the day of the week is often more immediately meaningful than the
  actual date.

  Although several interesting and clever algorithms let you calculate the
  day of the week, the actual work of writing a day-of-the-week program is
  usually rather tedious. Beginning with version 1.10, DOS incorporated a
  day-of-the-week calculation, which spared us the chore of writing our own.
  DOS's routine is available only in a form that reports the current day of
  the week, but that is no obstacle: We can temporarily change DOS's date to
  the date we're interested in and then have DOS report the day of the week.
  That is what the following assembly-language routine does for us.

  Besides being slightly foxy, this routine is interesting because it
  illustrates how three DOS function calls operate together to produce one
  result. It also illustrates the minor intricacies involved in saving and
  restoring things on the stack. As we will see here, stack use occasionally
  has to be carefully orchestrated so that different values don't get in
  each others' way.

  This particular subroutine, named Weekday, is set up in the form needed
  for use with the Microsoft C compiler. The routine is called with three
  integer variables, which specify the month, day, and year we are
  interested in. The routine returns the day of the week in the form of an
  integer in the range of 0 through 6 (signifying Sunday through Saturday).
  This conforms to the C language convention for arrays, providing an index
  to an array of strings that give the names of the days. Therefore, we
  could use this subroutine in this way:

  DayName[ Weekday( month, day, year ) ]

  It is important to note that this routine works blindly with the date,
  checking neither for a valid date nor for the range of dates accepted by
  DOS. Here is the subroutine:

  _TEXT           SEGMENT byte public 'CODE'
                  ASSUME  cs:_TEXT

                  PUBLIC  _Weekday
  _Weekday        PROC    near

                  push    bp              ; establish stack addressing ..
                  mov     bp,sp           ; .. through BP

                  mov     ah,2Ah          ; get current date
                  int     21h

                  push    cx              ; save current date on the stack
                  push    dx

                  mov     cx,[bp+8]       ; CX = year
                  mov     dl,[bp+6]       ; DL = day
                  mov     dh,[bp+4]       ; DH = month

                  mov     ah,2Bh          ; set the date specified
                  int     21h

                  mov     ah,2Ah          ; get the date back from DOS
                  int     21h             ; (AL = day of the week)

                  pop     dx              ; restore the current date ..
                  pop     cx              ; .. in CX and DX
                  push    ax              ; save day of week on the stack

                  mov     ah,2Bh          ; set the current date
                  int     21h

                  pop     ax              ; AL = day of week
                  mov     ah,0            ; AX = day of week

                  pop     bp              ; restore BP and return
                  ret

  _Weekday        ENDP

  _TEXT           ENDS

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson