Commodore‎ > ‎BASIC‎ > ‎Keywords‎ > ‎

SYS

Keyword Abbreviation Token (hex) Version(s) Classification
SYS S{Shift+Y} 9E 1.0+ Command and Statement

 
Syntax  
SYS address [ , registerA [ , registerX [ , registerY [ , registerP ] ] ] ]
 
Parameters Type Legal Value(s) Default Value Note(s)
address Unsigned integer
0 to 65535
 
Start of machine-language program.
registerA Unsigned byte
0 to 255
0
Only in BASIC 7.0
registerX  Unsigned byte  0 to 255  Only in BASIC 7.0 
registerY  Unsigned byte  0 to 255  0 Only in BASIC 7.0 
registerP  Unsigned byte  0 to 255  Only in BASIC 7.0 
 
 
Purpose  
Call a machine-langauge subroutine.
 
 
Remarks  
This statement calls a non-BASIC (machine-language) program in memory (either RAM or ROM).  If it returns to BASIC, then this statement is like GOSUB.  However the machine-language software may take over the machine and never return to BASIC; in which case this statement is like GOTO.  On machines with more than 64K of system RAM, the BANK statement should be used prior to SYS, so BASIC will access the correct "memory".
 
In versions of BASIC prior to 7.0, setting up the A, X, and Y registers of the CPU for the start of the program was only possible with machine-specific POKEs.  In those versions, attempting to pass CPU registers like shown above for v7.0 would typically generate a SYNTAX ERROR.  However, because the called program completely takes over the machine, it may be written to actually read the text (if any) following the address.  Such cases are implementation-dependant, so I can't tell you what would happen; consult any documentation you have for that particular software.
 
Because this is a statement, and not a function (compare with USR), there is no official "return value."  However, in BASIC v7.0 the RREG statement may be used to return the A, X, Y, and P registers when the machine-language program ended, and in earlier versions PEEKing the addresses used to set the registers would accomplish the same thing.
 
In versions of BASIC before 7.0, the A, X, Y, and P registers that are passed to and returned from the machine language program are secret variables.
 
Similar to POKE, the SYS statement is typically used to accomplish things that are not possible (or extremely clumsy) with BASIC.  Some programs consist only of an SYS command!  These are machine-language programs designed to be started with BASIC.  (Most machine-language programs are not started with BASIC.)  For example, a variety of machine-language programs were developed for the Commodore 64 to allow BASIC programs access to the useful features of that machine (such as bitmap graphics and music) that are not practical with BASIC v2.
 
If the address or any register is a string, a TYPE MISMATCH ERROR occurs.  Otherwise if any are not legal (see table above) then ILLEGAL QUANTITY ERROR is generated.
 
The safe / useful set of address(s) depend on what is currently in memory and is very machine-specific; this statement is non-portable.  As such, there are no examples.
 
If the address points to a "break" opcode, or if a "break" opcode is executed while the ML program is running then either a machine-language monitor (see MONITOR) will start-up or, if the machine does not have an ML monitor, the system will "soft-reset" (just like pressing STOP + RESTORE on the keyboard).  Machines that do not have the MONITOR keyword (but do have an ML monitor) may be able to start the ML monitor by giving an address that contains a zero byte (a CPU "break" opcode).  For example SYS 5.  This is very machine-specific, and some machines do not have an ML monitor in ROM; they need a special cartridge or a monitor loaded into RAM.
 
If the address points to an "illegal" opcode (not neccessarily an undocumented opcode) or if one is ever executed while the ML program runs, the machine will "hard-lock": pressing STOP + RESTORE will not work.  The only way to recover from a "hard lock" is to press the Reset button, if available; otherwise the computer must be powered off!  This means you would loose all unsaved data, so be careful.
 
 
Compare With  
 
See Also  

© H2Obsession, 2014
Comments