Borrowing ML From BASIC
How does a command like SYS 49152, 1000, A$ (1) work? I know the SYS command calls the routine, but how do you make SYS use the number and the string variable after 49152?
It's usually done by calling the same routines in ROM (Read Only Memory) that BASIC uses to accept information. Of course, this must be done from within the ML program called by SYS. Here's a short program for the Commodore 64 that shows one way to handle the statement you mentioned. You'll need a machine language assembler to type it in (the comments after the semicolons are optional):
JSR $AEFD ; Check for comma. JSR $AD8A ; Get numeric expression. LDA #$4E ; (Put your JSR $FFD2 ; code here.) JSR $AEFD ; Check second comma. JSR $AD9E ; Get any expression. BIT $0D ; Check string/numeric BPL ERROR ; flag in $ 0D. LDA #$53 ; (put your JSR $FFD2 ; code here.) RTS ERROR LDX #$16 ; Output TYPE JMP $A437 ; MISMATCH ERROR.
If you assemble this program at 49152, it accepts the statement SYS 49152,1000, A$(l), printing an N when it confirms that the first value is numeric and S when it determines that the second value is a string. (Of course, a working program would do something more useful than print N and S.) As long as you separate the expressions with commas, you can replace the number with any numeric expression (such as a numeric variable) and substitute any string expression for A$(l). For instance, SYS 49152,X,"HELLO" is also acceptable.
One advantage of using existing routines is that normal error-handling is preserved. This example detects missing or misplaced parameters as well as type mismatch errors (putting a string value where a number is expected or vice versa). The ROM routine at $AD8A looks for a numeric expression: If it finds a string instead, it automatically prints an error message and returns control to BASIC. $AD9E is BASIC'S all-purpose evaluation routine: It accepts any expression and sets the flag in location $0D to show whether it's a string ($0D=$FF) or a number ($0D = 0). BASIC'S general error-handler ($A437) prints a BASIC error message determined by what value the X register holds when it is called.
Naturally, it's your job to do something useful with the information once it has been passed. The computer's ROM contains a host of other routines that can simplify that task as well. You may find detailed discussions of the ROM routines in the 64 and VIC-20 in Tool Kit: BASIC and Tool Kit: Kernal, both available from COMPUTE! Books. Commented listings for the 64's ROM can be found in Anatomy of the Commodore 64, available from Abacus Software.