CALL and USR

 

BASIC provides two methods for running sections of code, CALL and USR.

How to run it

When you create some code, such as:
  P% = code%

  [ OPT l%

  .exit
    MOV    PC, R14

  .printit
    CMP    R0, #0
    MOVEQ  PC, R14
    SWI    "OS_Write0"
    B      exit
  ]
You have defined three possible entry points: Though, two of those (code% and exit) point to the same place. In more complex code, I find it preferable to call code by label rather than simply calling the block of memory. This allows me to do stuff at the start like a branch table or data storage. Therefore, your code could be something like:
  $code%   = filename$+CHR$0
  code%!12 = filetype%
  code%!16 = filelength%
  CALL process_file
Parameters will have already been set up, so to read the filename you just ADR a register, then LDRB r, [pointer], #1 until you reach a zero byte.

Both CALL and USR accept register values. Whatever you set A% to H% to be will be stored in R0 to R7.

  A% = 1
  B% = 2
  CALL somecode
The function somecode will be entered with R0 set to 1 and R1 set to 2.

The principal difference, however, is that with CALL you can pass parameters...

  CALL mycode, name$, address$, file_handle%
...and USR takes no parameters, but will return the value of R0.
  result% = USR(mycode)

Unfortunately, you cannot pass parameters AND return a value in R0 at the same time.

 

For simple code, or code that could be run stand-alone (i.e., not from BASIC), this is all you really need to know.
However BASIC offers much much more. All of this is detailed in the section on extended BASIC facilities. It is worth taking a look at, as it offers access to several useful BASIC functions and status words.

 


Return to assembler index
Copyright © 2004 Richard Murray