Can anybody help me retrieve the ARGC and the pointer to ARGV without using external libraries coded in C or other languages?

P.S : 16 bit with 286 instruction set
Posted on 2006-07-01 08:35:21 by XCHG
No. Those two variables are LibC constructs. The OS is the one that parses the command line and creates those variables.

If you try to do it "manually", in any sense of the word, you will still be using Library/API calls to get the argument counter/pointer for the variables.

The problem is, when you use certain linkers to link "CONSOLE" programs, they fill in the "startup" code for you. Part of that is getting ARGC and ARGV and putting their values "above the stack" when your program "main" is called.
Posted on 2006-07-01 18:17:01 by SpooK
Thank you spook for your reply. I'm wondering if there is a way i could retrieve those values from the stack pointer. I mean there should be a way right? When you execute the program with parameters wouldn't the OS let the program explicitly, somehow, access them?

Regards
Posted on 2006-07-02 12:09:32 by XCHG
Yes, that is the purpose of the OS and/or linker startup code.

Normally, when you enter your program entry point (main), you should be able to access ARGC as "ebp+4" and ARGV as "ebp+8".
Posted on 2006-07-02 13:01:52 by SpooK
Ah tough luck, i don't seem to be able to get it to work  :sad:

P.S : I compile the program with the command "TASM %PROG%" and link it with "TLINK %PROG%". The source is for an EXE file. STACK 500h
Posted on 2006-07-03 07:20:43 by XCHG
If you're doing raw asm without any libc under 16bit DOS, iirc there's nothing really useful on the stack. I honestly can't remember where the commandline is located, but something tells me it's in the Program Segment Prefix (PSP).

Yeah, just checked HELPPC, it's the last part of the PSP. This is a raw string that you'll have to parse manually.
Posted on 2006-07-03 12:35:37 by f0dder
Alright! I had not fiddled around with PSP ever, i am now reading an article about it. thank you both for the help. I'm thankful beyond words.
Posted on 2006-07-04 04:16:20 by XCHG
No problem :)
Posted on 2006-07-04 06:07:08 by f0dder
The offset into the PSP is 0080h, if that helps.

To retrieve the address, ah=62h, int 21h

PSP segment address is returned in BX

Paul
Posted on 2006-07-04 17:35:38 by PBrennick
Well thanks guys. i found three ways for retrieving ARGC and ARGV. First is the 51h function of INT 21 which would return the pointer to the starting point of the PSP in the BX register.

The second way is the function 62h of the same interrupt which would do the same as the 51h function except for the fact that the latter works on DOS 2 or greater but the 62h function works on DOS 3 or better.

By reading more articles i found that DOS moves the pointer to the PSP to the DS segment register when it starts either a COM file or an exe file. So now by knowing the offsets, 80h from the beginning of the PSP is the length of the arguments including the Carriage Return and 81h from the beginning of the PSP is the pointer to the starting point of the actual parameters, i could then write the below program because perhaps there are some others in the same board that are struggling with what i was. The below program uses 186 instruction set and is heavily commented. hope it helps.



Posted on 2006-07-05 12:18:10 by XCHG
ES also points to the PSP on startup. You do not need to copy it from DS.
Posted on 2006-07-05 20:22:08 by tenkey