FRE

From C64-Wiki

Revision as of 21:04, 3 January 2010 by Jodigi (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, 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 at the Commodore 64.

Typ: Function 
General Programming-Syntax: FRE(<"Dummy" numeric variable>)

FRE accepts any numerical argument within the limits of the floating point format, 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.

[edit] ROM details

FRE first performs a garbage collection to free up unused string memory. Then it calculates the "gap" in RAM between the top of array memory (as indicated by the pointer at 49–50/$31–32) and the bottom of string memory (as per the pointer 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 seeming 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 is 32 kilobytes or more, FRE will return a negative number. To correct for this, add 65536 to the result if it comes out negative.

If the numeric argument is missing the BASIC error ?SYNTAX ERROR IN line appears.

[edit] Examples

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

This example creates a user-defined function that fixes the "negative" results, and then uses it to report the well over 32 kilobytes of free memory:

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-Commands of Commodore 64

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

 
Personal tools
Help and Feedback
In other languages