FRE

From C64-Wiki
Jump to navigationJump to search
BASIC keyword
Keyword: FRE
Abbreviation: F Shift+R
Type: Function
Token code: 184/$B8
Handling routine
in BASIC ROM:
45949–45968
$B37D–B390
List of all BASIC keywords


Remark: This article describes the BASIC command FRE in BASIC V2 of the Commodore 64.

Type: Function 
General Programming-Syntax: FRE(<dummy argument>)

FRE accepts any numerical argument within the limits of the floating point format or any string argument, and returns the number of unused bytes of BASIC RAM. The argument has no influence on the result, and thus it seems this "function" is a system variable rather than a function per se.

ROM details[edit | edit source]

FRE first performs a garbage collection to free up unused string memory. Then it calculates the RAM "gap" between the top of array memory (as indicated by the pointer at 49–50/$31–$32) and the bottom of string memory (pointer located at 51–52/$33–$34), by subtracting the former from the latter.

Although the "binary" result of this calculation is an unsigned 16-bit integer, the creators of BASIC V2 seemingly saw fit to use a routine for converting the result back into floating point, which takes the result as a signed integer: As a result, if the amount of free memory exceeds 32767 bytes, FRE will return a negative number. To correct for this, add 65536 to the result if it comes out negative (see section Examples).

If the argument (either numeric or even a string) is missing or more then one argument is given the BASIC error ?SYNTAX ERROR IN line is displayed.

Examples[edit | edit source]

PRINT FRE(1)
PRINT FRE(0)+65536

The example below creates a user-defined function that fixes the "negative" results, and then uses it to report even the free memory well over 32767 bytes:

10 DEF FN FR(X)=FRE(0)-65536*(FRE(0)<0)
20 PRINT FN FR(0);"BYTES FREE"
RUN
 38834 BYTES FREE
BASIC V2.0 (second release) Commands

ABS | AND | ASC | ATN | CHR$ | CLOSE | CLR | CMD | CONT | COS | DATA | DEF | DIM | END | EXP | FN | FOR | FRE | GET | GET# | GOSUB | GOTO | IF | INPUT | INPUT# | INT | LEFT$ | LEN | LET | LIST | LOAD | LOG | MID$ | NEW | NEXT | NOT | ON | OPEN | OR | PEEK | π | POKE | POS | PRINT | PRINT# | READ | REM | RESTORE | RETURN | RIGHT$ | RND | RUN | SAVE | SGN | SIN | SPC | SQR | STATUS/ST | STEP | STOP | STR$ | SYS | TAB | TAN | THEN | TIME/TI | TIME$/TI$ | TO | USR | VAL | VERIFY | WAIT