Command line parameters

Anything QL Software or Programming Related.
Martin_Head
Aurora
Posts: 868
Joined: Tue Dec 17, 2013 1:17 pm

Command line parameters

Post by Martin_Head »

In a machine code procedure/function I would like to be able to enter a filename without it being in quotes.

For example COMMAND win2_SomeFileName
rather than COMMAND "win2_SomeFileName"

I know you need to examine, and alter the Type word in the Name Table, and alter the entry in the Name List.

The QDOS SMS Reference Manual (page 51) talks about it, but is a bit vague. It says you have to fetch the parameter from the name list and convert it to a string. But it doesn't say what you do with it, does it stay where it was, or is it moved to the Variable Value area? If so how?

Does anyone have, or can point me at, a bit of sample code that does this, so I figure out how to do it. Or is it very complicated, and not really worth the bother.

Also can anyone confirm that if I am in a machine code procedure/function, I don't need to worry about clearing any unread parameters, or removing everything I've placed on the maths stack (except for Functions) before returning to Basic.

My machine code books say you don't, I just want to be sure.


User avatar
tofro
Font of All Knowledge
Posts: 2724
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: Command line parameters

Post by tofro »

Martin_Head wrote:In a machine code procedure/function I would like to be able to enter a filename without it being in quotes.

Does anyone have, or can point me at, a bit of sample code that does this, so I figure out how to do it. Or is it very complicated, and not really worth the bother.
Martin,

Norman has a chapter on NAME type S*BASIC parameters in his (BTW excellent and thoroughly recommended) Assembler book here
http://qdosmsq.dunbar-it.co.uk/download ... sembly.pdf
Refer to chapter 7.7 (and don't forget to read the rest...)
Martin_Head wrote: Also can anyone confirm that if I am in a machine code procedure/function, I don't need to worry about clearing any unread parameters, or removing everything I've placed on the maths stack (except for Functions) before returning to Basic.
unread parameters don't need to be handled by your code (although I would consider it good practice that an assembler FN or PROC would return an error message if supplied with incorrect number of parameters which would basically rule out that possibility.... - Standard practice allows optional parameters to be left out, but supplying too many would normally result in an error) - S*Basic will clean up after rts from the assembler PROCedure or FuNction (i.e. will basically re-set the stack to where it was before your code was called).

It's a bit different with the maths stack in general - If you have obtained the math stack pointer by any of the parameter fetch routines (i.e. you're in a S*BASIC extension), BASIC will clean up after you, as above.

