home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / program / rs2lang / exinit.s < prev    next >
Encoding:
Text File  |  1993-10-23  |  8.1 KB  |  223 lines

  1. *************************************************************************
  2. *                                                                       *
  3. *    DSTART.S modifié :  Startup module for C programs using dLibs      *
  4. *                                                                       *
  5. *************************************************************************
  6. *
  7. * entry points
  8. *
  9. .globl    __base              ; basepage pointer
  10. .globl    __start             ; startup entry point
  11. .globl    _etext              ; end of text segment
  12. .globl    _edata              ; end of data segment
  13. .globl    _end                ; end of BSS segment (end of program)
  14. .globl    __break             ; location of stack/heap break
  15. .globl    __exit              ; terminate immediately with exit code
  16. .globl    __argc              ; number of arguments
  17. .globl    __argv              ; argument list pointer
  18. .globl    __envp              ; environment string pointer
  19. .globl    _errno              ; system error number
  20. .globl    _gemdos             ; trap #1 (gemdos) hook
  21. .globl    _bios               ; trap #13 (bios) hook
  22. .globl    _xbios              ; trap #14 (xbios) hook
  23. .globl    _bdos               ; trap #2 (bdos) hook
  24.  
  25. *
  26. * external references
  27. *
  28. .globl    __STKSIZ            ; Stack size value from C (unsigned long)
  29. ; .globl    __main              ; Initial entry point for C program
  30. .globl    _main               ; C program main() function
  31.  
  32. *
  33. * useful constants
  34. *
  35. MINSTK    equ       1024      ; Minimum 1K stack size
  36. MARGIN    equ       512       ; Minimum memory to return to OS
  37.  
  38. *
  39. * GEMDOS functions
  40. *
  41. Cconws    equ       $09       ; Console write string
  42. Pterm     equ       $4C       ; Process terminate (with exit code)
  43. Mshrink   equ       $4A       ; Shrink program space
  44.  
  45. *
  46. * basepage offsets
  47. *
  48. p_hitpa   equ       $04       ; top of TPA
  49. p_tbase   equ       $08       ; base of text
  50. p_tlen    equ       $0C       ; length of text
  51. p_dbase   equ       $10       ; base of data
  52. p_dlen    equ       $14       ; length of data
  53. p_bbase   equ       $18       ; base of BSS
  54. p_blen    equ       $1C       ; length of BSS
  55. p_env     equ       $2C       ; environment string
  56. p_cmdlin  equ       $80       ; command line image
  57.  
  58. *
  59. * STARTUP ROUTINE (must be first object in link)
  60. *
  61.           text
  62. *
  63. * save initial stack and basepage pointers
  64. *
  65. __start:  move.l    sp,a5               ; a5 = initial stack pointer
  66.           move.l    4(sp),a4            ; a4 = basepage address
  67.           move.l    a4,__base
  68.           move.l    p_tbase(a4),d3
  69.           add.l     p_tlen(a4),d3
  70.           move.l    d3,_etext           ; end of text segment
  71.           move.l    p_dbase(a4),d3
  72.           add.l     p_dlen(a4),d3
  73.           move.l    d3,_edata           ; end of data segment
  74.           move.l    p_bbase(a4),d3
  75.           add.l     p_blen(a4),d3
  76.           move.l    d3,_end             ; end of BSS (end of program)
  77.           move.l    d3,__break          ; set initial _break value
  78.           move.l    p_env(a4),__envp    ; save environment pointer
  79. *
  80. * calculate free space available to program
  81. *
  82.           move.l    __break,d3
  83.           move.l    d3,a3               ; a3 = base of free space
  84.           neg.l     d3
  85.           add.l     p_hitpa(a4),d3
  86.           sub.l     #MARGIN,d3          ; d3 = free space
  87. *
  88. * calculate new stack size (store in d2)
  89. *
  90.           move.l    #__STKSIZ,a2        ; a2 = &_STKSIZ
  91.           move.l    (a2),d2             ; if __STKSIZ is undefined
  92.           beq       minimum             ;   use MINSTK
  93.           bpl       setstk              ;   use __STKSIZ
  94.           add.l     d3,d2               ; if __STKSIZ is negative
  95.           cmp.l     #MINSTK,d2          ;   try (free space + __STKSIZ)
  96.           bge       setstk              ; if < MINSTK
  97. minimum:  move.l    #MINSTK,d2          ;   use MINSTK
  98. *
  99. * check to see if there is enough room for requested stack
  100. *
  101. setstk:   cmp.l     d3,d2
  102.           blt       shrink              ; if (available < requested)
  103.           move.l    #stkerr,-(sp)
  104.           move.w    #Cconws,-(sp)
  105.           trap      #1                  ;   report a stack error
  106.           addq.l    #6,sp
  107.           move.w    #-39,-(sp)
  108.           move.w    #Pterm,-(sp)
  109.           trap      #1                  ;   and return error -39 (ENSMEM)
  110. *
  111. * set up new stack pointer and Mshrink
  112. *
  113. shrink:   add.l     a3,d2               ; new stack = free base + stack size
  114.           move.l    d2,sp
  115.           sub.l     a4,d2               ; keep space = new stack - __base
  116.           move.l    d2,-(sp)
  117.           move.l    a4,-(sp)
  118.           clr.w     -(sp)
  119.           move.w    #Mshrink,-(sp)
  120.           trap      #1                  ; Mshrink(0, _base, keep);
  121.           add.l     #12,sp
  122. *
  123. * call C entry point function _main()
  124. *
  125.           jsr       _main
  126.  
  127. *
  128. * void _exit(code)
  129. *   int code;
  130. *
  131. * Terminate process with a return value of <code>
  132. *
  133. __exit:              ;tst.l     (sp)+   ; pop return PC off the stack
  134.           move.w    #Pterm,-(sp)        ;   leaving <code>
  135.           trap      #1                  ;   and terminate.
  136. *
  137. * operating system trap hooks for C
  138.  
  139. *
  140. * long gemdos(function, [parameter, ...])
  141. *   int function;
  142. *
  143. _gemdos:  move.l    (sp)+,traprtn       ; save return address
  144.           trap      #1                  ; do gemdos trap
  145.           bra       chkstk
  146. *
  147. * long bios(function, [parameter, ...])
  148. *   int function;
  149. *
  150. _bios:    move.l    (sp)+,traprtn       ; save return address
  151.           trap      #13                 ; do bios trap
  152.           bra       chkstk
  153. *
  154. * long xbios(function, [parameter, ...])
  155. *   int function;
  156. *
  157. _xbios:   move.l    (sp)+,traprtn       ; save return address
  158.           trap      #14                 ; do xbios trap
  159.           bra       chkstk
  160. *
  161. * int bdos(function, parameter)
  162. *   int function;
  163. *   long parameter;
  164. *
  165. _bdos:    move.l    (sp),traprtn        ; save return address
  166.           move.l    a6,(sp)             ; save old frame pointer
  167.           move.l    sp,a6               ; set up frame pointer
  168.           tst.l     -(a6)               ;  (fake "link a6,#0")
  169.           move.w    8(a6),d0            ; function code in D0.W
  170.           move.l    10(a6),d1           ; parameter value in D1.L
  171.           trap      #2                  ; do bdos trap
  172.           move.l    (sp)+,a6            ; restore old frame pointer
  173. *
  174. * check for stack overflow (done after all OS traps)
  175. *
  176. chkstk:   cmp.l     __break,sp
  177.           bgt       nosweat             ; if (_break > sp)
  178.           move.l    #stkovf,-(sp)
  179.           move.w    #Cconws,-(sp)
  180.           trap      #1                  ; report a stack overflow
  181.           addq.l    #6,sp
  182.           move.w    #-1,-(sp)
  183.           move.w    #Pterm,-(sp)
  184.           trap      #1                  ; and return error -1 (ERROR)
  185. nosweat:  move.l    traprtn,-(sp)       ; else, restore return address
  186.           rts                           ; and do a normal return.
  187. *
  188. * this call to _main ensures that it the user's main() function will be
  189. * linked, even if it is in a library.
  190. *
  191.           jsr       _main               ; NO PATH TO THIS STATEMENT
  192.  
  193. * THE FOLLOWING IS SELF MODIFYING CODE, DO NOT DISTURB
  194.           move.l    #$644c6962,$7320(sp)
  195.           moveq.l   #$31,d3
  196.           move.l    $0(a2,d0),d7
  197.  
  198. *
  199. * initialized data space
  200. *
  201.            .data
  202.            .even
  203. stkerr:   .dc.b       'Not enough memory',$d,$a,0
  204. stkovf:   .dc.b       'Stack overflow',$d,$a,0
  205.  
  206. _errno:   .dc.w       0         ; system error number
  207. __argc:   .dc.w       0         ; number of command line args
  208. __argv:   .dc.l       0         ; pointer to command line arg list
  209. *
  210. * uninitialized data space
  211. *
  212.            .bss
  213.            .even
  214. __base:   .ds.l       1         ; pointer to basepage
  215. _etext:   .ds.l       1         ; pointer to end of text segment
  216. _edata:   .ds.l       1         ; pointer to end of data segment
  217. _end:     .ds.l       1         ; pointer to end of BSS (end of program)
  218. __break:  .ds.l       1         ; pointer to stack/heap break
  219. __envp:   .ds.l       1         ; pointer to environment string
  220. traprtn:  .ds.l       1         ; storage for return PC in trap hooks
  221.  
  222.            end
  223.