home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_07_04 / v7n4079a.txt < prev    next >
Text File  |  1989-03-10  |  7KB  |  279 lines

  1.         page    60, 132
  2.         name    startup
  3.         title   ROM System Startup Code for Turbo C 2.0
  4.  
  5. ;
  6. ;       ROM System Startup Code for Turbo C 2.0
  7. ;       Copyright (C) 1987, 1988, 1989 by Paradigm Systems.
  8. ;       All rights reserved.
  9. ;
  10. ;       Assemble with MASM as follows:
  11. ;
  12. ;               masm tc /mx /d__MODEL__ ;
  13. ;
  14. ;       where
  15. ;               __MODEL__ is one of the following:
  16. ;
  17. ;               __SMALL__       Small memory model
  18. ;               __COMPACT__     Compact memory model
  19. ;               __MEDIUM__      Medium memory model
  20. ;               __LARGE__       Large memory model
  21. ;
  22. ;       If no memory model is specified, the small memory
  23. ;       model is assumed and a warning is issued.
  24. ;
  25.  
  26. ;
  27. ;       Segment and group declarations.  The order of these
  28. ;       declarations is used to control the order of
  29. ;       segments in the .EXE file since the Microsoft and
  30. ;       Borland linkers copy segments in the same order
  31. ;       they are encountered in the object files.
  32. ;
  33. ;       The default Borland model is to have the stack
  34. ;       after the heap in the small and medium memory
  35. ;       models.  This startup assumes that the stack
  36. ;       will follow immediately after the BSS area.
  37. ;
  38. ;       Make sure that this startup module is specified
  39. ;       first when linking the application.  Requires
  40. ;       MASM 5.10 to assemble.
  41. ;
  42.  
  43.  
  44. _text   segment byte    public  'CODE'
  45. _text   ends
  46. _etext  segment para    public  'CODEEND'
  47. _etext  ends
  48. _data   segment para    public  'DATA'
  49. _data   ends
  50. _bss    segment para    public  'BSS'
  51. _bss    ends
  52. _bssend segment byte    public  'BSSEND'
  53. _bssend ends
  54. _stack  segment para    stack   'STACK'
  55. _stack  ends
  56. romdata segment para    public  'ROMDATA'
  57. romdata ends
  58.  
  59. IFDEF           __SMALL__
  60. dgroup  group   _data, _bss, _bssend, _stack
  61. assume  cs:_text, ds:dgroup, es:nothing, ss:dgroup
  62. ELSEIFDEF       __COMPACT__
  63. dgroup  group   _data, _bss, _bssend
  64. assume  cs:_text, ds:dgroup, es:nothing, ss:_stack
  65. ELSEIFDEF       __MEDIUM__
  66. dgroup  group   _data, _bss, _bssend, _stack
  67. assume  cs:_text, ds:dgroup, es:nothing, ss:dgroup
  68. ELSEIFDEF       __LARGE__
  69. dgroup  group   _data, _bss, _bssend
  70. assume  cs:_text, ds:dgroup, es:nothing, ss:_stack
  71. ELSE
  72. dgroup  group   _data, _bss, _bssend, _stack
  73. assume  cs:_text, ds:dgroup, es:nothing, ss:dgroup
  74. ENDIF
  75.  
  76. ;
  77. ;       Make up the correct external references that
  78. ;       depend on the memory model
  79. ;
  80.  
  81.         .xlist
  82. IF1                             ; Only expand these
  83. IFDEF           __SMALL__       ; on pass 1
  84.         extrn   _main : near
  85.         %out    Assembling for small memory model
  86. ELSEIFDEF       __COMPACT__
  87.         extrn   _main : near
  88.         %out    Assembling for compact memory model
  89. ELSEIFDEF       __MEDIUM__
  90.         extrn   _main : far
  91.         %out    Assembling for medium memory model
  92. ELSEIFDEF       __LARGE__
  93.         extrn   _main : far
  94.         %out    Assembling for large memory model
  95. ELSE
  96.         %out    Warning: No model - small model assumed
  97.         extrn   _main : near
  98. ENDIF
  99. ENDIF
  100.         .list
  101.  
  102.         subttl  Startup code for Turbo C V2.0
  103.         page
  104. _text   segment
  105.  
  106. public  start
  107.  
  108. start   proc    far
  109.  
  110.         cli                             ; Disable interrupts
  111.         cld
  112.  
  113. ;
  114. ;       Set up the stack according to the memory model.
  115. ;
  116. IFDEF           __SMALL__
  117.         mov     ax, dgroup
  118.         mov     ss, ax
  119.         mov     sp, offset DGROUP:tos
  120. ELSEIFDEF       __MEDIUM__
  121.         mov     ax, dgroup
  122.         mov     ss, ax
  123.         mov     sp, offset DGROUP:tos
  124. ELSEIFDEF       __COMPACT__
  125.         mov     ax, _stack
  126.         mov     ss, ax
  127.         mov     sp, offset tos
  128. ELSEIFDEF       __LARGE__
  129.         mov     ax, _stack
  130.         mov     ss, ax
  131.         mov     sp, offset tos
  132. ELSE
  133.         mov     ax, dgroup
  134.         mov     ss, ax
  135.         mov     sp, offset DGROUP:tos
  136. ENDIF
  137.  
  138. ;
  139. ;       Prepare the segment registers for initialization.
  140. ;
  141.         mov     ax, dgroup      ; Get the target segment
  142.         mov     es, ax          ; for DGROUP
  143.  
  144.         mov     ax, romdata     ; Get the segment for the
  145.         mov     ds, ax          ; copy of DGROUP
  146.  
  147. ;
  148. ;       Copy DGROUP from its position in ROM (just after
  149. ;       the code class) to the target address in RAM
  150. ;
  151.         mov     si, offset DGROUP:idata
  152.         mov     di, si
  153.         mov     cx, offset DGROUP:bdata
  154.         sub     cx, di
  155.         rep     movsb
  156.  
  157.         push    es
  158.         pop     ds
  159.  
  160. ;
  161. ;       Zero out the BSS area
  162. ;
  163.         xor     al, al
  164.         mov     di, offset DGROUP:bdata
  165.         mov     cx, offset DGROUP:edata
  166.         sub     cx, di
  167.         rep     stosb
  168.  
  169. ;
  170. ;       Call the C language entry point
  171. ;
  172.         sti
  173.         call    _main           ; Call the Turbo C main()
  174.  
  175. ;
  176. ;       Restart the process should main() return
  177. ;
  178.         jmp     start
  179.  
  180. start   endp
  181.  
  182. ;
  183. ;       These routines are defined to satisfy any error
  184. ;       handling routines that might need to give up on
  185. ;       an error.  An example of this would be the
  186. ;       stack checking routine calling abort if a stack
  187. ;       overflow has been detected.
  188. ;
  189.  
  190. public  _exit, __exit, _abort
  191.  
  192. _abort  proc                            ; User customized
  193.         jmp     start
  194. _abort  endp
  195.  
  196. _exit   proc                            ; User customized
  197.         jmp     start
  198. _exit   endp
  199.  
  200. __exit  proc                            ; User customized
  201.         jmp     start
  202. __exit  endp
  203.  
  204. ;
  205. ;       This is code given control when Turbo C detects
  206. ;       a stack overflow.  It simply sets up a new stack
  207. ;       (it assumes that BP contains the original value
  208. ;       of the stack) and exits.
  209. ;
  210.  
  211. public  OVERFLOW@
  212.  
  213. OVERFLOW@       proc
  214.         jmp     start
  215. OVERFLOW@       endp
  216.  
  217. _text   ends
  218.  
  219.         subttl  Segment Declarations
  220.         page
  221. _etext  segment
  222. public  tend
  223. tend    label   byte            ; Mark the segment end
  224.         db      ?
  225. _etext  ends
  226.  
  227. romdata segment
  228. public  rdata
  229. rdata   label   byte
  230. romdata ends
  231.  
  232. _data   segment
  233. public  idata,  _errno
  234. idata   label   byte
  235. _errno          dw      0
  236.  
  237. IFDEF           __SMALL__
  238. public  ___brklvl
  239. ___brklvl       dw offset DGROUP:edata
  240. ELSEIFDEF       __MEDIUM__
  241. public  ___brklvl
  242. ___brklvl       dw offset DGROUP:edata
  243. ELSEIFDEF       __COMPACT__
  244. public  __stklen
  245. __stklen        dw offset tos
  246. ELSEIFDEF       __LARGE__
  247. public  __stklen
  248. __stklen        dw offset tos
  249. ELSE
  250. public  __brklvl
  251. __brklvl        dw offset DGROUP:edata
  252. ENDIF
  253.  
  254. _data   ends
  255.  
  256. _bss    segment
  257. public  bdata
  258. bdata   label   byte            ; Mark the start of
  259.                                 ; the _BSS (and the
  260. _bss    ends                    ; end of _DATA
  261.  
  262. _bssend segment
  263. public  edata
  264. edata   label   byte            ; Mark the end of
  265. IFDEF           __COMPACT__     ; the BSS
  266.         db      ?
  267. ELSEIFDEF       __LARGE__
  268.         db      ?
  269. ENDIF
  270. _bssend ends
  271.  
  272. _stack  segment
  273. public  tos                  ; Make the TOS visible
  274.         dw      256 dup (?)  ; Declare the stack size
  275. tos     label   word         ; Define the top of stack
  276. _stack  ends
  277.  
  278.         end     start
  279.