×   Main Menu ALL The 8BS News Manuals (New menu) Links Worth a Look Tools Disc and Basic Webring Site Map 8BS Guestbook Old Guest Book Me The Barnsley Rovers   
8-Bit Software

The BBC and Master Computer Public Domain Library

AEC5 MID$(

Submitted by Steve Fewell

Description:

Call routine &9D3B to evaluate the expression at BASIC text pointer B and issue a 'Type mismatch' error if a String
value was not returned.
If the next non-space character (stored in X) is not a comma then issue 'Missing ,' error; otherwise, increment BASIC text
pointer B offset to skip past the comma value and push the SWA value to the BASIC stack.

Get the Integer result of the expression at BASIC Text pointer B ('Type mismatch' error if value is not numeric or convert
to Integer if Floating-Point value).
Store &2A (the LSB of the Integer value - i.e. the starting point within the SWA value) on the 6502 processor Stack.

Set &2A to value #&FF (i.e. default value for Number of characters is #&FF, or the maximum number of characters
(which will return all characters from the Starting point to the end of SWA is reached)).

Increment BASIC text pointer B offset to skip past the next character (which should be either a comma or closing parenthesis).
If the next character (stored in X) was neither a Comma or closing parenthesis, then issue 'Missing ,' error.
If the next character (stored in X) was a Comma (','), then [&96A7] Extract the Integer result (after the comma) and
check for a closing parenthesis character (')'). If close bracket was not found then issue 'Syntax error'.

Retrieve the SWA String value from the Stack.
Retrieve the Integer Starting Position from the 6502 Stack and store the value in Y.

If Y is not zero (i.e. not starting at the beginning of the String Value) then:
* Subtract the SWA length (&36) + 1 from the Starting position.
* If the result did not underflow (SWA length is less than the Starting position), so exit with SWA set to a null string
   (length 0) and exit.
* Decrement Y (The Starting Position value)
Now Y contains the Starting Position - 1, or 0 is we are starting at the first character of the String value. This is
needed because the first SWA character starts at 0, whereas the first String character in BASIC starts at 1.
Store the (Starting Position - 1, i.e. the Y value before the above calculation) in X register and location &2C.
Set Y to 0.
Subtract &2C (Starting Position-1) from the SWA length (&36) to obtain the number of characters from the Starting
position to the end of the SWA value.
If the result is less than &2A (number of characters to take) then set &2A (number of characters to take) to A (the
number of characters in the SWA between the Starting point and the end of the SWA value).

If &2A (Number of characters to take) is zero then set the SWA value to a null string (length 0) and exit.
Otherwise, Keep copying the character at SWA location X (Starting position) to SWA location Y (start of SWA value, initially 0)
and then incrementing X and Y until Y is equal to &2A (number of characters to copy).
Set the SWA length (&36) to Y and exit with A = 0 (to indicate that the result is a String value).


Disassembly for the MID$( routine

AEBF L 076 146 144 4C 92 90 JMP &9092 Type mismatch error
AEC2 L 076 246 142 4C F6 8E JMP &8EF6 'Missing ,' error
AEC5 ; 032 059 157 20 3B 9D JSR &9D3B Evaluate expression at BASIC Text pointer B
AEC8   208 245 D0 F5 BNE -11 --> &AEBF [JMP &9092 Type mismatch error]
AECA , 224 044 E0 2C CPX#&2C
AECC   208 244 D0 F4 BNE -12 --> &AEC2 [JMP &8EF6 'Missing ,' error]
AECE Q 032 081 188 20 51 BC JSR &BC51 Push SWA to Stack
AED1   230 027 E6 1B INC &1B
AED3   032 175 150 20 AF 96 JSR &96AF Get expression result & convert it to Integer
AED6 * 165 042 A5 2A LDA &2A
AED8 H 072 48 PHA
AED9   169 255 A9 FF LDA#&FF
AEDB * 133 042 85 2A STA &2A
AEDD   230 027 E6 1B INC &1B
AEDF ) 224 041 E0 29 CPX#&29
AEE1   240 007 F0 07 BEQ 7 --> &AEEA
AEE3 , 224 044 E0 2C CPX#&2C
AEE5   208 219 D0 DB BNE -37 --> &AEC2 [JMP &8EF6 'Missing ,' error]
AEE7   032 167 150 20 A7 96 JSR &96A7 Extract Integer result of expression & check for ')'
AEEA   032 210 188 20 D2 BC JSR &BCD2 Pop String (SWA) from the stack
AEED h 104 68 PLA
AEEE   168 A8 TAY
AEEF   024 18 CLC
AEF0   240 006 F0 06 BEQ 6 --> &AEF8
AEF2 6 229 054 E5 36 SBC &36
AEF4   176 197 B0 C5 BCS -59 --> &AEBB Exit with SWA length (&36) = 0 and A = 0
AEF6   136 88 DEY
AEF7   152 98 TYA
AEF8 , 133 044 85 2C STA &2C
AEFA   170 AA TAX
AEFB   160 000 A0 00 LDY#&00
AEFD 6 165 054 A5 36 LDA &36
AEFF 8 056 38 SEC
AF00 , 229 044 E5 2C SBC &2C
AF02 * 197 042 C5 2A CMP &2A
AF04   176 002 B0 02 BCS 2 --> &AF08
AF06 * 133 042 85 2A STA &2A
AF08 * 165 042 A5 2A LDA &2A
AF0A   240 175 F0 AF BEQ -81 --> &AEBB Exit with SWA length (&36) = 0 and A = 0
AF0C   189 000 006 BD 00 06 LDA &0600,X
AF0F   153 000 006 99 00 06 STA &0600,Y
AF12   200 C8 INY
AF13   232 E8 INX
AF14 * 196 042 C4 2A CPY &2A
AF16   208 244 D0 F4 BNE -12 --> &AF0C
AF18 6 132 054 84 36 STY &36
AF1A ^ 128 094 80 5E BRA 94 --> &AF7A   Exit with A = 0

 


 Back to 8BS
Or