[Prev] [Next] [Contents] [Commodore] [New] [Search] [Home]

USING MACHINE LANGUAGE FROM BASIC

There are several methods of using BASIC and machine language on the Commodore 64, including special statements as part of CBM BASIC as well as key locations in the machine. There are five main ways to use machine language routines from BASIC on the Commodore 64. They are:

1) The BASIC SYS statement
2) The BASIC USR function
3) Changing one of the RAM I/O vectors
4) Changing one of the RAM interrupt vectors
5) Changing the CHRGET routine

1) The BASIC statement SYS X causes a JUMP to a machine language subroutine located at address X. The routine must end with an RTS (ReTurn from Subroutine) instruction. This will transfer control back to BASIC.

Parameters are generally passed between the machine language routine and the BASIC program using the BASIC PEEK and POKE statements, and their machine language equivalents.

The SYS command is the most useful method of combining BASIC with machine language. PEEKs and POKEs make multiple parameter passing easy. There can be many SYS statements in a program, each to a different (or even the same) machine language routine.

2) The BASIC function USR(X) transfers control to the machine language subroutine located at the address stored in locations 785 and 786. (The address is stored in standard low-byte/high-byte format.) The value X is evaluated and passed to the machine language subroutine through floating point accumulator #1, located beginning at address $61 (see memory map for more details). A value may be returned back to the BASIC program by placing it in the floating point accumulator. The machine language routine must end with an RTS instruction to return to BASIC.

This statement is different from the SYS, because you have to set up an indirect vector. Also different is the format through which the variable is passed (floating point format). The indirect vector must be changed if more than one machine language routine is used.


[Prev] [Next] [Contents] [Commodore] [New] [Search] [Home]
This page has been created by Sami Rautiainen.
Read the small print. Last updated December 17, 2002.