home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / ASM / STRTUP10.ZIP / STARTUP.ZIP / STARTUP / STARTUP0.ASM next >
Encoding:
Assembly Source File  |  1997-02-28  |  8.0 KB  |  284 lines

  1.     page 60,132
  2.     title STARTUP0 -- Simple Start Up Code for .EXE Programs
  3.     name STARTUP0
  4. comment ÷
  5.         STARTUP0                                                V1.05
  6. --------------------------------------------------------------------------
  7. NAME
  8.     STARTUP0    Simple start up code for .exe programs
  9.  
  10. SYNOPSIS
  11.     extern START_UP0
  12.  
  13. DESCRIPTION
  14.     This is the startup code for all .exe and .com assembly language
  15.     programs.  Just use the SYNOPSIS above in the main function to
  16.     include the startup code in the .exe file from a .lib.    For .com
  17.     assembly language programs, this source code must be the first
  18.     assembled so that this code is the linked first.
  19.  
  20.     This procedure performs the following functions:
  21.     - Initializes the following global variables:
  22.       -- DGRP, segment address of DGROUP
  23.       -- STACK_BOTTOM, offset to stack bottom in DGROUP
  24.       -- PSP, segment address of PSP
  25.       -- ENVIRON, segment address of passed copy of the ENVIRONMENT
  26.       -- OSMAJOR, integer part of OS system
  27.       -- OSMINOR, decimal part of OS system
  28.     - Initializes DS and ES segment registers to DGROUP
  29.     - Shrinks memory down to size of program by releasing all memory
  30.       above the program
  31.  
  32. RETURNS
  33.     If main returns to the startup code.  The program terminiates with
  34.     the return code in AL.
  35.  
  36. PROGRAMING NOTES
  37.     Assembled with Microsoft MASM V6.11d with options: /Cp /W3 /Wx
  38.     /memmod={see below}.  Do not use the /NOI switch with linker.
  39.  
  40.     Written to link with any memory model
  41.     
  42.     Assemble with the following command line options to get the desired
  43.     memory models:
  44.     
  45.     Use MEMMOD to specify the memory model.
  46.     /dMEMMOD=TINY|SMALL|COMPACT|MEDIUM|LARGE|HUGE
  47.  
  48.     Written using the FORTRAN/PASCAL/BASIC calling convention so passed
  49.     parameters are pushed in the order of appearance in the proc 
  50.     declaration.
  51.  
  52. MEMORY REQUIREMENTS  (in bytes using the .286 processor directive)
  53.              Tiny   Small  Compact   Medium    Large   Huge
  54.     Code:          74      84      84    86     86    86
  55.     _Data:           0       0       0     0      0     0
  56.     Const:           0       0       0     0      0     0
  57.     _BBS:          12      12      12    12     12    12
  58.     Stack:           2       2       2     4      4     4
  59.  
  60.     Note:  _BBS data is defined in sudata.asm so that all startup
  61.     procedures can reside in the same .lib file.
  62.  
  63. CAUTION
  64.     Startup0 defines a 512 byte stack.  This should be enough for programs
  65.     who make moderate use of the stack.  If automatic variables are used
  66.     extensively, more stack space should be defined in the main module
  67.     unless using the tiny memory model.  In this case, the startup code
  68.     must be the first souce file in the build, change the size of the
  69.     STACK_SIZE define.
  70.  
  71. EXTERNAL LIBRARIES
  72.     sudata        alib?
  73.  
  74. EXTERNAL PROCEDURES
  75.     main        user defined
  76.  
  77. INTERUPTS CALLED
  78.     Int 21h 30h - Get Version Number
  79.     Int 21h 4ah - Set Memory Block Size
  80.     Int 21h 4ch - Terminate program with return code
  81.  
  82. GLOBAL NAMES
  83.     Startup_0
  84.  
  85. AUTHOR
  86.     Raymond Moon - 7 Sep 87
  87.  
  88.     Copyright (c) 1987, 1988, 1995, 1996 - MoonWare
  89.     ALL RIGHTS RESERVED
  90.  
  91. VERSION
  92.     Version    - Date        - Remarks
  93.     1.00    -  7 Sep 87    - Original
  94.     1.01    -  8 Aug 88    - Updated to MASM V5.1
  95.     1.02    -  9 Oct 88    - Added Stack_Bottom & ZZ_PRGM_TOP segment
  96.                   to determine end of data.
  97.     1.03    - 30 Oct 94    - Moved ZZ_PRGM_TOP segment position in file.
  98.                 - Removed increment in program size in paras
  99.                     as ZZ_PRGM_TOP is aligned on a para.
  100.     1.04    -  6 Oct 95    - Added TINY Model capability
  101.                 - Converted to .dosseg using __end vice
  102.                   ZZ_PRGM_TOP for end of data
  103.                 - Removed DOS 1.0 checking and termination
  104.         1.05    - 29 Dec 96     - Optimized the .com coding
  105. ==========================================================================
  106.     ÷ Comment End
  107.  
  108. ;-----------------------------
  109. ; A    Make the small memory model the default
  110.  
  111. ifndef    memmod
  112. memmod    equ    <small>
  113. endif
  114.  
  115. ;-----------------------------
  116. ; B    Include the processor, memory model, associate ES register with
  117. ;    DGROUP, and specify DOS segment order.
  118.  
  119.     include procesor.inc
  120. %    .MODEL memmod, FORTRAN
  121.     assume es:DGROUP
  122.     .dosseg
  123.  
  124. ;----------------------------
  125. ; C    Required includes
  126.  
  127.     include startup.inc
  128.  
  129. ;----------------------------
  130. ; D    Define any required equates
  131.  
  132. STACK_SIZE    equ    512
  133.  
  134. ;=========================================================================
  135. ;    DATA
  136. ;=========================================================================
  137.  
  138. ;----------------------------
  139. ; E     Define __end
  140.  
  141. externdef    __end:byte
  142.  
  143. ;=========================================================================
  144. ;    STACK
  145. ;=========================================================================
  146. ; F     Define the stack if memory model is not TINY
  147.  
  148. if    @Model    NE  1
  149.  
  150.     .STACK    STACK_SIZE    ; Define a nominal stack
  151. endif
  152.  
  153. ;=========================================================================
  154. ;    PSP
  155. ;=========================================================================
  156. ; G     Define segment for addressing information in the PSP
  157.  
  158. PSP_SEG    segment at 00h
  159.  
  160.     org    2h
  161. NEXTPARA_PTR    dw    ?        ; Segment address of next memory para
  162.     org    2ch
  163. ENVIRON_PTR    dw    ?        ; Segment address of Environment
  164.  
  165. ;=========================================================================
  166. ;    CODE
  167. ;=========================================================================
  168. ; H     Put the called main procedure in the proper relationship to the
  169. ;    startup code.
  170.  
  171. if    @CodeSize
  172. extrn    Main:far
  173. .CODE
  174. else
  175. .CODE
  176. extrn    Main:near
  177. endif
  178.  
  179. ;-----------------------------
  180. ; I     Include org statement if tiny model
  181.  
  182. if    @Model    EQ 1
  183.  
  184.     org    100h
  185. endif
  186.  
  187. ;-----------------------------
  188. ; J     Start the START_UP0 code.  Make it a far procedure so error return
  189. ;    will work.
  190.  
  191. Start_Up0    proc    far
  192.  
  193. ;-----------------------------
  194. ; K     First, initialize global variables.  Set DS to DGROUP.
  195. ;    Skip DGROUP code for .com(Tiny) memory models
  196.  
  197. if    @Model    NE 1
  198.  
  199.     mov    ax, DGROUP        ; Get seg address of DGROUP
  200.     mov    ds, ax            ; Initialize DS segment register
  201.     mov    DGRP, ax        ; Initialize DGRP
  202. assume    es:PSP_SEG
  203. else
  204.     mov    DGRP, ds        ; Initialize DGRP
  205. endif
  206.     mov    PSP, es            ; Initialize PSP
  207. if    @Model    eq 1
  208. assume    ds:PSP_SEG
  209. endif
  210.     mov    bx, ENVIRON_PTR     ; Get segment address of environment
  211.     mov    cx, NEXTPARA_PTR    ; Get segment address of next memory
  212. if    @Model    eq 1
  213. assume    ds:DGROUP
  214. endif
  215.     mov    ENVIRON, bx        ; Initialize ENVIRON
  216.     mov    NEXTPARA, cx        ; Initialize NEXTPARA
  217.     mov    ah, 30h            ; Get DOS version number
  218.     int    21h            ; Call DOS
  219.     mov    OSMAJOR, al        ; Save major version number
  220.     mov    OSMINOR, ah        ; Save minor version number
  221.     
  222. ;----------------------------
  223. ; L     Combine the stack into DGROUP so that it is addressable from DGROUP.
  224. ;    Initialize STACK_BOTTOM.
  225.  
  226.     lea    bx, __end        ; Get pointer to end of data
  227.  
  228. if    @Model    eq    1        ; Ensure that Stack bottom at paragraph
  229.         add     bx, 15                  ; Add to ensure next para if necessary
  230.         and     bx, 0fff0h              ; Truncate to a paragraph boundary
  231. endif
  232.  
  233.     mov    STACK_BOTTOM, bx    ; Save it
  234.  
  235. if    @Model    eq    1        ; Get stack size
  236.     add    bx, STACK_SIZE        ; DI = stack size
  237. else
  238.     add    bx, sp            ; DI = stack size
  239. endif
  240.  
  241.     mov    dx, ds            ; Get DGROUP segment address
  242.     mov    ss, dx            ; Reset SS
  243.     mov    sp, bx            ; Reset SP
  244.  
  245. ;----------------------------
  246. ; M     Release all memory above program.  Calculate the size of the program
  247. ;    in paragraphs (16 bits).  BX starts with Stack Top
  248.  
  249.     mov    cl, 4            ; Convert to #para by dividing by 16
  250.     shr    bx, cl            ; Do division by bit shifting
  251.  
  252. if    @Model    ne    1        ; Needed in non-Tiny memory models
  253.     mov    ax, ds            ; AX => DGROUP
  254.     sub    ax, PSP            ; AX = # para for code
  255.     add    bx, ax            ; BX = # para in program
  256. endif
  257.  
  258.     mov    ah, 4ah            ; Request DOS set block
  259.     int    21h            ; Call DOS
  260.  
  261. if    @Model    ne  1
  262. ;-----------------------------
  263. ; N     Initialize ES to DGROUP
  264.  
  265.     mov    es, DGRP
  266. endif
  267.  
  268. ;-----------------------------
  269. ; O     call MAIN
  270.  
  271.     call    Main            ; Call MAIN procedure
  272.  
  273. ;----------------------------
  274. ; P     If main returns, the program is to terminate with the return code
  275. ;    in AL
  276.  
  277.     mov    ah, 4ch            ; End process
  278.     int    21h            ; Call DOS
  279.  
  280. Start_Up0    endp
  281.  
  282.     end    Start_Up0        ; Indicate that START_UP0 is the start
  283.                     ; of the program
  284.