I'm not conversant enough in Intel assembler to understand what's going on in the early MS interpreters, so maybe someone can help...

Generally in MS-derived BASIC an expression would only be semi-"chunked" and in the simple cases would pretty much leave the code in its original ASCII form. For instance, X+1 would appear that way in code.

Also, AFAIK, variable lookup occurs at runtime, so when it encounters X it looks through the variable table and returns/inserts it.

So how did it manage the "local" variables in DEF? For instance:


In this case, A is a "local", and B is a global and would be called like this:


So how did it substitute the 10 into the formula?


2 Answers 2


The substitution takes place by saving the current value of A on the hardware stack and assigning A the evaluated argument for the duration of the computation. Then the variable A is restored from the stack.

Some more detail. A function definition is stored in the variable table (with bit 7 of the first character of its name set to mark it as a function entry). The function definition in the variable table holds two pointers. The first one points to the start of the expression after the = sign of the DEF FN command. The second points to the storage of the variable that is named as the argument in the variable table.

For example, DEF FN X(Y) = Y+1 would create a function variable named X (with bit 7 set) which points to the Y+1 part in the program, and to the five bytes of storage for the variable Y. When a function is invoked as, say FN X(10), then

  1. The argument is evaluated (resulting in 10 in the FAC).
  2. The current value of the variable Y is pushed onto the hardware stack.
  3. The variable Y is assigned the evaluated argument in the FAC.
  4. The expression pointed to by the function X is evaluated (Y + 1).
  5. And finally the value of Y is restored from the hardware stack.
  • Huh, I never suspect they would use the stack for that. And hardware stack... using the 6502 stack page? Excellent work WimC, and welcome to retro. Dec 23, 2020 at 22:32
  • Yes, this is about the 6502 implementation.
    – WimC
    Dec 24, 2020 at 6:28

I tested this by creating two functions:

10 A=2
20 B=3
30 DEF FNX(A)=A+B+1
40 DEF FNY(A)=FNX(A)+10
50 PRINT FNY(100),A
114 2

Since the A variable is not clobbered, it is not creating a variable. Since you can have a function call a second function without the value of the first function being clobbered, I'd suspect the value is being placed on the stack until the each function exits. If there was a place in zero page where the value was being stored, then the second function would clobber the value of the first function.

  • Is it possible to recursively call a function? The abort condition might be constructed by a shortcut AND or OR. Dec 24, 2020 at 12:59
  • 1
    @thebusybee Both arguments of binary operators are computed first, then the result is computed. In particular, AND and OR do not short-circuit in MS BASIC.
    – WimC
    Dec 24, 2020 at 17:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .