home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_03 / 9n03071a < prev    next >
Text File  |  1990-12-20  |  5KB  |  175 lines

  1.  
  2.         page    60,132
  3.         title    start1 - simple start-up code
  4. ;***********************************************************************;
  5. ;                                    ;
  6. ;                start1.asm                ;
  7. ;                                    ;
  8. ;      start-up for stand-alone C code - Turbo C/C++ version        ;
  9. ;                                    ;
  10. ;             created: November 17, 1990                ;
  11. ;                                    ;
  12. ;             Copyright (c) 1990                ;
  13. ;            Pasquale J. Villani                ;
  14. ;            All Rights Reserved                ;
  15. ;                                    ;
  16. ;***********************************************************************;
  17.  
  18. _TEXT        segment    byte public 'CODE'
  19.         assume    cs:_TEXT                ; small model
  20. _TEXT        ends
  21.  
  22. _DATA        segment word public 'DATA'
  23. DGROUP        group    _DATA,_BSS,_BSSEND            ; small model
  24.         assume    ds:DGROUP,ss:DGROUP
  25. _DATA        ends
  26.  
  27. _BSS        segment word public 'BSS'
  28. _BSS        ends
  29.  
  30. _BSSEND        segment byte public 'STACK'
  31. _BSSEND        ends
  32.  
  33. _TEXT        segment    byte public 'CODE'
  34.  
  35.         extrn    _main:near        ; C entry point
  36.         extrn    __hdw_init:near        ; Hardware init (asm or C)
  37.  
  38. ;
  39. ; entry:
  40. ;    Our ROM module's entry point. This should be used as the
  41. ;    'jmp far ptr entry' at the top of ROM.
  42. ;
  43. entry        proc    far
  44.         public    entry;
  45.  
  46.         ;
  47.         ; First order of business is the initialization of the
  48.         ; machine itself. This is basically the initialization
  49.         ; of any segment registers that the processor needs for
  50.         ; proper operation. On an 80X86 class processor, this is
  51.         ; usually initializing the segmentation registers, going
  52.         ; to the proper mode (i. e. protected, etc.) for
  53.         ; 80[234]86 processors, etc. This file is for an 8086 or
  54.         ; 8088 machine (or any other in real mode). Segment
  55.         ; registers are the only processor initialization.
  56.         ;
  57.         ; NOTE: the cs register is not initialized because the
  58.         ; processor itself initializes the first cs, followed
  59.         ; by the far jump changing it to point to segment entry.
  60.         ;
  61.         cli            ; prevent interrupts while starting
  62.         mov    ax,DGROUP    ; initialize the segment registers
  63.         mov    ds,ax
  64.         mov    ss,ax
  65.         mov    es,ax
  66.         ;
  67.         ; Once the processor is initialized, it usually requires
  68.         ; the proper setting of the stack pointer(s). For the
  69.         ; 8086 case, all we need is to initialize the sp and bp
  70.         ; registers.
  71.         ;
  72.         mov    sp,offset DGROUP:tos
  73.         mov    bp,sp
  74.         ;
  75.         ; We are now ready to make any special hardware
  76.         ; initialization. This includes initializing the interrupt
  77.         ; vector table, moving the data segment to RAM and
  78.         ; initializing the BSS to zero to match  C programming
  79.         ; conventions. This should end with a call to __hdw_init
  80.         ; to initialize I/O, etc.
  81.         call    near ptr __hdw_init    ; initialize the system without ints
  82.         sti            ; now enable them
  83.         ;
  84.         ; After the hardware is initialized, we wish to enter
  85.         ; main(). This is accomplished by building the stack for
  86.         ; envp, argv and argc. This is a convienent mechanism
  87.         ; for passing system configuration, dip switch settings,
  88.         ; etc. to our C code.
  89.         ;
  90.         mov    ax,offset env    ; envp for this example
  91.         push    ax
  92.         mov    ax,offset args    ; argv for this example
  93.         push    ax
  94.         mov    ax,2        ; argc = 2
  95.         push    ax
  96.         call    near ptr _main    ; finally enter C code
  97.         add    sp,6        ; clean stack
  98.         ;
  99.         ; If main should return, we need a mechanism to catch this
  100.         ; condition. Depending on the system design, we should
  101.         ; alarm, restart, etc., therefore, the following code
  102.         ; is very implementation dependant. In our case, all we
  103.         ; want to do is call exit with a special -1 exit code.
  104.         ;
  105.         mov    ax,-1
  106.         push    ax
  107.         call    near ptr _exit
  108.         jmp    $        ; belt and suspenders
  109.  
  110. entry        endp
  111.  
  112.  
  113. ;
  114. ; exit:
  115. ;    Where to go for error conditions (typically) or shutdown
  116. ;    conditions. This code is very implementaion sensitive, since
  117. ;    we may want to light LEDs and sound alarms. In this case, we'll
  118. ;    only silently stop. In certain cases, we may want to only
  119. ;    display a message and restart. This should be decided upon
  120. ;    during system design.
  121. ;
  122. _exit        proc    near
  123.         public    _exit
  124.         
  125.         cli
  126.         hlt
  127.         jmp    _exit
  128.  
  129. _exit        endp
  130.  
  131.  
  132. _TEXT        ends
  133.  
  134.  
  135. _DATA        segment word public 'DATA'
  136. env0    label    byte
  137.         db    'ENV=ROM',0
  138. arg0    label    byte
  139.         db    'ex0s', 0
  140. arg1    label    byte
  141.         db    'rom', 0
  142. env    label    word
  143.     dw    DGROUP:env0
  144.     dw    0
  145. args    label    word
  146.     dw    DGROUP:arg0
  147.     dw    DGROUP:arg1
  148.     dw    0
  149. _DATA        ends
  150.  
  151. ;
  152. ; The stack segment. This size should be adjusted to fit any particular
  153. ; system requirements.
  154. ;
  155. _STACK        SEGMENT
  156.               dw      512 dup (?)
  157. tos        label    byte
  158.         dd    (?)            ; safety area
  159. last        label    word            ; must always be end of stack area
  160. _STACK  ENDS
  161.  
  162. _BSS        segment word public 'BSS'
  163. _errno        label    word            ; c lib errno
  164.         public    _errno
  165.         dw    (?)
  166. _BSS        ends
  167.  
  168. _BSSEND segment byte public 'STACK'
  169. _BSSEND ends
  170.  
  171.  
  172.     end
  173.  
  174.  
  175.