Reading command line

From BeebWiki
Jump to: navigation, search

When a transient command is run from a filing system OSARGS 1,0 can be used to find the command line parameters. This command line is always stored in the I/O processor. Transient commands usually run in the I/O processor so they can read the command line parameters directly using code such as LDA (zp),Y:

   \ lptr    = address in zero page to point to command line parameters
   \ Note, four bytes are read, so lptr+2 and lptr+3 are overwritten
   
   LDA #1:LDY #0:LDX #lptr:JSR OSARGS   :\ Read address of command line parameters
   
   \ (lptr),Y now points to the start of the command's parameters

However, if a transient command is written to run in the language processor it must copy the command line to local memory, or scan it character by character over the Tube. The following code fragments copy the command line parameters to local memory. Note that this method only works with code that is run from a file, running code selected with a ROM command needs a different method as OSARGS points to the last file run, not the last command issued.

Contents

6502

   \ addr    = 5-byte buffer in zero page
   \ cmdline = 256-byte buffer to copy command line parameters to
   
   LDA #1:LDY #0:LDX #addr:JSR OSARGS   :\ Read address of command line parameters
   .rdcmdlp
   TYA:PHA
   LDX #addr:LDY #0:LDA #5:JSR OSWORD   :\ Read byte from I/O memory
   PLA:TAY:LDA addr+4:STA cmdline,Y:INY :\ Copy byte to local buffer
   INC addr:BNE rdcmdnxt:INC addr+1     :\ Increment command line address
   .rdcmdnxt
   CMP #13:BNE rdcmdlp                  :\ Loop until <cr>

Z80

   \ addr    = 5-byte buffer
   \ cmdline = 256-byte buffer to copy command line parameters to
   
   LD A,1:LD E,0:LD HL,addr:CALL OSARGS :\ Read address of command line parameters
   LD DE,cmdline
   .rdcmdlp
   LD HL,addr:LD A,5:CALL OSWORD        :\ Read byte from I/O memory
   LD A,(addr+4):LD (DE),A:INC DE       :\ Copy byte to local buffer
   LD HL,(addr):INC HL:LD (addr),HL     :\ Increment command line address
   CP 13:JR NZ,rdcmdlp                  :\ Loop until <cr>

6809

   \ Code is entered with X=>command line parameters in local memory

80186

PDP11

   \ If code has a Unix header, SP=>stack frame with parameters
   \ Otherwise, R1=>command line parameters in local memory

32016

ARM

There is difficulty with ARM systems. The canonical method is to use SWI "OS_GetEnv", but OS_GetEnv returns inconsistant results on different ARM platforms. RISC OS and the ARM Development System return R0=>command line, but the Sprow CoPro returns R0=>"GO". Additionally, only RISC OS parses "*RUN filename parameters" correctly.

The following code will read the command line parameters to local memory on RISC OS, the ARM Development System, the Sprow ARM CoPro and the PiTube Native ARM CoPro.

   MOV R0,#1:MOV R1,#0:MOV R2,#0
   SWI "XOS_Args"              :\ R2=address of comand line parameters
   ORRS R2,R2,R2:BMI rdcmdline :\ Command line in I/O processor
   SWI "OS_GetEnv"             :\ Read command line from local memory
   .rdcmdlinelp1
   LDRB R1,[R0],#1:CMP R1,#ASC" "
   BHI rdcmdlinelp1:SUB R0,R0,#1
   .rdcmdlinelp2
   LDRB R1,[R0],#1:CMP R1,#ASC" "
   BEQ rdcmdlinelp2:SUB R0,R0,#1
   B rdcmdlinedone           :\ R0=>command line parameters
   :
   .rdcmdline
   ADR  R3,cmdline           :\ R3=>local buffer
   ADR  R1,addr              :\ Point to control block
   .rdcmdlp1
   STR  R2,addr              :\ Store address in control block
   MOV  R0,#5:SWI "OS_Word"  :\ Read byte from I/O memory
   LDRB R0,[R1,#4]           :\ Get byte read
   STRB R0,[R3],#1           :\ Store in local buffer
   ADD  R2,R2,#1             :\ Increment command line address
   CMP  R0,#13:BNE rdcmdlp1  :\ Loop until <cr>
   ADR  R0,cmdline           :\ R0=>command line parameters
   .rdcmdlinedone
   :
   \ R0=>command line in local memory

BASIC

If the transient command is written in BASIC, the command line is already in local memory due to it being used by BASIC to load the program, and is collectable with the FNOS_GetEnv function in the ProgEnv program library.

Notes

Some filing systems provide the OSGBPB 9,0 call to copy the command line parameters to local memory.

Jgharston 13:56, 2 December 2009 (UTC) Jgharston (talk) 00:07, 11 December 2017 (CET) Jgharston (talk) 01:16, 1 July 2018 (CEST)