In case you have obtained the A1 pointer by some other means (i.e you're not necessarily in an S*BASIC extension and have obtained the maths stack from the system variables) - You need to clean up yourself as you can't rely on BASIC in this case.

Hope this helps,
Tobias


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
User avatar
pjw
QL Wafer Drive
Posts: 1322
Joined: Fri Jul 11, 2014 8:44 am
Location: Norway
Contact:

Re: Command line parameters

Post by pjw »

On returning from a machine code S*Basic function the stack does have to be in order: Only the return value must be left on the stack with both a1 and the RI stack pointer pointing to it (and d4 must contain the code for the type of value to be returned). If you fail to do this, the stack will grow on each use of the function - or the system will crash, depending.. On errors, however, ie when no value gets returned, these steps arnt necessary.

Per


Per
dont be happy. worry
- ?
User avatar
NormanDunbar
Forum Moderator
Posts: 2287
Joined: Tue Dec 14, 2010 9:04 am
Location: Leeds, West Yorkshire, UK
Contact:

Re: Command line parameters

Post by NormanDunbar »

As far as I remember, if you check the parameter type and find a name, you need to copy the name from the name table. If you found a string, then it will be on the maths stack after fetching.

In a Procedure, only, you can leave any old crud on the stack when you return, for a Function, you must clear all the input parameters off, except for enough space for the return, and make sure BV_RIP is set correctly.

As Tobias pointed out, it's covered in my free book which also has a chapter on the maths stack.

Simon N Goodwin gave at least one example of getting a name rather than a string in DIY Toolkit, but I cannot remember which routine. Have a look at the code and docs for any routine that takes a filename parameter.

Cheers,
Norm.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals
Author of Arduino Interrupts

No longer on Twitter, find me on https://mastodon.scot/@NormanDunbar.
Martin_Head
Aurora
Posts: 868
Joined: Tue Dec 17, 2013 1:17 pm

Re: Command line parameters

Post by Martin_Head »

Thanks to fro,pjw and Norman.

My query about clearing up the maths stack, is that I have a procedure that takes three strings, (one being a filename) which are read and placed on the maths stack. after processing the strings, I was adjusting the maths stack pointer to clear them before returning to BASIC, while trying to take care of odd length strings etc.

The routine seems to work OK in the main SBASIC job (0), but I suspect I'm getting something wrong, because if I have daughter SBASIC job running, It's inclined to crash if I try to run a program in it, after executing my procedure. (or the whole system will crash) I'm using QPC2 by the way.
However if I don't enter the daughter SBASIC job, the main SBASIC does not seem to care and carries on fine. It's all a bit random, and I have not yet found a way to duplicate it exactly. So I'm not entirely sure it's the execution of my procedure (which is part of a larger project), or not.

I don't know if there is any connection between the Name Tables and the Maths Stack, with the main SBASIC and SBASIC daughter jobs, for one to be able to crash the other.

So I wanted to know if it was safe, to not bother clearing up the Maths stack before exiting the procedure, in case I am messing it up.

I've downloaded the PDF by Norman Dunbar, and I will have a look at it.

On the filename question. I thought there might be something in one of the DIY Toolkits in QL World. I've looked at few of my copies, but have not yet found anything that helps.

I suspect it's something quite complicated to do, or it would be done more often, and well known about.


User avatar
tofro
Font of All Knowledge
Posts: 2724
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: Command line parameters

Post by tofro »

Martin_Head wrote:.... I was adjusting the maths stack pointer to clear them before returning to BASIC, while trying to take care of odd length strings etc.
It would be interesting to know how you did this, as any manual changes to the AS need to be in properly allocated memory and reflected in the system variables.
Martin_Head wrote: I don't know if there is any connection between the Name Tables and the Maths Stack, with the main SBASIC and SBASIC daughter jobs, for one to be able to crash the other.
That's interesting stuff - I personally do not know how the interpreter synchronizes access between several SBASIC tasks (especially from machine code procedures). My need a bit of investigation.
Martin_Head wrote: I suspect it's something quite complicated to do, or it would be done more often, and well known about.
It's actually rather simple - instead of picking the string from the AS, you're searching for the name in the name table and pick the string from there. I guess it's done so rarely because you can easily achieve the same thing by only accepting strings ;)

Tobias


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
User avatar
pjw
QL Wafer Drive
Posts: 1322
Joined: Fri Jul 11, 2014 8:44 am
Location: Norway
Contact:

Re: Command line parameters

Post by pjw »

If your m/c routine is an S*Basic PROCedure there is no need to clear the stack on return! Just leave it alone.

Its really not that hard to get the name of a variable rather than its value (which, in effect, is what you are trying to do with quote-less file names):

Eg: MYPROC file_name_ext or MYPROC filename$

On entry to the procedure a3 points to the name table (see the QDOS/SMSQ Bible for the nitty-gritty). You probably want to test the type first: If its a string, fetch it in the normal way, otherwise consider it a 'name'. 2(a6,a3).w is the offset to the name from the name list. (The format of the name is unusual for the QL as the length is given by a single byte instead of a word.) Make room on the stack, or wherever, for the length word and the name string, and copy it over.

There is a little more to it than that, as you need to remove delimeter information and weed out expressions etc. But these are the basics, if you want to have a go. Otherwise there is a tried and tested Tony Tebby routine in the PD somewhere, called STOS, that does all that. I'll see if I can find it..

There are two potential disadvantages with using unquoted names: The names must comply with the variable name standard, ie MYPROC funny%_file wont work. The other is that if you want to compile a program that avails itself of this shortcut, you miss out on the opportunity to save some bytes as you have to include the name table with the compilation. (Both easy enough to work around, of course.)

Per


Per
dont be happy. worry
- ?
User avatar
tofro
Font of All Knowledge
Posts: 2724
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: Command line parameters

Post by tofro »

pjw wrote: There are two potential disadvantages with using unquoted names: The names must comply with the variable name standard, ie MYPROC funny%_file wont work. The other is that if you want to compile a program that avails itself of this shortcut, you miss out on the opportunity to save some bytes as you have to include the name table with the compilation. (Both easy enough to work around, of course.)
Per
Also, if you're using a lot of names in interpreted basic, this will occupy memory in the name table until the next NEW - Strings can be cleaned up much easier. Other than NEW, there's no way to remove something from the name table.

Tobias


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
User avatar
NormanDunbar
Forum Moderator
Posts: 2287
Joined: Tue Dec 14, 2010 9:04 am
Location: Leeds, West Yorkshire, UK
Contact:

Re: Command line parameters

Post by NormanDunbar »

I had a quick look in the DIY Toolkit source. Volume V has more_asm which displays a file on screen one page at a time. The following code can be found in there....

Code: Select all

*
* Get the file name parameter as a string or a name
*
get_name   tst.b      0(a3,a6.l)     Is the value set?
           bne.s      value_set      If so, use GTSTR to read it
           movea.l    24(a6),a0      A0 -> SuperBasic Name Table
           move.w     2(a3,a6.l),d0  D0 is index of parameter name
           lsl.w      #3,d0          Scale for eight byte entries
           adda.w     d0,a0          Implicitly extends D0 to match A0
           move.w     2(a0,a6.l),d0  D0 is offset of text in Name List 
           ext.l      d0             Data regs. need explicit extension
           add.l      32(a6),d0      Add Name List base offset
           move.l     d0,a2          Save total offset for later
           lea.l      filename(buf),a3
           move.l     a3,a0          Set name offset for OPEN later
           moveq      #0,d1          Clear high bytes of D1
           move.b     0(a6,d0.l),d1  D1 is length of name (1..255)
           clr.b      0(a3,a6.l)     Clear high byte of buffer word length
copy_name  move.b     0(a6,d0.l),1(a3,a6.l)
           addq.l     #1,a3          Advance through buffer
           addq.l     #1,d0          Advance through Name List
           dbra       d1,copy_name   Copy D1+1 bytes including length
           bra.s      name_ready
Hope that helps, and also that the formatting is ok after I submit this!

There will be unresolved labels in the above, but it's a pointer in the right direction.

As for procedures, my book covers leaving the A1 maths stack well alone. I'm not sure about daughter S BASIC jobs as I've never bothered with them myself.


Cheers,
Norm.


Why do they put lightning conductors on churches?
Author of Arduino Software Internals
Author of Arduino Interrupts

No longer on Twitter, find me on https://mastodon.scot/@NormanDunbar.
User avatar
pjw
QL Wafer Drive
Posts: 1322
Joined: Fri Jul 11, 2014 8:44 am
Location: Norway
Contact:

Re: Command line parameters

Post by pjw »

Martin_Head writes:
The routine seems to work OK in the main SBASIC job (0), but I suspect I'm getting something wrong, because if I have daughter SBASIC job running, It's inclined to crash if I try to run a program in it, after executing my procedure. (or the whole system will crash)
From the toolkit programmer's view, there is no difference between SBasic's daughter jobs and job 0. Each SBasic has its own name table (parts of which are inherited (and supplemented) from job 0). It is unlikely that a daughter job should affect job 0 or vice versa other than this unless there is a programming error. RI stack errors are common culprits in such cases, in my experience, but that could just be me..

Per


Per
dont be happy. worry
- ?
Post Reply