home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / MSDOS / PKTDRVR / PDTST217.ZIP / PDCLKSRC / SETTIME.ASM < prev    next >
Encoding:
Assembly Source File  |  1994-03-26  |  12.2 KB  |  585 lines

  1. ;========================================================================
  2.  
  3. ; Copyright (C) 1991-94 by Jan.Engvald@ldc.lu.se, see file COPYING.
  4.  
  5. ;************************************************************************
  6. ;*        SearchAndSub
  7. ;*
  8. ;* Find lowest index table entry with value (in seconds) <= DX,AX
  9. ;* and subtract off table value from DX,AX.
  10. ;************************************************************************
  11.  
  12.         assume    ds:code_s
  13.  
  14. SearchAndSub    proc    near
  15.         call    BinSearch
  16.         call    AfterSearch
  17.         ret
  18. SearchAndSub    endp
  19.  
  20.  
  21.  
  22. BinSearch    proc    near
  23.         push    bx
  24.         mov    bx,cx            ; compute power of 2
  25.         mov    cx,4            ;   table size
  26.   BinLenLoop:
  27.         shl    cx,1
  28.         cmp    cx,bx
  29.         jb    BinLenLoop
  30.  
  31.         mov    bx,cx            ; start binary search
  32.   BinLess:
  33.         sub    bx,cx
  34.   BinGreater:
  35.         shr    cx,1
  36.         cmp    cx,4
  37.         jb    BinDone         ; no match return
  38.  
  39.         add    bx,cx
  40.         cmp    dx,2[si+bx]
  41.   BinEval:
  42.         ja    BinGreater
  43.         jb    BinLess
  44.  
  45.         cmp    ax,[si+bx]
  46.         ja    BinGreater
  47.         jb    BinLess
  48.   BinDone:
  49.         mov    cx,bx            ; save table index
  50.         lea    si,[si+bx]        ;   and table entry address
  51.         pop    bx
  52.         ret
  53. BinSearch    endp
  54.  
  55.  
  56.  
  57. AfterSearch    proc    near
  58.         sub    ax,[si]         ; subtract seconds
  59.         sbb    dx,2[si]        ;   by table value
  60.         shr    cx,1            ; calculate table index
  61.         shr    cx,1
  62.         ret
  63. AfterSearch    endp
  64.  
  65.  
  66.  
  67. ;************************************************************************
  68. ;*        DateTimeCalc
  69. ;************************************************************************
  70.  
  71. DateTimeCalc    proc    near
  72.         mov    ax,bx
  73.         mov    dx,cx
  74.         push    si
  75.         push    ax            ; save seconds since
  76.         push    dx            ;   year 1900
  77.  
  78.         mov    si,offset Ytab
  79.         mov    cx,4*64
  80.         call    SearchAndSub        ; find and subtract years
  81.         add    cx,1964
  82.         mov    cYear,cx        ; set year
  83.  
  84.         push    ax            ; reminder still too big
  85.         push    dx            ;   for 16 bits,
  86.         shr    dx,1            ;   have to
  87.         rcr    ax,1            ;   divide by 2
  88.         div    m12hour
  89.         inc    ax
  90.         mov    cDayOfYear,ax        ; set day within year
  91.  
  92.         pop    dx            ; restore seconds within year
  93.         pop    ax
  94.  
  95.         and    cl,011b         ; extract leap year index
  96.         mov    cNonLeapYear,cl
  97.         mov    si,offset Motab
  98.         jnz    NoLeapYear
  99.         mov    si,offset Mltab
  100.   NoLeapYear:
  101.         mov    cx,4*16
  102.         call    SearchAndSub        ; find and subtract month
  103.         inc    cx
  104.         mov    cMonth,cx        ; set month within year
  105.  
  106.         shr    dx,1            ; reminder still too big
  107.         rcr    ax,1            ;   for 16 bits, have to
  108.         rcr    bx,1            ;   divide by 2
  109.         div    m12hour         ; divide by 24hr/2
  110.         inc    ax
  111.         mov    cDay,ax         ; set day within month
  112.  
  113.         mov    ax,dx
  114.         xor    dx,dx
  115.         shl    bx,1            ; recover lost bit
  116.         rcl    ax,1            ;   into reminder
  117.         rcl    dx,1
  118.         div    mhour            ; divide by 3600
  119.         mov    cHour,ax        ; set hour within day
  120.  
  121.         mov    ax,dx
  122.         div    m60b            ; divide by 60
  123.         mov    dl,ah
  124.         xor    dh,dh
  125.         mov    cSecond,dx        ; set second within minute
  126.         xor    ah,ah
  127.         mov    cMinute,ax        ; set minute within hour
  128.  
  129.         pop    dx            ; restore seconds since
  130.         pop    ax            ;   year 1900
  131.         shr    dx,1            ; too big for 16 bits...
  132.         rcr    ax,1
  133.         div    m12hour         ; calculate days since 1900
  134.         inc    ax
  135.         xor    dx,dx            ; calculate weeks since 1900
  136.         div    m7
  137.         mov    cWday,dx        ; set weekday
  138.         pop    si
  139.         ret
  140. DateTimeCalc    endp
  141.  
  142.  
  143.  
  144. ;************************************************************************
  145. ;*        SetTime
  146. ;************************************************************************
  147.  
  148. SetTime     proc    near
  149.         push    cs
  150.         push    cs
  151.         pop    ds
  152.         pop    es
  153.         cld
  154.                         ; convert from network byte
  155.         mov    cx,cTime        ;   order
  156.         xchg    ch,cl
  157.         mov    bx,cTime+2
  158.         xchg    bh,bl
  159.  
  160.         mov    dx,tzoffset+2        ;   and apply time zone
  161.         xchg    dh,dl
  162.         sub    bx,dx
  163.         mov    ax,tzoffset        ;   offset
  164.         xchg    ah,al
  165.         sbb    cx,ax
  166.  
  167.         mov    ax,AlterTime        ; add alter-days
  168.         imul    m6hour
  169.         shl    ax,1
  170.         rcl    dx,1
  171.         shl    ax,1
  172.         rcl    dx,1
  173.         add    bx,ax
  174.         adc    cx,dx
  175.  
  176.         mov    ax,AlterTime+2        ; add alter-time
  177.         mov    dx,AlterTime+4
  178.         add    bx,ax
  179.         adc    cx,dx
  180.  
  181.         mov    si,AlgPtr
  182.         or    si,si            ; any daylight algorithm?
  183.         jnz    DoDls
  184.         jmp    SetNoDls
  185.   DoDls:
  186.         push    bx
  187.         push    cx
  188.         add    si,4
  189.         lodsw                ; from switching time
  190.         imul    m1
  191.         sub    bx,ax
  192.         sbb    cx,dx
  193.  
  194.         call    DateTimeCalc        ; dst start evaluation
  195.  
  196.         mov    ax,AlgEntryLen
  197.  
  198.         cmp    si,offset AlgTab.AEE6    ; Europe 1996 algorithm switch?
  199.         ja    SetNotEuSw
  200.         cmp    cYear,1996
  201.         jb    SetNotEuSw
  202.         add    si,ax
  203.   SetNotEuSw:
  204.  
  205.         cmp    si,offset AlgTab.Apa0    ; four entry part?
  206.         jl    SingleEntry
  207.  
  208.         mul    cNonLeapYear
  209.         add    si,ax
  210.         cmp    si,offset AlgTab.Ais0    ; acyclic entry?
  211.         jb    SingleEntry
  212.  
  213.         cmp    cYear,WARNYEAR
  214.         jb    SingleEntry
  215.  
  216.         mov    MsgTermStop,' '        ; activate warning msg
  217.   SingleEntry:
  218.         mov    bp,cDayOfYear
  219.         lodsw                ; from switching weekday
  220.         cmp    al,SAT
  221.         ja    FromDate
  222.         dec    bp
  223.         cmp    ax,cWday
  224.         jg    CurWeekF
  225.         add    bp,7
  226.   CurWeekF:
  227.         sub    bp,cWday
  228.         add    bp,ax
  229.   FromDate:
  230.         pop    cx
  231.         pop    bx
  232.         push    bx
  233.         push    cx
  234.         lodsw                ; from switching week
  235.  
  236.         push    ax
  237.         lodsw                ; until switching time
  238.         imul    m1
  239.         sub    bx,ax
  240.         sbb    cx,dx
  241.  
  242.         call    DateTimeCalc        ; dst end evaluation
  243.  
  244.         mov    di,cDayOfYear
  245.         pop    dx
  246.  
  247.         lodsw                ; until switching weekday
  248.         cmp    al,SAT
  249.         ja    UntilDate
  250.         dec    di
  251.         cmp    ax,cWday
  252.         jg    CurWeekU
  253.         add    di,7
  254.   CurWeekU:
  255.         sub    di,cWday
  256.         add    di,ax
  257.   UntilDate:
  258.         lodsw                ; until switching week
  259.         test    cNonLeapYear,011b
  260.         jnz    SetNotLeap
  261.         cmp    ax,31+28
  262.         jbe    SetChkLU
  263.         inc    ax
  264.   SetChkLU:
  265.         cmp    dx,31+28
  266.         jbe    SetNotLeap
  267.         inc    dx
  268.   SetNotLeap:
  269.         pop    cx
  270.         pop    bx
  271.         cmp    ax,dx            ; north or south of equator?
  272.         jl    SetSouth
  273.         cmp    dx,bp            ; to do or not to do dls?
  274.         jg    SetNoDls
  275.         jmp    short SetChkUntil
  276.   SetSouth:
  277.         cmp    dx,bp            ; to do or not to do dls?
  278.         jle    DoDLSaving
  279.   SetChkUntil:
  280.         cmp    di,ax
  281.         jge    SetNoDls
  282.   DoDLSaving:
  283.         or    GenFlags,DSTNOW
  284.         lodsw                ; dst advance time
  285.         mov    DstAdvance,ax
  286.         imul    m1
  287.         add    bx,ax
  288.         adc    cx,dx
  289.   SetNoDls:
  290.         call    DateTimeCalc        ; calculate date and time
  291.  
  292.         test    Flagword,DONT_SETTIME    ; did we use alter argument?
  293.         jz    SetPCtime
  294.  
  295.         mov    di,offset msgset
  296.         mov    cx,12
  297.         mov    al,' '
  298.         rep    stosb            ; clear "Clock set to"
  299.         jmp    short SkipTimeset
  300.   SetPCtime:
  301.         mov    cx,cYear
  302.         mov    dh,byte ptr cMonth
  303.         mov    dl,byte ptr Cday
  304.         mov    ah,2Bh            ; set date
  305.         int    21h
  306.         or    al,al
  307.         jz    DateOK
  308.         mov    al,5            ; error code 5
  309.         call    Terminate
  310.   DateOK:
  311.         mov    ch,byte ptr cHour
  312.         mov    cl,byte ptr cMinute
  313.         mov    dh,byte ptr cSecond
  314.         mov    dl,99
  315.         mov    ah,2Dh            ; set time
  316.         int    21h
  317.         or    al,al
  318.         jz    TimeOK
  319.         mov    al,6            ; error code 6
  320.         call    Terminate
  321.   TimeOK:
  322.   SkipTimeset:                    ; edit time display msg
  323.         mov    al,byte ptr cWday
  324.         mul    m6
  325.         add    ax,offset Weekdays
  326.         mov    si,ax
  327.         mov    di,offset msgweek
  328.         mov    cx,3
  329.         rep    movsw            ; copy weekday string
  330.             
  331.         mov    si,offset cYear
  332.         mov    di,offset msgyear
  333.         mov    ch,'-'
  334.         mov    cl,3
  335.         call    PutNums         ; put date
  336.  
  337.         mov    di,offset msghour
  338.         mov    ch,':'
  339.         mov    cl,3
  340.         call    PutNums         ; put time
  341.  
  342.         mov    si,offset RespondingIpNr
  343.         mov    di,offset msgts
  344.         call    PutIpNum        ; put time server IP #
  345.  
  346.         mov    dx,offset msgset
  347.         mov    ah,9
  348.         int    21h            ; display string
  349.  
  350.         ret
  351. SetTime     endp
  352.  
  353.  
  354.  
  355. ;**********************************************************************
  356. ;
  357. ;    Timezone environment handling
  358. ;
  359. ;**********************************************************************
  360.  
  361.         assume    ds:code_s
  362.  
  363. comment |
  364. ==========
  365. tech.notes/pc.code #29, from pmaupin, 3407 chars, Sat Jun  4 22:40:45 1988
  366. ----------
  367. TITLE: Finding DOS's master environment pointer
  368. This is a fragment of code that my SD.COM program uses to find
  369. the environment.  This fragment is different than most ways of
  370. finding the environment, in that it finds the MASTER environment block,
  371. not the current process's parent's environment.
  372.  
  373. This is useful in some cases, and has the added advantage that
  374. it does NOT behave differently when executing under CodeView,
  375. so you do NOT have to hard-code your system's DOS environment address
  376. into your program in order to debug it.
  377.     |
  378.  
  379. EnvPtr           EQU         2CH       ; Offset in PSP
  380.  
  381. CommandInterrupt   EQU         2EH       ; entry point into first Command.Com
  382.                        ; through interpreter
  383.  
  384. DosSegPtr       EQU         CommandInterrupt * 4 + 2
  385.  
  386.  
  387. ; FindEnvironment is passed:
  388.  
  389. ;   DS should point to program PSP
  390.  
  391. ; FindEnvironment returns:
  392.  
  393. ;   ES points to master environment block, or program's copy if couldn't
  394. ;           find the master.
  395.  
  396. ;   CX is length of block, or 0 if couldn't find the master.
  397.  
  398. ; FindEnvironment destroys:
  399.  
  400. ;   AX, SI
  401.  
  402.  
  403. FindEnvironment    PROC  NEAR
  404.            xor     si,si              ; Point to segment 0
  405.            mov     es,si
  406.            mov     si, word ptr es:[DosSegPtr]
  407.            mov     ax,si
  408.            call  VerifyBlock          ; make sure we've found COMMAND
  409.            jnz     GotBlock          ; jump if not a good block --
  410.                           ; use process's environment
  411.  
  412.            mov     ax,es:[EnvPtr+10h]   ; get COMMAND's environment ptr
  413.            or     ax,ax              ; jump if COMMAND has a
  414.            jnz     MaybeGoodBlock       ; subsidiary environment
  415.  
  416.            mov     ax,si              ; If no subsidiary, just use
  417.            add     ax,cx              ; the allocation block
  418.            inc     ax              ; immediately after COMMAND
  419.  
  420. MaybeGoodBlock:    call  VerifyBlock          ; verify that we have a good
  421.                           ; one, one way or another
  422. GotBlock:
  423.            shl     cx,1              ; multiply by 16 to get
  424.            shl     cx,1              ; length in bytes
  425.            shl     cx,1
  426.            shl     cx,1
  427.            mov     es,ax
  428.            ret
  429.  
  430.  
  431. ; VerifyBlock tries to insure that we're pointing to a valid DOS
  432. ; allocation block.  If not, returns the current process's environment
  433. ; block.
  434.  
  435.  
  436. VerifyBlock       PROC  NEAR
  437.            dec     ax             ; get block header into ES
  438.            mov     es,ax
  439.            inc     ax
  440.  
  441.            cmp     byte ptr es:[0],04Dh     ; make sure signature is valid
  442.            jnz     UseCurrent
  443.            cmp     word ptr es:[1],si     ; make sure owner is valid
  444.            jnz     UseCurrent
  445.            mov     cx, word ptr es:[3]     ; retrieve the length
  446.            ret
  447.  
  448. UseCurrent:       mov     ax,word ptr ds:[EnvPtr] ; get current process's env
  449.            xor     cx,cx             ; zero length
  450.            ret
  451. VerifyBlock       ENDP
  452.  
  453. FindEnvironment    ENDP
  454.  
  455. comment |
  456. So far, this seems to work.  I would welcome any feedback on its
  457. efficacy, but if the feedback is negative, please give the DOS version
  458. and a detailed problem description.  Thanks,
  459. Pat
  460.     |
  461.  
  462. ;************************************************************************
  463. ;*        SetZone
  464. ;*
  465. ;************************************************************************
  466.  
  467.         assume    ds:code_s
  468.  
  469. SetZone     proc    near
  470.         test    GenFlags,ARGZONE    ; anything to SET?
  471.         jnz    SetZoneInfo
  472.         ret
  473.  
  474.   SetZoneInfo:
  475.         cld
  476.         test    GenFlags,ARGZONESPEC    ; extended syntax?
  477.         jnz    SetZoneSpecial
  478.  
  479.         mov    ax,DstAdvance        ; -no, use numeric zones
  480.         xor    dx,dx            ; compute current time offset
  481.         mov    cx,tzoffset
  482.         xchg    ch,cl
  483.         mov    bx,tzoffset+2
  484.         xchg    bh,bl
  485.         sub    ax,bx
  486.         sbb    dx,cx
  487.         mov    cl,'+'
  488.         jns    ZonePositive        ; current time offset sign?
  489.         mov    cl,'-'
  490.         call    NegDxAx
  491.   ZonePositive:
  492.         mov    di, offset ZoneString
  493.         add    di,ZoneVarLen
  494.         mov    [di],cl
  495.         div    mhour            ; compute hours
  496.         push    ax
  497.         mov    ax,dx
  498.         div    m60b            ;   and minutes
  499.         xor    ah,ah
  500.         mov    di,offset ZoneString+3
  501.         add    di,ZoneVarLen
  502.         mov    PutMinDigits,2    ; *test
  503.         call    PutNum            ; put minutes
  504.         pop    ax
  505.         sub    di,4
  506.         call    PutNum            ; put hours
  507.         jmp    short EnvMaster
  508.  
  509.   SetZoneSpecial:
  510.         test    GenFlags,DSTNOW     ; is it normal or dls time?
  511.         jz    EnvMaster
  512.         mov    di,offset ZoneString    ; -dls, copy dls name
  513.         mov    si,di            ;      on top of normal name
  514.         add    di,ZoneVarLen
  515.         add    si,ZoneDstInd
  516.   SetZoneLoop:
  517.         lodsb
  518.         stosb
  519.         cmp    al,0
  520.         jne    SetZoneLoop
  521.         stosb
  522.         mov    al,'$'
  523.         stosb
  524.         sub    di,offset ZoneString+1
  525.         mov    ZoneStrLen,di
  526.   EnvMaster:
  527.         call    FindEnvironment     ; Find master environment
  528.  
  529.         xor    di,di
  530.         xor    al,al
  531.   EnvNext:
  532.         jcxz    EnvNotFound
  533.  
  534.         test    FlagWord,DONT_SETTIME
  535.         jnz    EnvZonePr
  536.         push    cx            ; look for TZ=
  537.         push    di
  538.         mov    cx,ZoneVarLen
  539.         mov    si,offset ZoneString
  540.         repe    cmpsb
  541.         jnz    EnvNoMatch
  542.  
  543.         pop    di            ; if found, remove whole string
  544.         pop    cx
  545.         push    cx
  546.         push    di
  547.         repne    scasb
  548.         mov    si,di
  549.         pop    di
  550.         push    di
  551.         push    es
  552.         pop    ds
  553.         rep    movsb
  554.         push    cs
  555.         pop    ds
  556.   EnvNoMatch:
  557.         pop    di
  558.         pop    cx
  559.  
  560.         cmp    byte ptr es:[di],0    ; end of strings?
  561.         je    EnvEnd
  562.         repne    scasb            ; -no, skip past this string
  563.         jmp    short EnvNext
  564.   EnvEnd:
  565.         cmp    cx,ZoneStrLen        ; add TZ string to the end
  566.         jb    EnvNotFound
  567.         mov    cx,ZoneStrLen
  568.         mov    si,offset ZoneString
  569.         rep    movsb
  570.  
  571.   EnvZonePr:
  572.         mov    dx,offset ZoneMsg
  573.         push    cs
  574.         pop    ds
  575.         mov    ah,9
  576.         int    21h            ; display env var contents
  577.  
  578.   EnvNotFound:
  579.         push    cs            ; restore ds and es
  580.         push    cs
  581.         pop    ds
  582.         pop    es
  583.         ret
  584. SetZone     endp
  585.