8-Bit Software

The BBC and Master Computer Public Domain Library

Back to 8BS Or Back to Disassembly


Page Last Altered:

AD36 Evaluate Value/Variable/Open Bracket/BASIC Function

Submitted by Steve Fewell

Routine:evalValue
Name: Evaluate Value/Variable/Open Bracket/BASIC Function
Starting Address: &AD36
Entry criteria: &19, &1A & &1B (Text Pointer B) point to the program text location
at which we should begin our evaluation.
Exit: Any variable/value found will be loaded, or the function will have been executed,
Or the value inside the brackets ['(', ')'] will have been evaluated.

Description:

Load the ASCII value of the next character into the accumulator & increment the
Text pointer B offset (keeping a previous copy of it in Y). Skip any spaces in the text
by incrementing the Text Pointer B offset (past the space character) and loading the next character.

Now, we have our first non-space character in the accumulator.
If the character is '-', then we have a leading [unary] '-', so call &ACD7 to evaluate the
expression after the '-', and complement the result (or Type mismatch error if result is a string).
If the character is '"' [open quote], then call &AD19 to extract string into the SWA & exit.
If the character is '+', leading [unary] '+', then skip any spaces (after the '+') and load the next
non-space character after the '+'.

If the character is a BASIC Statement Keyword (non-function, i.e. >= 198 'AUTO') then
generate a 'No such variable' error as a BASIC Statement keyword can only occur
at the beginning of a line, and not in the middle of an expression!

If the character is >= 142 &8E (OPENIN) [& < 198 &C5], then this represents a BASIC Function Keyword,
so execute the portion of code (in the BASIC ROM) which deals with the appropriate function.
[9019] To do this, the character ASCII Code is multiplied by 2 & added to the base address
&874D to form a pointer to the execution address (LSB first, MSB next) of the required
function. This resulting address is jumped to.
Example 1: Character = &8E [OPENIN token] = 10001110 multiply by 2 = 00011100 (which is &1C in hex).
So, &874D + &1C = &8769 (The LSB of the execution address for the OPENIN function (&876A is
the MSB of the address)).
Example 2: Character = &C5 [EOF token] = 11000101 multiply by 2 = 10001010 (which is &8A in hex).
So, &874D + &8A = &87D7 (The LSB of the execution address for the EOF function (&87D8 is
the MSB of the address)).

BASIC Keywords between 128 and 141 are not considered, as these Keywords are used
in the middle of statements (and are not functions), so the statements/expression handler will
deal with these values. These keywords are as follows: AND, DIV, EOR, MOD, OR, ERROR,
LINE, OFF, STEP, SPC, TAB(, ELSE and THEN.

If the character is >&eq;"." and <"?" then call routine &A2E1 to evaluate the numeric
value at Text Pointer B. This routine will place the result either into the IWA or FWA (depending
on how large the value is, or whether it contains a fractional part).
If the carry flag is clear then the routine failed to convert the ASCII text to a numeric
value, so issue a 'No such variable' error. Otherwise exit.

If the character is "&" then call routine &ADB7 to extract the Hex Number and exit.
If the character is "(" then call routine &ADAC to get the result of the expression (inside
the brackets) & check for a closing bracket ')' at the end (issue a 'Missing )' error if not present) & exit.

If the character is anything else, then it must be a variable.
So, call routine &9909 to evaluate the variable name (or array reference) and
set (&2A, &2B) to the address of the variable's value (or the address of the value
of the specified Array element).
If &9909 returns with the zero flag clear, then the variable was evaluated sucessfully
so exit via routine &B1A0 to Load the variables value into the appropriate location (IWA/FWA/SWA).
Otherwise (zero flag was set), the variable either wasn't found, or a valid variable
name was not found, so check the value of &28, which is the OPT flag.
if Bit 1 of this flag (the second bit from the right) [Relocate is on?] is not set then issue a
'No such variable' error. If the carry flag (on exit from routine &9909) is set then
the text was not a valid variable name, so issue a 'No such variable' error.
Otherwise, (the second bit is set, meaning that OPT = Relocate?) Store back the Text pointer
B offset in &1B and set the IWA to the 2-byte address value from locations &0440-&0441
(&0440 is the LSB), this is the value of the address stored in variable P%, then exit.


Disassembly for the Evaluate Value/Variable/Open Bracket/BASIC Function routine

AD36   164 027 A4 1B LDY &1B
AD38   230 027 E6 1B INC &1B
AD3A   177 025 B1 19 LDA (&19),Y
AD3C   201 032 C9 20 CMP#&20
AD3E   240 246 F0 F6 BEQ -10 --> &AD36
AD40 - 201 045 C9 2D CMP#&2D
AD42   240 147 F0 93 BEQ -109 --> &ACD7 Complement result of expression
AD44 " 201 034 C9 22 CMP#&22
AD46   240 209 F0 D1 BEQ -47 --> &AD19 Extract String
AD48 + 201 043 C9 2B CMP#&2B
AD4A   208 003 D0 03 BNE 3 --> &AD4F
AD4C   032 213 142 20 D5 8E JSR &8ED5 Skip Spaces (Ptr B) & Get next character
AD4F   201 142 C9 8E CMP#&8E
AD51   144 007 90 07 BCC 7 --> &AD5A
AD53   201 198 C9 C6 CMP#&C6
AD55 5 176 053 B0 35 BCS 53 --> &AD8C No such variable error
AD57 L 076 025 144 4C 19 90 JMP &9019 Jump to BASIC Keyword evaluation routine
AD5A ? 201 063 C9 3F CMP#&3F
AD5C   176 012 B0 0C BCS 12 --> &AD6A
AD5E . 201 046 C9 2E CMP#&2E
AD60   176 018 B0 12 BCS 18 --> &AD74
AD62 & 201 038 C9 26 CMP#&26
AD64 Q 240 081 F0 51 BEQ 81 --> &ADB7 Extract Hex number
AD66 ( 201 040 C9 28 CMP#&28
AD68 B 240 066 F0 42 BEQ 66 --> &ADAC Evaluate expression & check for ')'
AD6A   198 027 C6 1B DEC &1B
AD6C   032 009 153 20 09 99 JSR &9909 Evaluate variable/array name & return the value's address
AD6F   240 009 F0 09 BEQ 9 --> &AD7A If variable wasn't found
AD71 L 076 160 177 4C A0 B1 JMP &B1A0 Load Variable's value
AD74   032 225 162 20 E1 A2 JSR &A2E1 ASCNUM: Extract ASCII number at PTRB to FWA/IWA
AD77   144 019 90 13 BCC 19 --> &AD8C No such variable error
AD79 ` 096 60 RTS
AD7A ( 165 040 A5 28 LDA &28
AD7C ) 041 002 29 02 AND#&02
AD7E   208 012 D0 0C BNE 12 --> &AD8C No such variable error
AD80   176 010 B0 0A BCS 10 --> &AD8C No such variable error
AD82   134 027 86 1B STX &1B
AD84 @ 173 064 004 AD 40 04 LDA &0440
AD87 A 172 065 004 AC 41 04 LDY &0441
AD8A k 128 107 80 6B BRA 107 --> &ADF7   [BRA &21 -> &AE1A (Load IWA with 2-byte value)]

 


 Back to 8BS
Or