home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 555a.lha / Day2Day / Day2Day.asm < prev    next >
Assembly Source File  |  1991-09-20  |  9KB  |  432 lines

  1.  
  2. *       Day2Day
  3. *
  4. *    Calculates the difference between two dates (in days).
  5. *
  6. *
  7. *    NOTE:    There's no need to 'RUN' or 'RUNBACK' this program from the
  8. *        CLI. It is auto-detaching.
  9. *
  10. *HISTORY
  11. *         Made with Hisoft V2.12
  12. *  V1.0   27-Jul-91: Made the calculation and parsing routines. Works fine.
  13. *                    Added intuition interface.
  14. *         28-Jul-91: Rewrote calculation routine - now much faster (but still
  15. *                    not optimal).
  16. *                    Added date validity-check.
  17.  
  18.     OPT O+
  19.     OPT O1+            ; Tells when a branch could be optimised to short
  20.     OPT i+            ; Tells when '#' is probably missing
  21.  
  22.         incdir        "AsmInc:"
  23.         include        "P.i"
  24.         include        "Intui.i"
  25.         include        "Detach.i"
  26.         include        "relMacros.i"
  27.         include        "intuition/intuition.i"
  28.         include        "intuition/intuition_lib.i"
  29.         include        "libraries/dosextens.i"
  30.  
  31. DB        EQUR        A4
  32.  
  33.  dcDeclare    A4
  34.  dcAPtr        WBenchMsg
  35.  dcAPtr        IntuiBase
  36.  dcAPtr        DWindow
  37.  dcAPtr        Up
  38.  dcAPtr        Rp
  39.  dcLong        Class
  40.  dcAPtr        IAddress
  41.  dcArea        FromInfo,si_SIZEOF
  42.  dcArea        ToInfo,si_SIZEOF
  43.  dcArea        FromBuf,11
  44.  dcArea        ToBuf,11
  45.  dcEnd
  46.  
  47. Start        DetachSingle    <'Day2Day'>,4000,0
  48.         dcAlloc                    ; Allocate memory for variables
  49.         dcReset                    ; Clear the memory
  50.         lea        FromGad(PC),A1
  51.         lea        FromInfo(DB),A2
  52.         move.l        A2,gg_SpecialInfo(A1)
  53.         lea        FromBuf(DB),A1
  54.         move.l        A1,si_Buffer(A2)
  55.         move.w        #11,si_MaxChars(A2)
  56.  
  57.         lea        ToGad(PC),A1
  58.         lea        ToInfo(DB),A2
  59.         move.l        A2,gg_SpecialInfo(A1)
  60.         lea        ToBuf(DB),A1
  61.         move.l        A1,si_Buffer(A2)
  62.         move.w        #11,si_MaxChars(A2)
  63.  
  64.         Prepare        Exec_Call
  65.         suba.l        A1,A1
  66.         CallLib        FindTask        ; Find us
  67.         movea.l        D0,A2
  68.         tst.l        pr_CLI(A2)
  69.         bne.S        GetLibs
  70.         lea        pr_MsgPort(A2),A0
  71.         CallLib        WaitPort        ; wait for a message
  72.         lea        pr_MsgPort(A2),A0
  73.         CallLib        GetMsg            ; then get it
  74.         move.l        D0,WBenchMsg(DB)    ; save it for later reply
  75. GetLibs        lea        IntuiName(PC),A1
  76.         CallLib        OldOpenLibrary
  77.         move.l        D0,IntuiBase(DB)
  78.         beq.S        Error
  79.  
  80.         Prepare        Intuition_Call
  81.         lea        NW(PC),A0
  82.         CallLib        OpenWindow
  83.         move.l        D0,DWindow(DB)
  84.         movea.l        D0,A0
  85.         beq.S        Error
  86.         move.l        wd_RPort(A0),Rp(DB)
  87.         move.l        wd_UserPort(A0),Up(DB)
  88.         lea        WinTitle(PC),A1
  89.         lea        ScrTitle(PC),A2
  90.         CallLib        SetWindowTitles
  91.         bra.S        Main
  92.  
  93. Error
  94. Exit
  95. FreeWindow    Prepare        Intuition_Call
  96.         move.l        DWindow(DB),D0
  97.         beq.S        FreeIntui
  98.         move.l        D0,A0
  99.         CallLib        CloseWindow
  100. FreeIntui    Prepare        Exec_Call
  101.         move.l        IntuiBase(DB),D0
  102.         beq.S        ReplyWB
  103.         movea.l        D0,A1
  104.         CallLib        CloseLibrary
  105. ReplyWB        move.l        WBenchMsg(DB),D2
  106.         beq.S        AllDone
  107.         CallLib        Forbid
  108.         movea.l        D2,A1
  109.         CallLib        ReplyMsg        ; Reply WBenchMessage if we are started from WB
  110. AllDone        dcFree
  111.         moveq        #0,D0
  112. DoNothing    rts
  113.  
  114.  
  115. Main
  116. EventLoop    movea.l        Up(DB),A0
  117.         Prepare        Exec_Call
  118.         CallLib        WaitPort
  119. GetNextMsg    Call        GetAMessage
  120.         beq.S        EventLoop
  121.         move.l        Class(DB),D0
  122.         cmp.l        #CLOSEWINDOW,D0
  123.         beq.S        Exit
  124.         andi.w        #GADGETDOWN+GADGETUP,D0
  125.         bne.S        GJ
  126.         cmp.l        #ACTIVEWINDOW,D0
  127.         bne.S        GetNextMsg
  128.         Call        ActivateFrom
  129.         bra.S        GetNextMsg
  130. GJ        movea.l        IAddress(DB),A1
  131.         move.w        gg_GadgetID(A1),D0    ; GadgetID is offset from GJ
  132.         jsr        GJ(PC,D0.W)
  133.         bra.S        GetNextMsg
  134.  
  135. *»»» User pressed RETURN in the 'To' string-gadget,
  136. *»»» or activated the window, or an error was found
  137. *»»» in the 'From' string-gadget.
  138. ActivateFrom    lea        FromGad(PC),A0
  139.         bra.S        ActivateStr
  140. *»»» User pressed RETURN in the 'From' string-gadget,
  141. *»»» or an error was found in the 'To' string-gadget.
  142. ActivateTo    lea        ToGad(PC),A0
  143. ActivateStr    Prepare        Intuition_Call
  144.         move.l        DWindow(DB),A1
  145.         suba.l        A2,A2
  146.         CallLib        ActivateGadget
  147.         rts
  148.  
  149. *»»» User clicked the 'Solve' button
  150. DoSolve        lea        FromBuf(DB),A0
  151.         Call        ParseDate
  152.         bmi.S        1$
  153.         move.l        D1,D4
  154.         move.l        D2,D5
  155.         move.l        D3,D6
  156.         lea        ToBuf(DB),A0
  157.         Call        ParseDate
  158.         bmi.S        2$
  159.         exg        D1,D4
  160.         exg        D2,D5
  161.         exg        D3,D6
  162.         move.l        D3,D0
  163.         swap        D0
  164.         move.w        D2,D0
  165.         lsl.w        #8,D0
  166.         move.b        D1,D0
  167.         move.l        D6,D7
  168.         swap        D7
  169.         move.w        D5,D7
  170.         lsl.w        #8,D7
  171.         move.b        D4,D7
  172.         cmp.l        D0,D7        ; Compare date order
  173.         blt.S        1$
  174.         Call        CalcDays
  175.         lea        TxtAre+6(PC),A0
  176.         Call        MakeDecStr
  177.         Call        PrintSolution
  178.         Call        ActivateFrom
  179.         rts
  180. 1$        Call        ActivateFrom
  181.         bra.S        3$
  182. 2$        Call        ActivateTo
  183. 3$        Prepare        Intuition_Call
  184.         move.l        DWindow(DB),A0
  185.         move.l        wd_WScreen(A0),A0
  186.         CallLib        DisplayBeep
  187.         lea        TxtAre+6(PC),A0
  188.         moveq        #8,D0
  189. 4$        move.b        #'?',(A0)+
  190.         dbf        D0,4$
  191.         Call        PrintSolution
  192.         rts
  193.  
  194. PrintSolution    Prepare        Intuition_Call
  195.         move.l        Rp(DB),A0
  196.         lea        ITxtAre(PC),A1
  197.         move.w        #Sx,D0
  198.         moveq        #Sy,D1
  199.         CallLib        PrintIText
  200.         rts
  201.  
  202. *»»» Call:    D1 = Day    (from)
  203. *»»»        D2 = Month    (from)
  204. *»»»        D3 = Year    (from)
  205. *»»»        D4 = Day    (to)
  206. *»»»        D5 = Month    (to)
  207. *»»»        D6 = Year    (to)
  208. CalcDays    Push        D1-D7/A0
  209.         moveq        #0,D7
  210.         move.w        D3,D0
  211.         Call        AdjustYear        
  212.         lea        Days-1(PC),A0
  213.         add.w        D2,A0
  214.         move.b        (A0)+,D7
  215.         sub.w        D1,D7
  216.         add.w        D4,D7
  217.         sub.w        D3,D6
  218.         subq.w        #1,D6
  219.         bge.S        1$
  220.         sub.w        D2,D5
  221.         bgt.S        6$
  222.         move.w        D4,D7
  223.         sub.w        D1,D7
  224.         bra.S        9$
  225. 1$        neg.w        D2
  226.         add.w        #12,D2
  227.         bra.S        3$
  228. 2$        moveq        #0,D0
  229.         move.b        (A0)+,D0
  230.         add.l        D0,D7
  231. 3$        dbf        D2,2$
  232.         bra.S        5$
  233. 4$        add.l        #365-28,D7
  234.         moveq        #0,D0
  235.         move.b        Days+1(PC),D0
  236.         add.l        D0,D7
  237. 5$        addq.w        #1,D3
  238.         move.w        D3,D0
  239.         Call        AdjustYear        
  240.         dbf        D6,4$
  241.         lea        Days(PC),A0
  242. 6$        subq.w        #1,D5
  243.         bra.S        8$
  244. 7$        moveq        #0,D0
  245.         move.b        (A0)+,D0
  246.         add.l        D0,D7
  247. 8$        dbf        D5,7$
  248. 9$        move.l        D7,D0
  249.         Pop        D1-D7/A0
  250.         rts
  251.  
  252. *»»» Call:    D0 = year to adjust
  253. *»»» Changes the number of days in the month of February
  254. *»»» according to the rules for leapyear.
  255. AdjustYear    Push        D0-D1/A0
  256.         ext.l        D0
  257.         move.l        D0,D1
  258.         andi.w        #%11,D1            ;Year%4
  259.         bne.S        1$
  260.         move.l        D0,D1
  261.         divu        #400,D1            ;Year%400
  262.         swap        D1
  263.         tst.w        D1
  264.         beq.S        2$
  265.         move.l        D0,D1
  266.         divu        #100,D1
  267.         swap        D1            ;Year%100
  268.         tst.w        D1
  269.         bne.S        2$
  270. 1$        moveq        #28,D0
  271.         bra.S        3$
  272. 2$        moveq        #29,D0            ;If ((Year%400==0)||((Year%100!=0)&&(Year%4==0)))
  273. 3$        lea        Days+1(PC),A0
  274.         move.b        D0,(A0)            ;Days[1]=28 or Days[1]=28
  275.         Pop        D0-D1/A0
  276.         rts
  277.  
  278. *»»» Call:    A0 = String
  279. ParseDate    Call        DoNumber
  280.         bne.S        1$
  281.         move.l        D0,D1        ; Day
  282.         Call        DoNumber
  283.         bne.S        1$
  284.         move.l        D0,D2        ; Month
  285.         Call        DoNumber
  286.         bmi.S        1$
  287.         beq.S        1$
  288.         move.l        D0,D3        ; Year
  289.         Call        AdjustYear    ; Check for valid date
  290.         tst.l        D2
  291.         ble.S        1$
  292.         cmp.w        #12,D2
  293.         bgt.S        1$
  294.         tst.l        D1        ; Month was valid
  295.         ble.S        1$
  296.         lea        Days(PC),A0
  297.         cmp.b        -1(A0,D2),D1
  298.         bgt.S        1$
  299.         moveq        #0,D0        ; And day was valid too
  300.         rts
  301. 1$        moveq        #-1,D0
  302.         rts
  303.  
  304. *»»» Call:    A0 = String
  305. DoNumber    Push        D1
  306.         moveq        #0,D0
  307. 1$        move.b        (A0)+,D1
  308.         beq.S        6$
  309.         cmp.b        #'-',D1
  310.         beq.S        5$
  311.         sub.b        #'0',D1
  312.         blt.S        4$
  313.         cmp.b        #9,D1
  314.         bgt.S        4$
  315.         mulu        #10,D0
  316.         ext.w        D1
  317.         add.w        D1,D0
  318.         bra.S        1$
  319. 4$        moveq        #-1,D1
  320.         bra.S        3$
  321. 6$        moveq        #1,D1
  322.         bra.S        3$
  323. 5$        moveq        #0,D1
  324. 3$        Pop        D1
  325.         rts
  326.  
  327. *»»» Call:    D0 = Number to convert to ascii
  328. *»»»        A0 = Where to put string
  329. MakeDecStr    Push        D1-D5/A0
  330.         moveq        #9,D1
  331.         tst.l        D0
  332.         beq.S        6$
  333.         subq.l        #1,D1
  334.         asl.l        #2,D1
  335.         moveq        #' ',D4
  336.         moveq        #'0',D2
  337. 1$        move.w        D2,D3
  338.         move.l        9$(PC,D1.l),D5
  339. 2$        cmp.l        D5,D0
  340.         blt.S        3$
  341.         addq.w        #1,D3
  342.         sub.l        D5,D0
  343.         bra.S        2$
  344. 3$        cmp.b        D2,D3
  345.         bne.S        4$
  346.         move.w        D4,D3
  347.         bra.S        5$
  348. 4$        move.w        D2,D4
  349. 5$        move.b        D3,(A0)+
  350.         subq.w        #4,D1
  351.         bge.S        1$
  352.         bra.S        8$
  353. 6$        subq.l        #2,D1
  354. 7$        move.b        #' ',(A0)+
  355.         dbf        D1,7$
  356.         move.b        #'0',(A0)+
  357. 8$        Pop        D1-D5/A0
  358.         rts
  359. 9$        dc.l        1,10,100,1000,10000,100000,1000000,10000000
  360.  
  361. GetAMessage    Push        D0-D1/A0-A1/A6
  362.         movea.l        Up(DB),A0
  363.         Prepare        Exec_Call
  364.         CallLib        GetMsg
  365.         tst.l        D0
  366.         beq.S        1$
  367.         movea.l        D0,A1
  368.         move.l        20(A1),Class(DB)
  369.         move.l        28(A1),IAddress(DB)
  370.         CallLib        ReplyMsg
  371.         moveq        #1,D0
  372. 1$        Pop        D0-D1/A0-A1/A6
  373.         rts
  374.  
  375. Days        dc.b        31,28,31,30,31,30,31,31,30,31,30,31
  376.  
  377. IntuiName    dc.b        'intuition.library',0
  378.         EVEN
  379.  
  380. IDCMPFlags    =GADGETUP+GADGETDOWN+CLOSEWINDOW+ACTIVEWINDOW
  381. OtherFlags    =WINDOWCLOSE+WINDOWDRAG+WINDOWDEPTH+NOCAREREFRESH+ACTIVATE
  382. NW        dc.w        320-WW/2,128-WH/2,WW,WH
  383.         dc.b        0,1
  384.         dc.l        IDCMPFlags,OtherFlags
  385.         dc.l        GadgetList,0,0,0,0
  386.         dc.w        0,0,0,0,WBENCHSCREEN
  387.  
  388. WW        =222        ; window width
  389. WH        =78        ; window height
  390. SW        =53        ; gadget width
  391. SH        =21        ; gadget height
  392. Sx        =157        ; gadget xpos
  393. Sy        =34        ; gadget ypos
  394. FW        =88        ; gadget width
  395. FH        =10        ; gadget height
  396.  
  397. GadgetList
  398. FromGad        Gadget        ToGad,52,33,FW,FH,GADGHCOMP,RELVERIFY,STRGADGET
  399.         Gadget2        FBorder,0,ITxtFrom,0,0,ActivateTo-GJ,0
  400. ToGad        Gadget        SolveGad,52,48,FW,FH,GADGHCOMP,RELVERIFY,STRGADGET
  401.         Gadget2        FBorder,0,ITxtTo,0,0,ActivateFrom-GJ,0
  402. SolveGad    Gadget        0,Sx,Sy,SW,SH,GADGHCOMP,RELVERIFY,BOOLGADGET
  403.         Gadget2        ButBorder,0,ITxtSolve,0,0,DoSolve-GJ,0
  404.  
  405. ButBorder    Border        -2,-1,1,0,1,9,ButVectors,But2Border
  406. ButVectors    dc.w        2,0,SW+1,0,SW+3,2,SW+3,SH-1,SW+1,SH+1,2,SH+1,0,SH-1,0,2,2,0
  407. But2Border    Border        -107,37,1,0,1,2,FVectors,0
  408. FBorder        Border        0,8,1,0,1,2,FVectors,0
  409. FVectors    dc.w        0,0,FW-1,0
  410.  
  411. ITxtSolve    IntuiText    3,0,1,6,7,TxtSolve,ITxtAre
  412. ITxtAre        IntuiText    1,0,1,-147,29,TxtAre,ITxtFormat
  413. ITxtFormat    IntuiText    1,0,1,-148,-16,TxtFormat,0
  414. ITxtFrom    IntuiText    1,0,1,-43,0,TxtFrom,0
  415. ITxtTo        IntuiText    1,0,1,-43,0,TxtTo,0
  416.  
  417. TxtSolve    dc.b        'Solve',0
  418. TxtAre        dc.b        'are   ?????????   days',0
  419. TxtFormat    dc.b        'Date-format is DD-MM-YYYY',0
  420. TxtFrom        dc.b        'From',0
  421. TxtTo        dc.b        'to',0
  422. WinTitle    dc.b        'Day2Day V1.0',0
  423. ScrTitle    dc.b        'Day2Day V1.0 © 1991 by Preben Nielsen',0
  424.         EVEN
  425.  
  426. TxtAttr        dc.l        FontName
  427.         dc.w        TOPAZ_EIGHTY
  428.         dc.b        FS_NORMAL,FPB_ROMFONT
  429. FontName    dc.b        'topaz.font',0
  430.         END
  431.  
  432.