Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
8687 turbocat 1
format ELF
2
section '.text' executable
3
public start
4
public start as '_start'
5
;extrn mf_init
6
extrn main
7
;include 'debug2.inc'
8
include 'proc32.inc'
9
include 'macros.inc'
10
include 'dll.inc'
11
__DEBUG__=0
12
 
13
;start_:
14
virtual at 0
15
	db 'MENUET01' ; 1. Magic number (8 bytes)
16
	dd 0x01       ; 2. Version of executable file
17
	dd start       ; 3. Start address
18
imgsz	dd 0x0	      ; 4. Size of image
19
	dd 0x100000   ; 5. Size of needed memory
20
	dd 0x100000   ; 6. Pointer to stack
21
hparams dd 0x0	      ; 7. Pointer to program arguments
22
hpath	dd 0x0	      ; 8. Pointer to program path
23
end virtual
24
 
25
start:
26
;DEBUGF 'Start programm\n'
27
    ;init heap of memory
28
    mov eax,68
29
    mov ebx,11
30
    int 0x40
31
 
32
    mov  [argc], 0
33
    mov  eax, [hparams]
34
    test eax, eax
35
    jz	 .without_path
36
    mov  eax, path
37
    cmp	 word ptr eax, 32fh  ; '/#3'  UTF8
38
    jne  .without_path
39
    mov  word ptr eax, 12fh  ; '/#1'  fix to CP866
40
.without_path:
41
    mov  esi, eax
42
    call push_param
43
; retrieving parameters
44
    mov  esi, params
45
    xor  edx, edx  ; dl - идёт параметр(1) или разделители(0)
46
		   ; dh - символ с которого начался параметр (1 кавычки, 0 остальное)
47
    mov  ecx, 1    ; cl = 1
48
		   ; ch = 0  просто ноль
49
.parse:
50
    lodsb
51
    test al, al
52
    jz	 .run
53
    test dl, dl
54
    jnz  .findendparam
55
		     ;{если был разделитель
56
    cmp  al, ' '
57
    jz	 .parse  ;загружен пробел, грузим следующий символ
58
    mov  dl, cl  ;начинается параметр
59
    cmp  al, '"'
60
    jz	 @f	 ;загружены кавычки
61
    mov  dh, ch     ;параметр без кавычек
62
    dec  esi
63
    call push_param
64
    inc  esi
65
    jmp  .parse
66
 
67
  @@:
68
    mov  dh, cl     ;параметр в кавычеках
69
    call push_param ;если не пробел значит начинается какой то параметр
70
    jmp  .parse     ;если был разделитель}
71
 
72
.findendparam:
73
    test dh, dh
74
    jz	 @f ; без кавычек
75
    cmp  al, '"'
76
    jz	 .clear
77
    jmp  .parse
78
  @@:
79
    cmp  al, ' '
80
    jnz  .parse
81
 
82
.clear:
83
    lea  ebx, [esi - 1]
84
    mov  [ebx], ch
85
    mov  dl, ch
86
    jmp  .parse
87
 
88
.run:
89
    call load_imports
90
    push argv
91
    push [argc]
92
    call main
93
.exit:
94
    xor  eax,eax
95
    dec  eax
96
    int  0x40
97
    dd	 -1
98
.crash:
99
    jmp  .exit
100
;============================
101
push_param:
102
;============================
103
;parameters
104
;  esi - pointer
105
;description
106
;  procedure increase argc
107
;  and add pointer to array argv
108
;  procedure changes ebx
109
    mov  ebx, [argc]
110
    cmp  ebx, max_parameters
111
    jae  .dont_add
112
    mov  [argv+4*ebx], esi
113
    inc  [argc]
114
.dont_add:
115
    ret
116
;==============================
117
 
