-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGETARG.ASM
124 lines (122 loc) · 2.84 KB
/
GETARG.ASM
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
; -----------------------------------------------------------
; Small C Compiler for TRDOS 386 (v2.0.9 and later)
; Erdogan Tan - 2024
; Beginning: 05/09/2024
; Last Update: 14/09/2024
; -----------------------------------------------------------
; Derived from 'getarg.asm' file of KolibriOS SCC source code
; 2024
; Get command line argument.
; Entry: n = Number of the argument.
; s = Destination string pointer.
; size = Size of destination string.
; argc = Argument count from main().
; argv = Argument vector(s) from main().
; Returns number of characters moved on success,
; else EOF.
_getarg: ; int n; char *s; int size,argc,argv[];
PUSH EBP
MOV EBP,ESP
ADD ESP,-8 ; char *str; int i;
LEA EAX,[EBP+24] ; n
MOV EBX,[EAX] ; if(n<0 | n>=argc)
XOR EAX,EAX ; 0
CALL __lt ; if(n<0
PUSH EAX ; 0 (false) or 1 (true)
;LEA EBX,[EBP+24]
;MOV EAX,[EBX]
;PUSH EAX
PUSH EBX ; n
LEA EBX,[EBP+12] ; argc
MOV EAX,[EBX]
POP EBX
CALL __ge ; | n>=argc)
POP EBX
OR EAX,EBX ; (condition 1 or condition 2)
;OR EAX,EAX
;JNZ short _0_3
JZ short _0_2 ; }
_0_3: ; {*s=NULL;
LEA EAX,[EBP+20] ; pointer to *s address
MOV EBX,[EAX] ; (ebx = address of *s)
XOR EAX,EAX ; NULL
MOV [EBX],AL ; *s=NULL; ; ([ebx] = s value)
MOV EAX,-1 ; return EOF;
MOV ESP,EBP
POP EBP
RETN
_0_2: ; }
LEA EBX,[EBP-8]
;XOR EAX,EAX
;eax = 0
MOV [EBX],EAX ; i=0;
LEA EAX,[EBP-4]
PUSH EAX ; str
LEA EBX,[EBP+8] ; pointer to argv[]
MOV EAX,[EBX]
PUSH EAX ; argv[] (address)
LEA EBX,[EBP+24] ; n
MOV EAX,[EBX]
POP EBX
SHL EAX,1
SHL EAX,1 ; n*4
ADD EBX,EAX ; argv[0]+4*n
MOV EAX,[EBX] ; argv[n]
POP EBX ; str (address)
MOV [EBX],EAX ; str = argv[n]
_0_4: ; while(i<size)
LEA EBX,[EBP-8]
MOV EAX,[EBX]
PUSH EAX ; i
LEA EBX,[EBP+16] ; pointer to size
MOV EAX,[EBX]
POP EBX
CALL __lt ; is i<size ?
OR EAX,EAX
JZ short _0_5 ; false, jump to s[i]=NULL;
_0_6: ; true, if((s[i]=str[i])==NULL) break;
LEA EBX,[EBP+20]
MOV EAX,[EBX]
PUSH EAX ; s (address)
LEA EBX,[EBP-8]
MOV EAX,[EBX] ; i (value)
POP EBX
ADD EAX,EBX ; (i offset from start of s)
PUSH EAX ; s[i]
LEA EBX,[EBP-4] ; pointer to of *str address
MOV EAX,[EBX] ; str
PUSH EAX
LEA EBX,[EBP-8]
MOV EAX,[EBX]
POP EBX ; str (address)
ADD EBX,EAX ; (i offset from start of str)
MOVSX EAX,BYTE [EBX] ; char str[i]
POP EBX ; s[i]
MOV [EBX],AL ; s[i] = str[i]
OR EAX,EAX ; if((s[i]=str[i])==NULL
JZ short _0_5 ; break;
_0_7: ; else, ++i;
LEA EBX,[EBP-8]
;MOV EAX,[EBX]
;INC EAX
;MOV [EBX],EAX
INC DWORD [EBX] ; ++i
JMP short _0_4 ; jump to while(i<size)
_0_5:
LEA EBX,[EBP+20]
MOV EAX,[EBX]
PUSH EAX ; s
LEA EBX,[EBP-8]
MOV EAX,[EBX] ; i
POP EBX
ADD EBX,EAX ; start of s + i = s[i]
;PUSH EAX ; i
;XOR EAX,EAX ; NULL
;MOV [EBX],AL ; s[i]=NULL;
MOV BYTE [EBX],0
;;LEA EBX,[EBP-8]
;;MOV EAX,[EBX]
;POP EAX ; return i;
MOV ESP,EBP
POP EBP
RETN