Subversion Repositories SvarDOS

Rev

Rev 1556 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1556 Rev 1558
Line -... Line 1...
-
 
1
; minimalist Watcom C startup routine - TINY memory model ONLY - (.COM files)
-
 
2
; kindly contributed by Bernd Boeckmann
-
 
3
 
1
.8086
4
.8086
2
 
5
 
3
STACK_SIZE = 2048
6
STACK_SIZE = 2048
4
 
7
 
5
      dgroup group _TEXT,_DATA,CONST,CONST2,_STACK,_BSS
8
      DGROUP group _TEXT,_DATA,CONST,CONST2,_BSS,_EOF
6
 
9
 
7
      extrn   "C",main : near
10
      extrn   "C",main : near
8
 
11
 
9
;      public _cstart_, _small_code_, __STK
12
;      public _cstart_, _small_code_, __STK
10
      public _cstart_, _small_code_
13
      public _cstart_, _small_code_
Line 21... Line 24...
21
      ; the entire memory (not only the process' block) to the program, which
24
      ; the entire memory (not only the process' block) to the program, which
22
      ; makes it impossible to allocate memory or run child processes.
25
      ; makes it impossible to allocate memory or run child processes.
23
      ; for this reasons it is beneficial to resize the memory block we occupy
26
      ; for this reasons it is beneficial to resize the memory block we occupy
24
      ; into a more reasonable value
27
      ; into a more reasonable value
25
 
28
 
26
      ; step 1: if SP is higher than my top_of_stack, then set SP explicitely
29
      ; step 1: if SP > COM size + stack size, then set SP explicitely
-
 
30
      ; POTENTIAL error: offset DGROUP:_EOF + STACK_SIZE may overflow!
27
      cmp sp, top_of_stack
31
      ; Has to be detected at linking stage, but WLINK does not catch it.
28
      jle resizemem
32
    adjustsp:
-
 
33
      cmp sp, offset DGROUP:_EOF + STACK_SIZE
-
 
34
      jbe resizemem   ; JBE instead of JLE (unsigned comparison!)
29
      mov sp, top_of_stack
35
      mov sp, offset DGROUP:_EOF + STACK_SIZE
30
 
36
 
31
      ; step 2: resize our memory block to sp bytes (ie. sp/16 paragraphs)
37
      ; step 2: resize our memory block to sp bytes (ie. sp/16 paragraphs)
32
    resizemem:
38
    resizemem:
33
      mov ah, 4ah
39
      mov ah, 4ah
34
      mov bx, sp
40
      mov bx, sp
Line 37... Line 43...
37
      shr bx, 1
43
      shr bx, 1
38
      shr bx, 1
44
      shr bx, 1
39
      inc bx
45
      inc bx
40
      int 21h
46
      int 21h
41
 
47
 
-
 
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
 
42
      call  main
56
      call  main
43
      mov   ah, 4ch
57
      mov   ah, 4ch
44
      int   21h
58
      int   21h
45
 
59
 
46
; Stack overflow checking routine is absent. Remember to compile your
60
; Stack overflow checking routine is absent. Remember to compile your
Line 58... Line 72...
58
CONST2  ends
72
CONST2 ends
59
 
73
 
60
_BSS    segment word public 'BSS'
74
_BSS  segment word public 'BSS'
61
_BSS    ends
75
_BSS  ends
62
 
76
 
63
_STACK  segment para public 'BSS'
-
 
64
        db      (STACK_SIZE) dup(0) ; set this explicitely to zero, otherwise
-
 
65
                                    ; static variables are not properly
-
 
66
                                    ; initialized. this makes the COM file
77
; _EOF should be the last segment in .COM (see linker memory map)
67
                                    ; much bigger, but it is irrelevant if it
-
 
68
                                    ; is UPXed afterwards anyway. If you care,
-
 
69
                                    ; then you may zero out this area in
78
; if not, someone introduced other segments, or startup.obj is not the first
70
                                    ; code instead (before calling main)
79
; linker object file
71
        top_of_stack:
80
_EOF  segment word public
72
_STACK  ends
81
_EOF  ends
73
 
82
 
74
_TEXT ends
83
_TEXT ends
75
 
84
 
76
      end _cstart_
85
      end _cstart_
-
 
86