home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_progs / prog-asm / examples.lzh / EXAMPLES / TASK / TASK.ASM < prev    next >
Encoding:
Assembly Source File  |  1991-08-16  |  5.1 KB  |  145 lines

  1.   XREF    _SysBase,_DOSBase,_stdout  ;from startup code
  2.  
  3.   XREF     _LVOWait                  ;from amiga.lib
  4.   XREF     _LVOWaitTOF
  5.   XREF     _LVOCloseLibrary,_LVOOpenLibrary
  6.   XREF     _LVODelay,_LVOWrite
  7.   XREF     _CreateTask,_DeleteTask
  8.  
  9.   XREF     sprintf   ;from functions.o
  10.  
  11. ; Task.asm - a cheap sub-task example in by C Scheppner. Asm by JG.
  12. ; Cheap because there is shared data for communication (ie Counter) rather
  13. ; than using MsgPorts. This runs the risk of the subTask modifying Counter
  14. ; while the main task is printing out its value. We could use Forbid() and
  15. ; Permit() in main() to disable the subTask while we print Count, but then
  16. ; subTask could not be incrementing Count "during" main.
  17. ; Link with StartUp.o instead of SmallStart.o because we need _stdout.
  18.  
  19. LIB_VERSION equ 33
  20.  
  21.     SECTION  TaskCode,CODE
  22.  
  23.    XDEF   _main
  24. _main:
  25.     movea.l  _SysBase,a6
  26. ;----Open the graphics library
  27.     moveq    #LIB_VERSION,d0
  28.     lea      GfxName,a1
  29.     jsr      _LVOOpenLibrary(a6)
  30.     move.l   d0,_GfxBase
  31.     bne.s    .5            ;branch if successful open
  32.     lea      CantOpenGfx,a0
  33.     bra.s    _cleanexit
  34. ;----create a subTask by calling CreateTask--------
  35. ;---CreateTask(subTaskName,0,subTaskRtn,2000)-----
  36. .5  pea      2000          ;stack size, 2K is just enough for a minor task
  37.     pea      subTaskCode   ;the code where execution starts
  38.     clr.l    -(sp)         ;no special termination code, main will delete it
  39.     pea      _subTaskName  ;name of the task
  40.     jsr      _CreateTask   ;standard C function in amiga.lib (or Manx c lib)
  41.     lea      16(sp),sp
  42.     move.l   d0,_subTaskPtr
  43.     bne.s    .6            ;branch if successfully created
  44.     lea      CantCreateTask,a0
  45.     bra.s    _cleanexit
  46. ;---for 0 to 9 (ie loop 10 times)---------
  47. .6  moveq    #10-1,d6      ;subtract 1 for the Dbra instruction
  48.     movea.l  _DOSBase,a6   ;wait while the subTask increments
  49.    ;----Delay(50) (main is a process and can call Delay. subTask can't.)
  50. .9  moveq    #50,d1
  51.     jsr      _LVODelay(a6) ;the Counter every 1/60th of a second.
  52.   ;---Print out the current value (whatever it is) of the Counter to the CLI
  53.     move.l   _Counter,-(sp)
  54.     lea      _buffer,a0   ;where to store the formatted ascii string
  55.     move.l   #CounterFormat,d0
  56.     jsr      sprintf
  57.     addq.w   #4,sp
  58.     move.l   d0,d3         ;length of string
  59.     move.l   #_buffer,d2
  60.     move.l   _stdout,d1
  61.     beq.s    skip          ;no _stdout? Must have run from WorkBench
  62.     jsr      _LVOWrite(a6)
  63. skip:
  64.     Dbra     d6,.9
  65. ;---Close everything after 10 loops
  66.     bra.s    .14
  67.  
  68.    XDEF   _cleanexit
  69. _cleanexit:
  70. ;---Print the passed string message (in a0) to the CLI
  71.     move.l   _stdout,d1
  72.     beq.s    .14           ;no _stdout? Must have run from WorkBench
  73.     move.l   a0,d2
  74. len move.b   (a0)+,d0
  75.     bne.s    len
  76.     move.l   a0,d3
  77.     sub.l    d2,d3         ;length of string
  78.     movea.l  _DOSBase,a6
  79.     jsr      _LVOWrite(a6)
  80. .14 movea.l  _SysBase,a6
  81. ;---If subTask exists, then set PrepareToDie to tell it "Get ready to be
  82. ;   deleted, you heathen pig!"
  83.     move.l   _subTaskPtr,d0
  84.     beq.s    .19
  85.     lea      _PrepareToDie,a0
  86.     Bset.b   #0,(a0)
  87. ;---(Busy) Wait for subTask to recognize PrepareToDie, then delete subTask.
  88. ;   (subTask will clear PrepareToDie before it goes to sleep)
  89. .20 Btst.b   #0,(a0)
  90.     bne.s    .20     ;"Finish incrementing Counter and recognize us, scum!"
  91. ;----Delete the subTask
  92.     move.l   d0,-(sp)    ;address of SubTask
  93.     jsr      _DeleteTask ;standard C Function
  94.     addq.w   #4,sp
  95. ;---If graphics library is open, close it
  96. .19 move.l   _GfxBase,d0
  97.     beq.s    .22
  98.     movea.l  d0,a1
  99.     jsr      _LVOCloseLibrary(a6)
  100. ;--- exit the program (i.e. return to the startup code)-----
  101. .22 rts
  102.  
  103. ;subTaskCode increments the Counter every 1/60 second until main tells it to
  104. ;"prepare to be deleted" by setting bit #0 of PrepareToDie. subTask then
  105. ;clears PrepareToDie, telling main() that it is now waiting to be deleted.
  106. ;It calls the Wait function with a mask of 0. This task will never wake up
  107. ;in a million years when you pass a 0 to Wait, but it doesn't matter because
  108. ;main is going to delete it.
  109.  
  110.    XDEF   subTaskCode
  111. subTaskCode:
  112.     movea.l  _GfxBase,a6
  113. ;---increment the counter until _main() says "PrepareToDie" (sets bit #0)
  114. .26 jsr      _LVOWaitTOF(a6)  ;slight delay (of 1/60 sec) inbetween each inc
  115.     addq.l   #1,_Counter
  116.     Bclr.b   #0,_PrepareToDie ;Should I Wait?
  117.     beq.s    .26
  118. ; At this point, subTask has just cleared PrepareToDie, telling _main
  119. ; "I'm ready to be deleted". Now, Wait forever (or until _main deletes me)
  120.     moveq    #0,d0        ;0 = wait forever, never returns from Wait
  121.     movea.l  _SysBase,a6
  122.     jmp      _LVOWait(a6)
  123.  
  124.    SECTION  TaskData,DATA
  125.  
  126.    XDEF    _subTaskPtr,_subTaskName
  127. _subTaskPtr    dc.l 0
  128.  
  129. ; Data shared by main and subTaskRtn
  130.    XDEF   _GfxBase,_Counter,_PrepareToDie
  131. _GfxBase       dc.l 0
  132. _Counter       dc.l 0 ;the hex Counter value
  133. _PrepareToDie  dc.b 0
  134.  
  135. ; Strings
  136. _subTaskName   dc.b 'Example SubTask',0
  137. GfxName        dc.b 'graphics.library',0
  138. CantOpenGfx    dc.b 'Cannot open graphics.library',0
  139. CantCreateTask dc.b 'Cannot create the subTask',0
  140. CounterFormat  dc.b 'Counter = %ld',10,0
  141.  
  142. _buffer  ds.b 40 ;we store our ascii "numeral" Counter here
  143.  
  144.    END
  145.