   incdir   'asm:include/'
   include  "exec/exec.i"
   include  "libraries/dosextens.i
findtask=-294
waitport=-384
getmsg=-372
forbid=-132
replymsg=-378
openf=-30
closef=-36
read=-42
write=-48
callexec macro
   move.l   4,a6
   jsr   \1(a6)
   endm
calldos  macro
   move.l   dosbase,a6
   jsr   \1(a6)
   endm
callint  macro
   move.l   intbase,a6
   jsr   \1(a6)
   endm
closeint macro
   move.l   intbase,a1
   callexec -414
   endm
closedos macro
   move.l   dosbase,a1
   callexec -414
   endm
openint  macro
   lea   intname,a1
   callexec -414
   tst.l d0
   beq   openintQ
   move.l   d0,intbase
   move.b   #1,d1
   jmp   openintok
openintq move.b   #0,d1
openintok   
   endm
opendos  macro
   lea   dosname,a1
   callexec -414
   tst.l d0
   beq   opendosQ
   move.l   d0,dosbase
   move.b   #1,d1
   jmp   opendosok
opendosq move.b   #0,d1
opendosok   
   endm
   movem.l  d0/a0,-(sp)    save initial values
   clr.l returnMsg
   sub.l a1,a1
   CALLEXEC FindTask    find us
   move.l   d0,a4
   tst.l pr_CLI(a4)
   beq.s fromWorkbench
   movem.l  (sp)+,d0/a0    restore regs
   bra   end_startup    and run the user prog
fromWorkbench
   lea   pr_MsgPort(a4),a0
   CALLEXEC WaitPort    wait for a message
   lea   pr_MsgPort(a4),a0
   CALLEXEC GetMsg         then get it
   move.l   d0,returnMsg      save it for later reply
   nop
   movem.l  (sp)+,d0/a0    restore
end_startup
   bsr.s _main       call our program
   move.l   d0,-(sp)    save it
   tst.l returnMsg
   beq.s exitToDOS      if I was a CLI
   CALLEXEC Forbid
   move.l   returnMsg(pc),a1
   CALLEXEC ReplyMsg
exitToDOS
   move.l   (sp)+,d0    exit code
   rts
returnMsg   dc.l  0
intbase  dc.l  0
dosbase  dc.l  0
dosname  dc.b  'dos.library',0
intname  dc.b  'intuition.library',0

   even
_main 
