Skip to content

Commit 1f8b4d4

Browse files
committed
Update Argument Parsing Functions
Add Ansi and Wide/Unicode versions of the argument parsing functions: Arg_GetCommandLineExW, Arg_GetCommandLineExA, Arg_GetCommandLineW, Arg_GetCommandLineA, Arg_GetArgumentW, Arg_GetArgumentA
1 parent 17d694b commit 1f8b4d4

27 files changed

+851
-119
lines changed

Arg_GetArgument.asm renamed to Arg_GetArgumentA.asm

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ option casemap : none
1414
IF @Platform EQ 1
1515
option win64 : 11
1616
ENDIF
17-
1817
option frame : auto
19-
18+
2019
include UASM64.inc
20+
21+
2122

2223
.DATA
2324

@@ -52,7 +53,7 @@ abntbl \
5253

5354
UASM64_ALIGN
5455
;------------------------------------------------------------------------------
55-
; Arg_GetArgument
56+
; Arg_GetArgumentA
5657
;
5758
; Return the contents of an argument from an argument list by its number.
5859
;
@@ -95,12 +96,14 @@ UASM64_ALIGN
9596
; the parser in this algorithm return an empty destination buffer that has an
9697
; ascii zero as it first character.
9798
;
99+
; This function as based on the MASM32 Library function: ArgByNumber
100+
;
98101
; See Also:
99102
;
100-
; Arg_GetCommandLine, Arg_GetCommandLineEx
103+
; Arg_GetCommandLineA, Arg_GetCommandLineExA
101104
;
102105
;------------------------------------------------------------------------------
103-
Arg_GetArgument PROC FRAME USES RBX RCX RDX RDI RSI lpszArgumentList:QWORD,lpszDestination:QWORD,nArgument:QWORD,qwArgumentListReadOffset:QWORD
106+
Arg_GetArgumentA PROC FRAME USES RBX RCX RDX RDI RSI lpszArgumentList:QWORD,lpszDestination:QWORD,nArgument:QWORD,qwArgumentListReadOffset:QWORD
104107

105108
; -----------------------------------------------
106109

@@ -233,7 +236,7 @@ Arg_GetArgument PROC FRAME USES RBX RCX RDX RDI RSI lpszArgumentList:QWORD,lpszD
233236
rstack:
234237

235238
ret
236-
Arg_GetArgument ENDP
239+
Arg_GetArgumentA ENDP
237240

238241

239242
END

