!pr0
!lm12
!rm75
Using USR for a WEEK.......................Bob Sander-Cederlof

The "&" and CALL statements are not the only ways to use machine language to enhance the Applesoft Language.  USR is a third way, and provides an easy way to return a single value.

How many times have you seen the Applesoft code "PEEK(X) + 256*PEEK(X+1)"?  It is used over and over again.  What it does is look in memory at X and X+1 for a 16-bit value (stored low-byte first as are most 16-bit values in the 6502 environment).  The high byte is multiplied by 256, and the low byte added in.  Wouldn't it be nice to have a USR function which would convert a two-byte value directly?  This function is sometimes called "WEEK", meaning "Word pEEK" (hence the awful pun in the title above).

When I was in California last week someone categorically and unequivocally assured me that it is impossible to use the USR function with a value of 32768.  I tried it with the WEEK function, and it works fine.  So much for the assurances!  I think his problem was that he followed the instructions in the Applesoft manual, which are somewhat incomplete.

Here is the USR code, set up to run at $300.  However, it is "run-anywhere" code, because there are no internal references.  You do have to tell Applesoft where it starts, though.  Line 100 in the example shows how to do that.  Location 11 and 12 must be set to the low- and high-bytes of the address of the USR code.
