Skip to content

Commit c02dc45

Browse files
committed
reorder
1 parent bf8a6f4 commit c02dc45

1 file changed

Lines changed: 39 additions & 48 deletions

File tree

32shellEmulator/shell32.s

Lines changed: 39 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,86 +7,77 @@
77

88
global main
99

10-
main:
10+
main:
11+
12+
do_fork:
13+
14+
SYSTEM_CALL(fork)
15+
test eax,eax
16+
jz short child
17+
parent:
18+
push byte 0
19+
push byte 0
20+
push byte 0
21+
pop ebx
22+
pop ecx
23+
pop edx
24+
SYSTEM_CALL(waitpid)
25+
jmp short do_fork
26+
child:
1127
cld
28+
1229
get_input:
1330
xor eax,eax
1431
cdq
1532
mov dx,BUFFERLEN
1633
sub esp,edx
1734
mov ecx,esp
1835
xor ebx,ebx
19-
push byte read
20-
pop eax
21-
SYSTEM_CALL
36+
SYSTEM_CALL(read)
2237
mov ebp,eax
2338
test eax,eax
24-
jz do_exit ;synchronous IO or GTFO
39+
jz short do_exit ;synchronous IO or GTFO
2540
mov byte [eax+esp-1],0
26-
push eax
27-
28-
29-
do_fork:
30-
31-
push byte fork
32-
pop eax
33-
SYSTEM_CALL
34-
test eax,eax
35-
jz child
36-
parent:
37-
push waitpid
38-
pop eax
39-
xor ebx,ebx
40-
mov ecx,ebx
41-
mov edx,ebx
42-
SYSTEM_CALL
43-
jmp get_input
41+
42+
43+
;; push eax ;return of read pushed by get_input
44+
;; pop ecx
4445
45-
child:
46-
;; let's parse the arguments here
47-
pop ecx ;return of read pushed by get_input
46+
;let's parse the arguments here
47+
xchg eax,ecx
4848
push byte " "
4949
pop eax ;space used for inlined strchr
5050
mov ebx,esp
51-
xor edx,edx
52-
add esp,BUFFERLEN
51+
cdq ;msb of eax is zero so this is ok
52+
53+
add esp,BUFFERLEN ;space for argv[]
5354
add_token: ;; calculate the pointerp to push
5455
5556
mov esi,ebp
5657
sub esi,ecx
5758
5859
lea edi,[ebx + esi] ;register subtraction no good in lea
5960
mov [esp+edx*4], edi ;save the current token
60-
inc edx
61+
inc edx ;increment argv[] index
6162
62-
63-
repne scasb
63+
repne scasb ;find the next space
6464
6565
mov esi,ebp
6666
sub esi,ecx
67-
mov byte[ebx+esi-1],0
68-
69-
test ecx,ecx
70-
jz exec
67+
mov byte[ebx+esi-1],0 ;replace the space with null byte (strtok)
7168
72-
jmp short add_token
69+
test ecx,ecx ;if ECX is zero we've hit the end of the input str
70+
jz short exec ;set up for execve systemcall (with argv =D)
71+
jmp short add_token ;if not, strtok
7372
7473
exec:
75-
76-
77-
xor eax,eax
74+
xchg ecx,eax ;eax=0
7875
mov [esp+edx*4],eax
7976
cdq
80-
push eax
81-
mov al,11
82-
lea ecx,[esp+4]
83-
84-
xor edx,edx
85-
77+
mov al,execve
78+
lea ecx,[esp]
8679
SYSTEM_CALL
8780
8881
do_exit:;; exit nicely if anything fails
89-
push byte exit
90-
pop eax
9182
xor ebx,ebx ;optional
92-
SYSTEM_CALL
83+
SYSTEM_CALL(exit)

0 commit comments

Comments
 (0)