Vector

From C64-Wiki
Jump to navigationJump to search

A vector is a pair of bytes, which together form the start address of a machine language subroutine or program. Vectors are very similar to pointers (and sometimes these two words are used interchangeably), but strictly speaking, a vector points to executable (machine) code, whereas pointers point to data.

Since vectors are used in conjunction with the JMP instruction, they always conform to the convention that the first of the two bytes represent the 8 least significant bits, or the "low-byte" first, followed by the byte holding the 8 most significant bits, or the "high-byte".

The general "formula" for reading out the actual address pointed to by a vector (as well as by a pointer) at address n is:

PRINT PEEK(n)+256*PEEK(n+1)

For instance, the two bytes at the addresses 788–789 form the vector for the standard IRQ interrupt service routine: PEEKing these two addresses normally yields the standard values 49/$31 (low-byte), and 234/$EA (high-byte). Applying the formula above, the actual entry point for this routine comes out as:

PRINT PEEK(788)+256*PEEK(789)
 59953
READY.

Vectors are used a lot by the routines held in KERNAL and BASIC ROM, which set up numerous vectors in RAM (mostly within the 768–819/$0300–0333 range) at initialization, and later uses indirect JMP's through these RAM vectors when calling key routines. This allows the programmer to "redirect" parts of the system to use his/her own routines in stead of the standard ones in ROM, thereby adding new features and capabilities to the system, e.g. BASIC expansions. System routines that are "hooked up" on vectors at a specific address in this manner, are said to be vectored through that address.