Arg_GetArgumentW.asm

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
;==============================================================================
2+
;
3+
; UASM64 Library
4+
;
5+
; https://github.com/mrfearless/UASM64-Library
6+
;
7+
;==============================================================================
8+
.686
9+
.MMX
10+
.XMM
11+
.x64
12+
13+
option casemap : none
14+
IF @Platform EQ 1
15+
option win64 : 11
16+
ENDIF
17+
option frame : auto
18+
19+
include UASM64.inc
20+
21+
.CODE
22+
23+
UASM64_ALIGN
24+
;------------------------------------------------------------------------------
25+
; Arg_GetArgumentW
26+
;
27+
; Return the contents of an argument from an argument list by its number.
28+
;
29+
; Parameters:
30+
;
31+
; * lpszArgumentList - The address of the zero terminated argument list.
32+
;
33+
; * lpszDestination - The address of the destination buffer.
34+
;
35+
; * nArgument - The number of the argument to return.
36+
;
37+
; * qwNotUsed - Not used, for compatibility with Masm32 ArgByNumber and
38+
; Arg_GetArgumentA, using same amount of parameters for each.
39+
;
40+
; Returns:
41+
;
42+
; The return value is the updated next read offset in the source if it is
43+
; greater than zero.
44+
;
45+
; The three possible return values are:
46+
; > 1 = The next read offset in the source.
47+
; 0 = The end of the argument list has been reached.
48+
; -1 = A non matching quotation error has occurred in the source.
49+
;
50+
; Notes:
51+
;
52+
; This function supports double quoted text and it is delimited by the space
53+
; character, a tab or a comma or any combination of these three. It may be used
54+
; in two separate modes, single argument mode and streaming mode.
55+
;
56+
; In separate argument mode you specify the argument you wish to obtain with
57+
; the nArgument parameter and you set the qwArgumentListReadOffset parameter to
58+
; zero.
59+
;
60+
; In streaming mode you set a variable to zero and pass it as the 4th parameter
61+
; qwArgumentListReadOffset and save the RAX return value back into this variable
62+
; for the next call to the function. The nArgument parameter in streaming mode
63+
; should be set to one "1"
64+
;
65+
; To support the notation of an empty pair of double quotes in an argument list
66+
; the parser in this algorithm return an empty destination buffer that has an
67+
; ascii zero as it first character.
68+
;
69+
; This function as based on the MASM32 Library function: ucArgByNum
70+
;
71+
; See Also:
72+
;
73+
; Arg_GetCommandLineW, Arg_GetCommandLineExW
74+
;
75+
;------------------------------------------------------------------------------
76+
Arg_GetArgumentW PROC FRAME USES RBX RCX RDX RDI RSI lpszArgumentList:QWORD, lpszDestination:QWORD, nArgument:QWORD, qwNotUsed:QWORD
77+
78+
mov rsi, lpszArgumentList
79+
mov rcx, 1
80+
xor rax, rax
81+
82+
mov rdx, lpszDestination
83+
mov WORD PTR [rdx], 0
84+
85+
; ------------------------------------
86+
; handle src as pointer to NULL string
87+
; ------------------------------------
88+
cmp WORD PTR [rsi], 0
89+
jne next1
90+
jmp bailout
91+
next1:
92+
sub rsi, 2
93+
94+
ftrim:
95+
add rsi, 2
96+
mov ax, WORD PTR [rsi]
97+
cmp ax, 32
98+
je ftrim
99+
cmp ax, 9
100+
je ftrim
101+
cmp ax, 0
102+
je bailout ; exit on empty string (only white space)
103+
104+
cmp WORD PTR [rsi], 34
105+
je quoted
106+
107+
sub rsi, 2
108+
109+
; ----------------------------
110+
111+
unquoted:
112+
add rsi, 2
113+
mov ax, WORD PTR [rsi]
114+
test ax, ax
115+
jz scanout
116+
cmp ax, 32
117+
je wordend
118+
mov WORD PTR [rdx], ax
119+
add rdx, 2
120+
jmp unquoted
121+
122+
wordend:
123+
cmp rcx, nArgument
124+
je scanout
125+
add rcx, 1
126+
mov rdx, lpszDestination
127+
mov WORD PTR [rdx], 0
128+
jmp ftrim
129+
130+
; ----------------------------
131+
132+
quoted:
133+
add rsi, 2
134+
mov ax, WORD PTR [rsi]
135+
test ax, ax
136+
jz scanout
137+
cmp ax, 34
138+
je quoteend
139+
mov WORD PTR [rdx], ax
140+
add rdx, 2
141+
jmp quoted
142+
143+
quoteend:
144+
add rdi, 2
145+
cmp rcx, nArgument
146+
je scanout
147+
add rcx, 1
148+
mov rdx, lpszDestination
149+
mov WORD PTR [rdx], 0
150+
jmp ftrim
151+
152+
; ----------------------------
153+
154+
scanout:
155+
.if nArgument > rcx
156+
bailout: ; error exit
157+
mov rdx, lpszDestination ; reload dest address
158+
mov WORD PTR [rdx], 0 ; zero dest buffer
159+
xor rax, rax ; zero return value
160+
jmp quit
161+
.else ; normal exit
162+
mov WORD PTR [rdx], 0 ; terminate output buffer
163+
mov rax, rcx ; set the return value
164+
.endif
165+
166+
quit:
167+
168+
ret
169+
Arg_GetArgumentW ENDP
170+
171+
172+
END
173+

Arg_GetCommandLine.asm renamed to Arg_GetCommandLineA.asm

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ include UASM64.inc
3131

3232
UASM64_ALIGN
3333
;------------------------------------------------------------------------------
34-
; Arg_GetCommandLine
34+
; Arg_GetCommandLineA
3535
;
3636
; Retrieve the argument specified by the nArgument parameter and returns it in
3737
; the buffer specified by the lpszArgumentBuffer parameter.
@@ -62,12 +62,14 @@ UASM64_ALIGN
6262
; The buffer for the returned argument should be set at 128 bytes in length
6363
; which is the maximum allowable
6464
;
65+
; This function as based on the MASM32 Library function: getcl
66+
;
6567
; See Also:
6668
;
67-
; Arg_GetCommandLineEx, Arg_GetArgument
69+
; Arg_GetCommandLineExA, Arg_GetArgumentA
6870
;
6971
;------------------------------------------------------------------------------
70-
Arg_GetCommandLine PROC FRAME USES RCX RDI RSI nArgument:QWORD, lpszArgumentBuffer:QWORD
72+
Arg_GetCommandLineA PROC FRAME USES RCX RDI RSI nArgument:QWORD, lpszArgumentBuffer:QWORD
7173
; -------------------------------------------------
7274
; arguments returned in "lpszArgumentBuffer"
7375
;
@@ -284,7 +286,7 @@ Arg_GetCommandLine PROC FRAME USES RCX RDI RSI nArgument:QWORD, lpszArgumentBuff
284286