118
;==============================
119
load_imports:
120
;==============================
121
;parameters
122
;  none
123
;description
124
;  imports must be located at end of image (but before BSS sections)
125
;  the address of end of imports (next byte after imports) is located in imgsz
126
;  look at each import from that address up to illegal import
127
;  legal import is such that:
128
;    first pointer points to procedure name
129
;      and is smaller than imgsz
130
;    second pointer points lo library name, starting with 0x55, 0xAA
131
;      and is smaller than imgsz
132
;  each library should be initialized as appropriate, once
133
;  so as library is initialized, its name will be replaced 0x00
134
    mov ebx, [imgsz]                ; byte after imports
135
.handle_next_import:
136
    sub ebx, 4                      ; ebx = pointer to pointer to library name
137
    mov esi, dword[ebx]             ; esi = pointer to library name
138
    push ebx
139
    push esi
140
    call load_library               ; eax = pointer to library exports
141
    pop esi
142
    pop ebx
143
    test eax, eax
144
    jz .done
145
    sub ebx, 4                      ; ebx = pointer to pointer to symbol name
146
    push ebx
147
    stdcall dll.GetProcAddress, eax, dword[ebx]
148
    pop ebx
149
    test eax, eax
150
    jz .fail
151
    mov dword[ebx], eax
152
    jmp .handle_next_import
153
.done:
154
    ret
155
.fail:
156
    ret
157
;==============================
158
 
159
;==============================
160
load_library:
161
;==============================
162
;parameters
163
;  ebx: library name address
164
;description
165
;  each library should be initialized as appropriate, once
166
;  so as library is initialized, its name will be replaced 0x00
167
;  and 4 next bytes will be set to address of library
168
    ; first two bytes of library name must be 0x55, 0xAA (is like a magic)
169
    cld                ; move esi further, not back
170
    cmp esi, [imgsz]
171
    ja .fail
172
    lodsb              ; al = first byte of library name
173
    cmp al, 0x55
174
    jne .fail
175
    lodsb              ; al = second byte of library name
176
    cmp al, 0xAA
177
    jne .fail
178
    lodsb              ; al = third byte of library name (0x00 if the library is already loaded)
179
    test al, al
180
    jnz .load
181
    lodsd              ; if we here, then third byte is 0x00 => address of library is in next 4 bytes
182
    ; now eax contains address of library
183
    ret
184
.load:
185
    dec esi ; we checked on 0 before, let's go back
186
    mov eax, 68
187
    mov ebx, 19
188
    mov ecx, esi
189
    int 0x40           ; eax = address of exports
190
    mov byte[esi], 0   ; library is loaded, let's place 0 in first byte of name
191
    mov [esi + 1], eax ; now next 4 bytes of library name are replaced by address of library
192
    ; call lib_init
193
    stdcall dll.GetProcAddress, eax, lib_init_str ; eax = address of lib_init
194
    test eax, eax
195
    jz .ret
196
    stdcall dll.Init, eax
197
.ret:
198
    mov eax, [esi + 1] ; put address of library into eax
199
    ret
200
.fail:
201
    mov eax, 0
202
    ret
203
 
204
; ==== memmove for tcc ======
205
 
206
proc memmove c, to:dword,from:dword,count:dword
207
 
208
    push esi
209
    push edi
210
	mov ecx,[count]
211
	test ecx,ecx
212
	jz no_copy_block_
213
		mov esi,[from]
214
		mov edi,[to]
215
		cmp esi, edi
216
		je no_copy_block_
217
		jg copy_
218
            add	esi, ecx
219
            add	edi, ecx
220
            dec	esi
221
            dec	edi
222
            std
223
copy_:
224
		rep movsb
225
        cld
226
no_copy_block_:
227
 
228
    pop edi
229
    pop esi
230
    mov eax,[to]
231
	ret
232
endp
233
 
234
 
235
;==============================
236
 
237
lib_init_str db 'lib_init', 0
238
 
239
public argc as '__argc'
240
public params as '__argv'
241
public path as '__path'
242
public memmove
243
 
244
section '.bss'
245
buf_len = 0x400
246
max_parameters=0x20
247
argc	 rd 1
248
argv	 rd max_parameters
249
path	 rb buf_len
250
params	 rb buf_len
251
 
252
;section '.data'
253
;include_debug_strings ; ALWAYS present in data section