I've been torturing myself with this issue for a month and I'm about to give up. It is a simple program and I just can't see why is not working properly.
So, what I am trying to do create is a simple application that will parse the command line and display the first argument, which is the full path to the executable.
This is how my code looks like:
use32
[EXTERN GetStdHandle]
[EXTERN GetCommandLineW]
[EXTERN WriteConsoleW]
[EXTERN ExitProcess]
[section .bss]
StdHandle resd 1
PathStart resw 1
PathEnd resw 1
WrittenChars resw 1
[section .data]
message db __utf16__(""Hello everybody"), 13, 10, 0
[section .text]
global start
start:
call GetHandler
call GetCommandLine
end:
mov eax, e
ret
GetHandler:
push -11
call GetStdHandle
cmp eax, 1
push ebx
mov ebx, 1
jl CloseApp
pop ebx
mov dword[StdHandle], eax
ret
GetCommandLine:
cld
call GetCommandLineW ; UNICODE
mov esi, eax
mov bh, 0 ; here we save the argc
mov ecx, eax ; here we save the pointer of the first arg
Parse:
lodsw
cmp ax, __utf16__(' ')
je NewArg
jmp ContinueParsing
NewArg:
inc bh
cmp bh, 1
jne Parse
; if the first arg was just read save the start from ecx and end from esi to the BSS variables
mov dword[PathStart], ecx
mov dword[PathEnd], esi
jmp ShowPath
ContinueParsing:
cmp ax, 0
jne Parse
ShowPath:
mov ecx, [PathEnd]
mov ebx, [PathStart]
sub ecx, ebx ; text size
shr ecx, 1 ; is UNICODE
push dword[PathStart]
push dword[ecx]
call ShowText
ret
ShowText:
push ebp
mov ebp, esp
push 0
push dword WrittenChars
push dword [ebp+8]
push dword [ebp+12]
push dword [StdHandle]
call WriteConsoleW
pop
ret 8
CloseApp:
push ebx
call ExitProcess
pop ebx
ret
Well, I might have misspelled or missed something but that is not the problem. The code is compiled and built successfully but the message that I see is only the name of the executable, not the full path that I expect. If the full path is "D:\My Projects\NASM\Demo.exe" I only see "Demo". If before calling ShowText I prepare the arguments for the message variable it works and I can see the text correctly, so I think the problem lies in getting the pointer and length of the full path correctly. Yet, when studying the running of the application with OllyDbg I can see that correct values are stored in the BSS section. This is very weird and maybe somebody with a better eye can catch the cause of it. Thanks in advance