285287
mov rax, 1 ; return value success
286288
ret
287-
Arg_GetCommandLine ENDP
289+
Arg_GetCommandLineA ENDP
288290

289291

290292
END

Arg_GetCommandLineEx.asm renamed to Arg_GetCommandLineExA.asm

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ include UASM64.inc
3232
IF @Platform EQ 1 ; Win x64
3333
UASM64_ALIGN
3434
;------------------------------------------------------------------------------
35-
; Arg_GetCommandLineEx
35+
; Arg_GetCommandLineExA
3636
;
3737
; Extended version of Arg_GetCommandLine. This Arg_GetCommandLineEx function
3838
; uses the Arg_GetArgument function to obtain the selected argument from a
@@ -55,13 +55,17 @@ UASM64_ALIGN
5555
; 1 = successful operation
5656
; 2 = no argument exists at specified arg number
5757
; 3 = non matching quotation marks
58-
;
58+
;
59+
; Notes:
60+
;
61+
; This function as based on the MASM32 Library function: getcl_ex
62+
;
5963
; See Also:
6064
;
61-
; Arg_GetCommandLine, Arg_GetArgument
65+
; Arg_GetCommandLineA, Arg_GetArgumentA
6266
;
6367
;------------------------------------------------------------------------------
64-
Arg_GetCommandLineEx PROC FRAME USES RCX nArgument:QWORD, lpszArgumentBuffer:QWORD
68+
Arg_GetCommandLineExA PROC FRAME USES RCX nArgument:QWORD, lpszArgumentBuffer:QWORD
6569
LOCAL lpszArgsList:QWORD
6670

6771
; 1 = successful operation
@@ -72,7 +76,7 @@ Arg_GetCommandLineEx PROC FRAME USES RCX nArgument:QWORD, lpszArgumentBuffer:QWO
7276
7377
Invoke GetCommandLineA
7478
mov lpszArgsList, rax
75-
Invoke Arg_GetArgument, lpszArgsList, lpszArgumentBuffer, nArgument, 0
79+
Invoke Arg_GetArgumentA, lpszArgsList, lpszArgumentBuffer, nArgument, 0
7680

7781
.if rax >= 0
7882
mov rcx, lpszArgumentBuffer
@@ -87,13 +91,13 @@ Arg_GetCommandLineEx PROC FRAME USES RCX nArgument:QWORD, lpszArgumentBuffer:QWO
8791

8892
ret
8993

90-
Arg_GetCommandLineEx ENDP
94+
Arg_GetCommandLineExA ENDP
9195
ENDIF
9296

9397
IF @Platform EQ 3 ; Linux x64
9498
UASM64_ALIGN
9599
;------------------------------------------------------------------------------
96-
; Arg_GetCommandLineEx
100+
; Arg_GetCommandLineExA
97101
;
98102
; Extended version of Arg_GetCommandLine. This Arg_GetCommandLineEx function
99103
; uses the Arg_GetArgument function to obtain the selected argument from a
@@ -119,14 +123,14 @@ UASM64_ALIGN
119123
;
120124
; See Also:
121125
;
122-
; Arg_GetCommandLine, Arg_GetArgument
126+
; Arg_GetCommandLineA, Arg_GetArgumentA
123127
;
124128
;------------------------------------------------------------------------------
125-
Arg_GetCommandLineEx PROC FRAME nArgument:QWORD, lpszArgumentBuffer:QWORD
129+
Arg_GetCommandLineExA PROC FRAME nArgument:QWORD, lpszArgumentBuffer:QWORD
126130
mov rax, 0 ; maybe we can get command line later with
127131
; ; https://baturin.org/blog/hello-from-a-compiler-free-x86-64-world/
128132
ret
129-
Arg_GetCommandLineEx ENDP
133+
Arg_GetCommandLineExA ENDP
130134
ENDIF
131135

132136
END

0 commit comments

Comments
 (0)