;
;TYPE
;$STRING80=STRING 80;
;$STRING0 = STRING 0;
;$STRING255 = STRING 255;
;BYTE = 0..255;
;VAR
;DATA:$STRING255;
;START,STOP:INTEGER;
;
;
;FUNCTION LENGTH(X:$STRING255):INTEGER;EXTERNAL;
	EXTD	L168,LENGTH  
;PROCEDURE SETLENGTH(VAR X:$STRING0;Y:INTEGER);EXTERNAL;
	EXTD	L169,SETLENGT
;
;{function to extract characters from within a string, starting at
;position "y" and ending at position "z"..
;corresponds to BASIC command  MID$(A$,X,Y) } 
;
;FUNCTION MID(X:$STRING255; Y,Z:BYTE):$STRING255; 
;
;{y = starting position and z = the ending position in string x}
;
;LABEL 1;
;VAR
;LEN,I:INTEGER;
;MIDDLE:$STRING255;
;
;BEGIN
L170
	NAME MID
	ENTRY MID
MID:
	ENTR	D,2,260
;SETLENGTH(MIDDLE,0);
	STMT	D,1
	PUSH	IX
	POP	H
	LXI	B,-4
	DADD	B
	PUSH	H
	MOV	H,A
	MOV	L,A
	PUSH	H
	CALL	L169
;LEN:=LENGTH(X);
	STMT	D,2
	PUSH	IX
	POP	H
	LXI	B,265
	DADD	B
	SPSH	S,255
	CALL	L168
	STMT	M,2
	MOV	-2(IX),D
	MOV	-3(IX),E
;
;IF (Y < 1) OR (Y > Z) OR ( Y > LEN) OR (LEN = 0) OR (Z > 255) THEN GOTO 1; 
	STMT	D,3
	MOV	H,A
	MOV	L,9(IX)
	MOV	A,L
	CMPI	D,1
	MOV	A,H
	JC	L198
	MOV	H,A
	MOV	L,9(IX)
	MOV	D,A
	MOV	E,8(IX)
	GRET	D,0
	JC	L198
	MOV	H,A
	MOV	L,9(IX)
	MOV	E,-3(IX)
	MOV	D,-2(IX)
	GRET	D,0
	JC	L198
	MOV	L,-3(IX)
	MOV	H,-2(IX)
	MOV	D,A
	MOV	E,A
	DSB1	D,0
	JZ	L198
	MOV	H,A
	MOV	L,8(IX)
	MVI	A,255
	CMP	L
	MOV	A,H
	JNC	L197
L196
L198	EQU	L196
	STMT	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