News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

argc andd argv ??????/

Started by pwnpwn, August 28, 2010, 04:58:05 PM

Previous topic - Next topic

pwnpwn

hi ppl how do i get argc and argv[0] and argv[1] in assmebly??/

bomz

invoke GetCommandLine
other handly

bomz


Quote
.386

.model flat, stdcall
option casemap :none

include \MASM32\INCLUDE\windows.inc
include \MASM32\INCLUDE\user32.inc
include \MASM32\INCLUDE\kernel32.inc
includelib \MASM32\LIB\user32.lib
includelib \MASM32\LIB\kernel32.lib

.data

.code
start:
invoke GetCommandLine
invoke MessageBox, NULL, EAX, NULL, MB_OK
invoke ExitProcess,0
end start

pwnpwn

thx that works perfectly
but is there a way to get argc and each argv separely/?
i saw somewhere the code

push ebp
mov ebp, esp
mov eax, [ebp+12]

eax shoud then have argv[0] if i rember correctly but t doesnt work for me

so basicly when tha program starts, on [esp+8] shoud be argv[0] but it does't work for me

bomz

this is assembler. programmer must all do by himself or using basic


bomz

#5
del

pwnpwn


bomz

if your dare, take in opinion all variants

.\MYPROG.EXE
C:\MYPROG.EXE
"C:\Programe Files\MYPROG.EXE"
MYPROG.EXE
"MY PROG.EXE"

bomz

#8
try it
Quote
right code see below

this is I think argv[]
to get argc - invoke lstrlen, EAX
it's would be better to use ebx, because invoke destroy eax
to get arg[1].... do by yourself and I hope you may stay assembler programmer

dedndave

....even better to use EAX, ECX, or EDX   :P
EBX, EBP, ESI, and EDI should be preserved in win32 functions
also, DF should be left cleared (mentioned for completeness)

bomz

in russian translation of ICZELION there is two opinion about destroy registers, but practic I never see destroy ebx.

try it

jj2007

Test it.
include \masm32\include\masm32rt.inc

.data?
buffer db MAX_PATH dup(?)

.code
start: xor ebx, ebx
.Repeat
inc ebx
invoke GetCL, ebx, offset buffer
.Break .if eax!=1
print "arg #"
print str$(ebx), " = ", 9
print offset buffer, 13, 10
.Until 0
print "BYE"
exit

end start

BogdanOntanu

The concept of argc and argv are C run time specific. At OS low level they do not exist. They exist in C because the C runtime does execute some pre-startup code and prepares this for you before execution reaches the main() function. You can eventually have them in ASM but in that case you must link and use the C runtime library.

However in ASM it is kind of a custom to write your own by using the GetCommandLine API and parsing the command line string as shown above ;)
Ambition is a lame excuse for the ones not brave enough to be lazy.
http://www.oby.ro

jj2007

Bogdan,
Roll your own maximises the fun but the Masm32 library is a good second choice :wink

QuoteGetCL proc ArgNum:DWORD, ItemBuffer:DWORD

Description
GetCl will retreive the argument specified in the procedure call and return the argument in the buffer specified. It differs slightly in its implementation in that it treats the path and name of the application making the call as arg 0.

Arguments are specified from arg 1 up to the number of command line arguments available. The procedure supports the retreival of arguments that are enclosed in quotation marks and will return the argument with the quotation marks removed.

Parameters
1. ArgNum Argument number.
2. ItemBuffer Address of buffer to receive the argument if it exists.

Return values
1 = successful operation
2 = no argument exists at specified arg number
3 = non matching quotation marks
4 = empty quotation marks

bomz

#14
RIGHT CODE

Quote
.386

.model flat, stdcall
option casemap :none

include \MASM32\INCLUDE\windows.inc
include \MASM32\INCLUDE\user32.inc
include \MASM32\INCLUDE\kernel32.inc
includelib \MASM32\LIB\user32.lib
includelib \MASM32\LIB\kernel32.lib

.data
No_Param db "No Parameter",0

.code
start:

invoke GetCommandLine
mov bl, ' '
mov ecx, 1
cmp byte ptr[eax],'"'
jne next
inc eax
mov bl, '"'
mov ecx, 2

next:
cmp byte ptr[eax],bl
je found
cmp byte ptr[eax],0
jz found1
inc eax
jmp short next
found1:
inc eax

found:
add eax, ecx

cmp byte ptr [eax], ' '
jne No_Shell
inc eax
No_Shell:

cmp byte ptr [eax], 0
jne Slash
invoke MessageBox,0,ADDR No_Param,NULL, MB_OK + MB_ICONASTERISK
invoke ExitProcess,1

Slash:
invoke MessageBox,0,EAX,NULL, MB_OK + MB_ICONASTERISK
invoke ExitProcess,0
end start
name of file can't begin with space

previous code don't work correctly when program starts with batch file without extension and parametre