Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4842 | eAndrew | 1 | ; PRIVATE FIELDS |
2 | |||
3 | __shell.buf__ rd 1 |
||
4 | __shell.name__ rb 16 |
||
5 | __shell.str__ rb 16 |
||
6 | |||
7 | ;----------------------------------- |
||
8 | ; a = 0 |
||
9 | ;----------------------------------- |
||
10 | |||
11 | macro clear a { |
||
12 | xor a, a |
||
13 | } |
||
14 | |||
15 | ;----------------------------------- |
||
16 | ; if a == b then c |
||
17 | ;----------------------------------- |
||
18 | |||
19 | macro cmpe a, b, c { |
||
20 | cmp a, b |
||
21 | je c |
||
22 | } |
||
23 | |||
24 | ;----------------------------------- |
||
25 | ; if a != b then c |
||
26 | ;----------------------------------- |
||
27 | |||
28 | macro cmpne a, b, c { |
||
29 | cmp a, b |
||
30 | jne c |
||
31 | } |
||
32 | |||
33 | ;----------------------------------- |
||
34 | ; Shell`s initialization |
||
35 | ; without arguments |
||
36 | ;----------------------------------- |
||
37 | |||
38 | shell.init: |
||
39 | pusha |
||
40 | |||
41 | ; Get PID |
||
42 | mov eax, 68 |
||
43 | mov ebx, 11 |
||
44 | int 64 |
||
45 | |||
46 | mov eax, 68 |
||
47 | mov ebx, 12 |
||
48 | mov ecx, 1024 |
||
49 | int 64 |
||
50 | |||
51 | mov esi, eax |
||
52 | |||
53 | mov eax, 9 |
||
54 | mov ebx, esi |
||
55 | mov ecx, -1 |
||
56 | int 64 |
||
57 | |||
58 | mov eax, dword [esi + 30] |
||
59 | mov [@pid], eax |
||
60 | |||
61 | mov eax, 68 |
||
62 | mov ebx, 13 |
||
63 | mov ecx, esi |
||
64 | int 64 |
||
65 | |||
66 | ; Convert PID to STR |
||
67 | mov eax, [@pid] |
||
68 | mov ebx, 10 |
||
69 | clear ecx |
||
70 | @@: |
||
71 | clear edx |
||
72 | div ebx |
||
73 | push edx |
||
74 | inc ecx |
||
75 | cmpne eax, 0, @b |
||
76 | |||
77 | mov ebx, __shell.name__ |
||
78 | @@: |
||
79 | pop eax |
||
80 | add al, "0" |
||
81 | mov [ebx], al |
||
82 | inc ebx |
||
83 | loop @b |
||
84 | |||
85 | ; Add postfix |
||
86 | mov [ebx + 0], dword "-SHE" |
||
87 | mov [ebx + 4], word "LL" |
||
88 | mov [ebx + 6], byte 0 |
||
89 | |||
90 | ; Open buffer |
||
91 | mov eax, 68 |
||
92 | mov ebx, 22 |
||
93 | mov ecx, __shell.name__ |
||
94 | mov edx, 4096 |
||
95 | mov esi, 101b |
||
96 | int 64 |
||
97 | |||
98 | mov [__shell.buf__], eax |
||
99 | |||
100 | mov eax, 5 |
||
101 | mov ebx, 5 |
||
102 | int 64 |
||
103 | |||
104 | popa |
||
105 | ret |
||
106 | |||
107 | ;----------------------------------- |
||
108 | ; Wait answer from shell |
||
109 | ; |
||
110 | ; INPUT: |
||
111 | ; edi - shell-buffer |
||
112 | ;----------------------------------- |
||
113 | |||
114 | shell.wait: |
||
115 | pusha |
||
116 | |||
117 | @@: |
||
118 | mov eax, 5 |
||
119 | mov ebx, 1 |
||
120 | int 64 |
||
121 | |||
122 | cmpne [edi], byte 0, @b |
||
123 | |||
124 | popa |
||
125 | ret |
||
126 | |||
127 | ;----------------------------------- |
||
128 | ; Exit from shell |
||
129 | ; without arguments |
||
130 | ;----------------------------------- |
||
131 | |||
132 | shell.destroy: |
||
133 | pusha |
||
134 | |||
135 | mov edi, [__shell.buf__] |
||
136 | mov [edi], byte 1 |
||
137 | call shell.wait |
||
138 | |||
139 | mov eax, 68 |
||
140 | mov ebx, 23 |
||
141 | mov ecx, __shell.name__ |
||
142 | int 64 |
||
143 | |||
144 | popa |
||
145 | ret |
||
146 | |||
147 | ;----------------------------------- |
||
148 | ; Print char to shell |
||
149 | ; |
||
150 | ; INPUT: |
||
151 | ; al - char |
||
152 | ;----------------------------------- |
||
153 | |||
154 | shell.print_char: |
||
155 | pusha |
||
156 | |||
157 | mov edi, [__shell.buf__] |
||
158 | mov [edi], byte 2 |
||
159 | mov [edi + 1], al |
||
160 | call shell.wait |
||
161 | |||
162 | popa |
||
163 | ret |
||
164 | |||
165 | ;----------------------------------- |
||
166 | ; Print string to shell |
||
167 | ; |
||
168 | ; INPUT: |
||
169 | ; eax - string |
||
170 | ;----------------------------------- |
||
171 | |||
172 | shell.print_string: |
||
173 | pusha |
||
174 | |||
175 | mov edi, [__shell.buf__] |
||
176 | mov [edi], byte 3 |
||
177 | inc edi |
||
178 | |||
179 | @@: |
||
180 | mov bl, [eax] |
||
181 | mov [edi], bl |
||
182 | inc edi |
||
183 | inc eax |
||
184 | cmpne [eax], byte 0, @b |
||
185 | |||
186 | mov [edi], byte 0 |
||
187 | mov edi, [__shell.buf__] |
||
188 | call shell.wait |
||
189 | |||
190 | popa |
||
191 | ret |
||
192 | |||
193 | ;----------------------------------- |
||
194 | ; Print number to shell |
||
195 | ; |
||
196 | ; INPUT: |
||
197 | ; eax - number |
||
198 | ;----------------------------------- |
||
199 | |||
200 | shell.print_num: |
||
201 | pusha |
||
202 | |||
203 | mov ecx, eax |
||
204 | and ecx, 1 shl 31 |
||
205 | cmpe ecx, 0, @f |
||
206 | mov [__shell.str__], "-" |
||
207 | not eax |
||
208 | inc eax |
||
209 | |||
210 | @@: |
||
211 | mov ebx, 10 |
||
212 | clear ecx |
||
213 | @@: |
||
214 | clear edx |
||
215 | div ebx |
||
216 | push edx |
||
217 | inc ecx |
||
218 | cmpne eax, 0, @b |
||
219 | |||
220 | mov ebx, __shell.str__ |
||
221 | inc ebx |
||
222 | |||
223 | @@: |
||
224 | pop eax |
||
225 | add al, "0" |
||
226 | mov [ebx], al |
||
227 | inc ebx |
||
228 | loop @b |
||
229 | |||
230 | mov [ebx], byte 0 |
||
231 | mov eax, __shell.str__ |
||
232 | cmpe [eax], byte "-", @f |
||
233 | inc eax |
||
234 | @@: |
||
235 | call shell.print_string |
||
236 | |||
237 | popa |
||
238 | ret |
||
239 | |||
240 | ;----------------------------------- |
||
241 | ; Get char from shell |
||
242 | ; |
||
243 | ; OUTPUT: |
||
244 | ; al - char |
||
245 | ;----------------------------------- |
||
246 | |||
247 | shell.get_char: |
||
248 | push ebx ecx edx esi edi |
||
249 | |||
250 | mov edi, [__shell.buf__] |
||
251 | mov [edi], byte 4 |
||
252 | call shell.wait |
||
253 | mov al, [edi + 1] |
||
254 | |||
255 | pop edi esi edx ecx ebx |
||
256 | ret |
||
257 | |||
258 | ;----------------------------------- |
||
259 | ; Get string from shell |
||
260 | ; |
||
261 | ; INPUT: |
||
262 | ; eax - addres of memory for str |
||
263 | ;----------------------------------- |
||
264 | |||
265 | shell.get_string: |
||
266 | pusha |
||
267 | |||
268 | mov edi, [__shell.buf__] |
||
269 | mov [edi], byte 5 |
||
270 | call shell.wait |
||
271 | |||
272 | inc edi |
||
273 | @@: |
||
274 | mov bl, [edi] |
||
275 | mov [eax], bl |
||
276 | inc eax |
||
277 | inc edi |
||
278 | cmpne [edi], byte 0, @b |
||
279 | |||
280 | mov [eax], byte 0 |
||
281 | |||
282 | popa |
||
283 | ret |
||
284 | |||
285 | ;----------------------------------- |
||
286 | ; Get number from shell |
||
287 | ; |
||
288 | ; OUTPUT: |
||
289 | ; eax - number |
||
290 | ;----------------------------------- |
||
291 | |||
292 | shell.get_num: |
||
293 | push ebx ecx edx esi edi |
||
294 | |||
295 | mov eax, __shell.str__ |
||
296 | call shell.get_string |
||
297 | |||
298 | mov ebx, eax |
||
299 | clear eax |
||
300 | clear edi |
||
301 | cmpne [ebx], byte "-", @f |
||
302 | mov edi, 1 |
||
303 | inc ebx |
||
304 | |||
305 | @@: |
||
306 | sub [ebx], byte "0" |
||
307 | imul eax, 10 |
||
308 | add al, [ebx] |
||
309 | inc ebx |
||
310 | cmpne [ebx], byte 10, @b |
||
311 | |||
312 | cmpe edi, 0, @f |
||
313 | not eax |
||
314 | inc eax |
||
315 | |||
316 | @@: |
||
317 | pop edi esi edx ecx ebx |
||
318 | ret |
||
319 | |||
320 | |||
321 | ;----------------------------------- |
||
322 | ; Clear shell |
||
323 | ; without arguments |
||
324 | ;----------------------------------- |
||
325 | |||
326 | shell.clear: |
||
327 | pusha |
||
328 | |||
329 | mov edi, [__shell.buf__] |
||
330 | mov [edi], byte 6 |
||
331 | call shell.wait |
||
332 | |||
333 | popa |
||
334 | ret |
||
335 | |||
336 | ;----------------------------------- |
||
337 | ; Print char to shell |
||
338 | ; |
||
339 | ; INPUT: |
||
340 | ; arg1 - char |
||
341 | ;----------------------------------- |
||
342 | |||
343 | macro shpc [char] { |
||
344 | push eax |
||
345 | mov al, char |
||
346 | call shell.print_char |
||
347 | pop eax |
||
348 | } |
||
349 | |||
350 | ;----------------------------------- |
||
351 | ; Go to new line in shell |
||
352 | ; without arguments |
||
353 | ;----------------------------------- |
||
354 | |||
355 | macro shln { |
||
356 | shpc 10 |
||
357 | shpc 13 |
||
358 | } |
||
359 | |||
360 | ;----------------------------------- |
||
361 | ; Print string to shell |
||
362 | ; |
||
363 | ; INPUT: |
||
364 | ; arg1 - string |
||
365 | ;----------------------------------- |
||
366 | |||
367 | macro shps [string] { |
||
368 | local ..string, ..label |
||
369 | jmp ..label |
||
370 | ..string db string, 0 |
||
371 | ..label: |
||
372 | |||
373 | push eax |
||
374 | mov eax, ..string |
||
375 | call shell.print_string |
||
376 | pop eax |
||
377 | } |
||
378 | |||
379 | ;----------------------------------- |
||
380 | ; Print string to shell |
||
381 | ; |
||
382 | ; INPUT: |
||
383 | ; arg1 - addres of string |
||
384 | ;----------------------------------- |
||
385 | |||
386 | macro shpsa [addr] { |
||
387 | push eax |
||
388 | mov eax, addr |
||
389 | call shell.print_string |
||
390 | pop eax |
||
391 | } |
||
392 | |||
393 | ;----------------------------------- |
||
394 | ; Print number to shell |
||
395 | ; |
||
396 | ; INPUT: |
||
397 | ; arg1 - number |
||
398 | ;----------------------------------- |
||
399 | |||
400 | macro shpn [num] { |
||
401 | push eax |
||
402 | mov eax, num |
||
403 | call shell.print_num |
||
404 | pop eax |
||
405 | } |
||
406 | |||
407 | |||
408 | ;----------------------------------- |
||
409 | ; Get char from shell |
||
410 | ; |
||
411 | ; OUTPUT: |
||
412 | ; al - char |
||
413 | ;----------------------------------- |
||
414 | |||
415 | macro shgc { |
||
416 | call shell.get_char |
||
417 | } |
||
418 | |||
419 | ;----------------------------------- |
||
420 | ; Get string from shell |
||
421 | ; |
||
422 | ; INPUT: |
||
423 | ; arg1 - addres of memory for str |
||
424 | ;----------------------------------- |
||
425 | |||
426 | macro shgs [addr] { |
||
427 | mov eax, addr |
||
428 | call shell.get_string |
||
429 | } |
||
430 | |||
431 | ;----------------------------------- |
||
432 | ; Get number from shell |
||
433 | ; |
||
434 | ; INPUT: |
||
435 | ; arg1 - addres of memory for num |
||
436 | ;----------------------------------- |
||
437 | |||
438 | macro shgn [addr] { |
||
439 | push eax |
||
440 | call shell.get_num |
||
441 | mov [addr], eax |
||
442 | pop eax |
||
443 | } |
||
444 | |||
445 | |||
446 | ;----------------------------------- |
||
447 | ; Wait any char from shell |
||
448 | ; without arguments |
||
449 | ;----------------------------------- |
||
450 | |||
451 | macro shw { |
||
452 | push eax |
||
453 | call shell.get_char |
||
454 | pop eax |
||
455 | } |
||
456 | |||
457 | ;----------------------------------- |
||
458 | ; Clear shell |
||
459 | ; without arguments |
||
460 | ;----------------------------------- |
||
461 | |||
462 | macro shc { |
||
463 | call shell.clear |
||
464 | } |
||
465 | |||
466 | ;----------------------------------- |
||
467 | ; Choice |
||
468 | ; |
||
469 | ; INPUT: |
||
470 | ; arg1 - question |
||
471 | ; arg2 - asnwers |
||
472 | ; |
||
473 | ; OUTPUT: |
||
474 | ; al - answer |
||
475 | ;----------------------------------- |
||
476 | |||
477 | macro shchs str, chs { |
||
478 | local ..loop1, ..loop2, ..chs, ..start, ..res |
||
479 | jmp ..start |
||
480 | ..chs db chs, 0 |
||
481 | ..start: |
||
482 | push ebx |
||
483 | |||
484 | shps str, " [" |
||
485 | shpsa ..chs |
||
486 | shps "]: " |
||
487 | |||
488 | ..loop1: |
||
489 | call shell.get_char |
||
490 | |||
491 | mov ebx, ..chs |
||
492 | ..loop2: |
||
493 | cmpe al, [ebx], ..res |
||
494 | inc ebx |
||
495 | cmpe [ebx], byte 0, ..loop1 |
||
496 | jmp ..loop2 |
||
497 | |||
498 | ..res: |
||
499 | mov al, [ebx] |
||
500 | call shell.print_char |
||
501 | shln |
||
502 | |||
503 | mov eax, ebx |
||
504 | sub eax, ..chs |
||
505 | |||
506 | pop ebx |
||
507 | } |