For BASIC programming, the areas of RAM normally available for storage of programs and variables are locations 7168-65279/ $1COO-$FEFF in block 0 and 1024-65279/$0400-$FEFF in block 1. This is a total of 122,368 bytes of available RAM space (illustrated in Figure 4-1). This explains why part of the message you see when you turn on or reset the computer says 122365 BYTES FREE. (The missing three bytes are to account
for the zero byte required by BASIC before the first program line and the two zero bytes used to mark the end of the program.)
Actually, it's a bit misleading to claim that many free bytes, since you can't write a BASIC program 120,000 bytes long. For BASIC, the free RAM is divided into two distinct segments: the 58,112 bytes in block 0 for BASIC program text and the 64,256 bytes in block 1 for variables and strings. (By comparison, the Commodore 64 offers 38,911 bytes for pro- gram text and variables combined.)
As noted in Figure 4-1, there is one additional factor which affects the amount of memory available for program text. When you use a GRAPHIC statement to set up a high- resolution screen, an additional 9K is reserved in block 0: IK at 7168-8191/$1COO-$1FFF for color information and 8K at 8192-16383/$2000-$3FFF for the high-resolution-screen bit- map. In this case, the amount of RAM available for BASIC program text is reduced to 48,896 bytes (locations 16384-65279/ $4000-$FEFF in block 0). If a program is already in memory when the GRAPHIC statement is executed, the program is moved upward in memory (the starting address will be changed from 7169/$1CO1 to 16385/$4001) and relinked to work at the new addresses. Once a high-resolution memory area is established, it remains allocated until a GRAPHIC CLR statement is executed, at which time the program text is moved down to start at 7169/$1CO1 again.
Pointers in zero page and page 18/$ 12 are used to specify the length of program text and variables. BASIC program text is assumed to begin at the address in block 0 specified in loca- tions 45-46/$2D-$2E. That pointer is initialized to 7169/ $1CO1 during the BASIC cold start routine [$4023]. Unlike the Commodore 64, which sets its start-of-BASIC pointer accord- ing to the value in the system's start-of-free-memory pointer, the 128 sets the address value without regard for the value in 2565-2566/$0A05-$0A06. Locations 4626-4627/$1212-$1213 point to one byte beyond the highest available address in block 0. That pointer is initialized during BASIC cold start to 65280/$FF00, again without regard to the Kernal memory pointer value in 2567-2568/$0A07-$0A08. The actual ending address of the program text currently in memory is specified by the value in 4624-4625/$1210-$1211. That pointer is ini- tialized during the BASIC CLR routine [$51F8] to two bytes
184
Figure 4-1. BASIC RAM Usage
for the zero byte required by BASIC before the first program line and the two zero bytes used to mark the end of the program.)
Actually, it's a bit misleading to claim that many free bytes, since you can't write a BASIC program 120,000 bytes long. For BASIC, the free RAM is divided into two distinct segments: the 58,112 bytes in block 0 for BASIC program text and the 64,256 bytes in block 1 for variables and strings. (By comparison, the Commodore 64 offers 38,911 bytes for pro- gram text and variables combined.)
As noted in Figure 4-1, there is one additional factor which affects the amount of memory available for program text. When you use a GRAPHIC statement to set up a high- resolution screen, an additional 9K is reserved in block 0: IK at 7168-8191/$1COO-$1FFF for color information and 8K at 8192-16383/$2000-$3FFF for the high-resolution-screen bit- map. In this case, the amount of RAM available for BASIC program text is reduced to 48,896 bytes (locations 16384-65279/ $4000-$FEFF in block 0). If a program is already in memory when the GRAPHIC statement is executed, the program is moved upward in memory (the starting address will be changed from 7169/$1CO1 to 16385/$4001) and relinked to work at the new addresses. Once a high-resolution memory area is established, it remains allocated until a GRAPHIC CLR statement is executed, at which time the program text is moved down to start at 7169/$1CO1 again.
Pointers in zero page and page 18/$ 12 are used to specify the length of program text and variables. BASIC program text is assumed to begin at the address in block 0 specified in loca- tions 45-46/$2D-$2E. That pointer is initialized to 7169/ $1CO1 during the BASIC cold start routine [$4023]. Unlike the Commodore 64, which sets its start-of-BASIC pointer accord- ing to the value in the system's start-of-free-memory pointer, the 128 sets the address value without regard for the value in 2565-2566/$0A05-$0A06. Locations 4626-4627/$1212-$1213 point to one byte beyond the highest available address in block 0. That pointer is initialized during BASIC cold start to 65280/$FF00, again without regard to the Kernal memory pointer value in 2567-2568/$0A07-$0A08. The actual ending address of the program text currently in memory is specified by the value in 4624-4625/$1210-$1211. That pointer is ini- tialized during the BASIC CLR routine [$51F8] to two bytes
184
Figure 4-1. BASIC RAM Usage
beyond the starting address in 45-46/$2D-$2E. An OUT OF MEMORY error occurs if the address in 4624-4625/ $1210-$1211 reaches the value in 4626-4627/ $1212-$1213. The ending address pointer is set after a BASIC LOAD [$912C], and the BASIC SAVE routine [$9112] uses the values in the starting and ending address pointers as the starting and end- ing address for the block of memory to be saved.
The address in the pointer at locations 47-48/$2F-$30 marks the start of scalar (nonarray) variables in bank I. The pointer is initialized to 1024/$0400 during the BASIC cold start routine. A pointer at 49-50/$31-$32 marks the end of scalar variables and the beginning of arrays; another pointer at 51-52/$33-$34 marks the end of arrays and the beginning of free memory in block 1. Both of these pointers are reset to the value in 47-48/$2F-$30 during the BASIC CLR routine. The pointer at 57-58/$39-$3A holds an address which is one byte beyond the highest address of free memory in block 1. It is initialized during BASIC cold start to point to 65280/$FF00. The free memory in block 1 is used to hold strings of all types—constants, variables, and arrays. The string pool starts at the top of free memory and is filled downward toward the bottom of free memory indicated in 51-52/$33-$34. The pointer at 53-54/$35-$36 marks the current address of the bottom of the string pool. That pointer is reset to the value in 57-58/$39-$3A by the BASIC CLR routine. An OUT OF MEMORY error occurs when the value in 53-54/$35-$36 reaches the value in 51-52/$33-$34.