Subversion Repositories SvarDOS

Rev

Rev 1556 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1558 mateusz.vi 1
; minimalist Watcom C startup routine - TINY memory model ONLY - (.COM files)
2
; kindly contributed by Bernd Boeckmann
3
 
1556 mateusz.vi 4
.8086
5
 
6
STACK_SIZE = 2048
7
 
1558 mateusz.vi 8
      DGROUP group _TEXT,_DATA,CONST,CONST2,_BSS,_EOF
1556 mateusz.vi 9
 
10
      extrn   "C",main : near
11
 
12
;      public _cstart_, _small_code_, __STK
13
      public _cstart_, _small_code_
14
 
15
_TEXT segment word public 'CODE'
16
      org   100h
17
 
18
_small_code_ label near
19
 
20
_cstart_:
21
 
22
      ; DOS puts the COM program in the largest memory block it can find
23
      ; and sets SP to the end of this block. On top of that, it reserves
24
      ; the entire memory (not only the process' block) to the program, which
25
      ; makes it impossible to allocate memory or run child processes.
26
      ; for this reasons it is beneficial to resize the memory block we occupy
27
      ; into a more reasonable value
28
 
1558 mateusz.vi 29
      ; step 1: if SP > COM size + stack size, then set SP explicitely
30
      ; POTENTIAL error: offset DGROUP:_EOF + STACK_SIZE may overflow!
31
      ; Has to be detected at linking stage, but WLINK does not catch it.
32
    adjustsp:
33
      cmp sp, offset DGROUP:_EOF + STACK_SIZE
34
      jbe resizemem   ; JBE instead of JLE (unsigned comparison!)
35
      mov sp, offset DGROUP:_EOF + STACK_SIZE
1556 mateusz.vi 36
 
37
      ; step 2: resize our memory block to sp bytes (ie. sp/16 paragraphs)
38
    resizemem:
39
      mov ah, 4ah
40
      mov bx, sp
41
      shr bx, 1
42
      shr bx, 1
43
      shr bx, 1
44
      shr bx, 1
45
      inc bx
46
      int 21h
47
 
1558 mateusz.vi 48
      ; clear _BSS to be ANSI C conformant
49
      mov di, offset DGROUP:_BSS
50
      mov cx, offset DGROUP:_EOF
51
      sub cx, di
52
      xor al, al
53
      cld
54
      rep stosb
55
 
1556 mateusz.vi 56
      call  main
57
      mov   ah, 4ch
58
      int   21h
59
 
60
; Stack overflow checking routine is absent. Remember to compile your
61
; programs with the -s option to avoid referencing __STK
62
;__STK:
63
;      ret
64
 
1558 mateusz.vi 65
_DATA segment word public 'DATA'
66
_DATA ends
1556 mateusz.vi 67
 
1558 mateusz.vi 68
CONST segment word public 'DATA'
69
CONST ends
1556 mateusz.vi 70
 
1558 mateusz.vi 71
CONST2 segment word public 'DATA'
72
CONST2 ends
1556 mateusz.vi 73
 
1558 mateusz.vi 74
_BSS  segment word public 'BSS'
75
_BSS  ends
1556 mateusz.vi 76
 
1558 mateusz.vi 77
; _EOF should be the last segment in .COM (see linker memory map)
78
; if not, someone introduced other segments, or startup.obj is not the first
79
; linker object file
80
_EOF  segment word public
81
_EOF  ends
1556 mateusz.vi 82
 
83
_TEXT ends
84
 
85
      end _cstart_
1558 mateusz.vi 86