flat assembler
Message board for the users of flat assembler.
Index
> DOS > Get filename of itself / current running file AKA argv[0] |
Author |
|
SonOfEarth 26 Feb 2009, 09:29
Hello,
I've tried to get the filename of the current running file. (I want to change a specific byte in the current file. for example to get the number of execution of the program; or to save data without external files/dbs.) I've tried to get the filename via GetCommandLine-synonym. Therefor I've read the threat here (http://board.flatassembler.net/topic.php?t=838). But unfortunatly that example codes just give the arguments argv[1...n], but not argv[0]; even in C and Win32 API GetCommandLine it returns the filename as 0th argument. Can anybody tell me how to find the filename of the current running file? thank you! EDIT by DOS386 : enhanced subject |
|||
26 Feb 2009, 09:29 |
|
SonOfEarth 26 Feb 2009, 15:45
Tomasz Grysztar wrote: You can find it in the program's environment segment. After all the environment variables, there comes a 16-bit value determning number of additional strings that follow, and the first of those string is the full pathname of the program's file. Thank you Tomasz for your fast answere. That helped me to know, where I should know for the filename. Well, I presume that you've meant the PSP (http://en.wikipedia.org/wiki/Program_Segment_Prefix)? If yes: I've read the infos at wikipedia and compiled the program - but see at screen-shot - this just gives me the arguments, not the pathname+filename. Could you please give me some further hint how to find a solution - for example a link to a description of my problem or a source to a com-file, which gets its own filename. kind regards, SOE
|
||||||||||
26 Feb 2009, 15:45 |
|
Tomasz Grysztar 26 Feb 2009, 15:50
In PSP at offset 2Ch you have a 16-bit segment value of the environment segment. This segment contains (starting from offset 0) the copy of environment variables set specifically for you program, and then then array of additional strings that I mentioned earlier.
|
|||
26 Feb 2009, 15:50 |
|
Tomasz Grysztar 26 Feb 2009, 16:07
OK, I made a short example program for you. This program displays the full pathname of itself.
Code: org 100h mov ds,[ds:2Ch] ; for .com program CS=DS=PSP segment xor si,si ; DS:SI points to environment variables skip_environment_value: lodsb test al,al jnz skip_environment_value lodsb test al,al ; is this the end of environment? jnz skip_environment_value lodsw ; load amount of additional strings cmp ax,1 jb exit display_program_pathname: lodsb test al,al jz exit mov dl,al mov ah,2 int 21h jmp display_program_pathname exit: ret |
|||
26 Feb 2009, 16:07 |
|
SonOfEarth 26 Feb 2009, 16:55
OMG - thank you so much Tomasz! Thanks alot for that code and for all other things you have done for me by coding and documenting the amazing fasm!!!
Just befor seeing your code, i've also party understood what you said. I took out my old asm book, and read some things about segments - and wondered how i could have coded asm for so long without understanding the memory-structure of DOS. hehe! THANKS ALOT AGAIN!!! |
|||
26 Feb 2009, 16:55 |
|
DOS386 22 Mar 2009, 01:40
Tomasz Grysztar wrote: OK, I made a short example program for you. This program displays the full pathname of itself. COOL, FAQ added, nevertheless, one can save 5 more Bytes in the code: Code: ; SELFPATH.ASM - 45 Bytes FASM DOS example ; Get filename of itself / current running file AKA argv[0] ; http://board.flatassembler.net/topic.php?t=9872 ; ( See also : http://board.flatassembler.net/topic.php?t=838 ) ; 2009-Feb-26 16:07 ; 2009-Mar-20 improved: optimized (-5 Bytes), made nicer+safer (+14 Bytes) ; (CL) 2009 Public Domain - ABUSE at your own risk !!! format binary as "COM" org $0100 use16 mov ds, [ds:$2C] ; For DOS ".COM" program CS = DS = PSP segment xor si, si ; MOVNTQ SI,0 | DS:SI points to enviro area cld ; !!! skip_enviro_value: lodsw dec si ; !!! test ax, ax ; CMPNTQ AX, 0 | Is this the end of enviro ? jnz short skip_enviro_value inc si ; !!! lodsw ; Pick amount of additional strings test ax, ax ; CMPNTQ AX, 0 jz short wow_eol ; Not present !!! call wow_eol display_program_pathname: lodsb ; LODS BYTE [DS:SI] cmp al, 0 jz short wow_eol call write_char jmp short display_program_pathname ;------------------------------------- wow_eol: mov al, $0D call write_char mov al, $0A ; pass write_char: xchg ax, dx mov ah, 2 int $21 ret ;---- also, there is an other ( BETTER ) way to do (returns bare name only): Code: ; SELFNAME.ASM - 37 Bytes FASM DOS example ; Get filename of itself / current running file AKA argv[0] ; http://board.flatassembler.net/topic.php?t=9872 ; ( See also : http://board.flatassembler.net/topic.php?t=838 ) ; 2009-Mar-20 written: other solution for same problem, gives bare name only ; (CL) 2009 Public Domain - ABUSE at your own risk !!! format binary as "COM" org $0100 use16 cld mov ax, cs dec ax mov ds, ax mov si, 8 mov cx, si call wow_eol pick_byte: lodsb ; LODS BYTE [DS:SI] cmp al, 0 je short wow_eol call write_char loop short pick_byte wow_eol: mov al, $0D call write_char mov al, $0A ; pass write_char: xchg ax, dx mov ah, 2 int $21 ret ;---- Download now: http://board.flatassembler.net/download.php?id=4250 (3.5 KiB) |
|||
22 Mar 2009, 01:40 |
|
baldr 11 May 2009, 13:18
Do anybody encounter more than one additional string after environment?
|
|||
11 May 2009, 13:18 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.