-
Notifications
You must be signed in to change notification settings - Fork 14
/
eval.asm
1525 lines (1525 loc) · 21.8 KB
/
eval.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; TITLE BBC BASIC (C) R.T.RUSSELL 1987
;
;BBC BASIC INTERPRETER - Z80 VERSION
;EXPRESSION EVALUATION & ARITHMETIC MODULE - "EVAL"
;(C) COPYRIGHT R.T.RUSSELL 1984
;VERSION 2.3, 07-05-1984
;Modified to use external FPP, 01-03-1987
;VERSION 3.0, 08-03-1987
;INSTR bug fixed, 30-09-1992
;
;BINARY FLOATING POINT REPRESENTATION:
; 32 BIT SIGN-MAGNITUDE NORMALIZED MANTISSA
; 8 BIT EXCESS-128 SIGNED EXPONENT
; SIGN BIT REPLACES MANTISSA MSB (IMPLIED "1")
; MANTISSA=0 & EXPONENT=0 IMPLIES VALUE IS ZERO.
;
;BINARY INTEGER REPRESENTATION:
; 32 BIT 2'S-COMPLEMENT SIGNED INTEGER
; "EXPONENT" BYTE = 0 (WHEN PRESENT)
;
;NORMAL REGISTER ALLOCATION: MANTISSA - HLH'L'
; EXPONENT - C
;
PUBLIC EXPR
PUBLIC EXPRN
PUBLIC EXPRI
PUBLIC EXPRS
PUBLIC ITEMI
PUBLIC CONS
PUBLIC LOADS
PUBLIC VAL0
PUBLIC SFIX
PUBLIC STR
PUBLIC HEXSTR
PUBLIC LOAD4
PUBLIC LOADN
PUBLIC DLOAD5
PUBLIC NXT
PUBLIC SWAP
PUBLIC TEST
PUBLIC ZERO
PUBLIC COMMA
PUBLIC BRAKET
PUBLIC DECODE
PUBLIC PUSHS
PUBLIC POPS
PUBLIC SEARCH
;
EXTERN ERROR
EXTERN SYNTAX
EXTERN CHANEL
EXTERN STORE4
EXTERN STORE5
EXTERN CHECK
EXTERN USR
EXTERN FN
;
EXTERN GETVAR
EXTERN LEXAN2
EXTERN RANGE
;
EXTERN STAVAR
EXTERN PAGE
EXTERN TOP
EXTERN LOMEM
EXTERN HIMEM
EXTERN RANDOM
EXTERN COUNT
EXTERN LISTON
EXTERN PC
EXTERN ERL
EXTERN ERR
EXTERN ACCS
;
EXTERN OSRDCH
EXTERN OSOPEN
EXTERN OSBGET
EXTERN OSSTAT
EXTERN GETCSR
EXTERN GETIME
EXTERN GETIMS
EXTERN GETEXT
EXTERN GETPTR
EXTERN OSKEY
;
EXTERN POINT
EXTERN ADVAL
;
EXTERN FPP
;
;TABLE OF ADDRESSES FOR FUNCTIONS:
;
FUNTOK: EQU 8DH ;1st FUNCTION TOKEN
;
FUNTBL: DEFW DECODE ;Line number
DEFW OPENIN ;OPENIN
DEFW PTR ;PTR
DEFW PAGEV ;PAGE
DEFW TIMEV ;TIME
DEFW LOMEMV ;LOMEM
DEFW HIMEMV ;HIMEM
DEFW ABS ;ABS
DEFW ACS ;ACS
DEFW ADVAL ;ADVAL
DEFW ASC ;ASC
DEFW ASN ;ASN
DEFW ATN ;ATN
DEFW BGET ;BGET
DEFW COS ;COS
DEFW COUNTV ;COUNT
DEFW DEG ;DEG
DEFW ERLV ;ERL
DEFW ERRV ;ERR
DEFW EVAL ;EVAL
DEFW EXP ;EXP
DEFW EXT ;EXT
DEFW ZERO ;FALSE
DEFW FN ;FN
DEFW GET ;GET
DEFW INKEY ;INKEY
DEFW INSTR ;INSTR(
DEFW INT ;INT
DEFW LEN ;LEN
DEFW LN ;LN
DEFW LOG ;LOG
DEFW NOT ;NOT
DEFW OPENUP ;OPENUP
DEFW OPENOT ;OPENOUT
DEFW PI ;PI
DEFW POINT ;POINT(
DEFW POS ;POS
DEFW RAD ;RAD
DEFW RND ;RND
DEFW SGN ;SGN
DEFW SIN ;SIN
DEFW SQR ;SQR
DEFW TAN ;TAN
DEFW TOPV ;TO(P)
DEFW TRUE ;TRUE
DEFW USR ;USR
DEFW VAL ;VAL
DEFW VPOS ;VPOS
DEFW CHRS ;CHR$
DEFW GETS ;GET$
DEFW INKEYS ;INKEY$
DEFW LEFTS ;LEFT$(
DEFW MIDS ;MID$(
DEFW RIGHTS ;RIGHT$(
DEFW STRS ;STR$
DEFW STRING ;STRING$(
DEFW EOF ;EOF
;
TCMD: EQU FUNTOK+($-FUNTBL)/2
;
AND: EQU 80H
DIV: EQU 81H
EOR: EQU 82H
MOD: EQU 83H
OR: EQU 84H
;
SOPTBL: DEFW SLE ;<= (STRING)
DEFW SNE ;<>
DEFW SGE ;>=
DEFW SLT ;<
DEFW SEQ ;=
DEFW SGT ;>
;
;EXPR - VARIABLE-TYPE EXPRESSION EVALUATION
; Expression type is returned in A'F':
; Numeric - A' bit 7=0, F' sign bit cleared.
; String - A' bit 7=1, F' sign bit set.
;Floating-point or integer result returned in HLH'L'C
; Integer result denoted by C=0 and HLH'L' non-zero.
;String result returned in string accumulator, DE set.
;
;Hierarchy is: (1) Variables, functions,
; constants, bracketed expressions.
; (2) ^
; (3) * / MOD DIV
; (4) + -
; (5) = <> <= >= > <
; (6) AND
; (7) EOR OR
;
EXPR: CALL EXPR1 ;GET FIRST OPERAND
EXPR0A: CP EOR ;CHECK OPERATOR
JR Z,EXPR0B
CP OR
RET NZ
EXPR0B: CALL SAVE ;SAVE FIRST OPERAND
CALL EXPR1 ;GET SECOND OPERAND
CALL DOIT ;DO OPERATION
JR EXPR0A ;CONTINUE
;
EXPR1: CALL EXPR2
EXPR1A: CP AND
RET NZ
CALL SAVE
CALL EXPR2
CALL DOIT
JR EXPR1A
;
EXPR2: CALL EXPR3
CALL RELOPQ
RET NZ
LD B,A
INC IY ;BUMP OVER OPERATOR
CALL NXT
CALL RELOPQ ;COMPOUND OPERATOR?
JR NZ,EXPR2B
INC IY
CP B
JP Z,SYNTAX ;ILLEGAL COMBINATION
ADD A,B
LD B,A
EXPR2B: LD A,B
EX AF,AF'
JP M,EXPR2S
EX AF,AF'
SUB 4
CP '>'-4
JR NZ,EXPR2C
ADD A,2
EXPR2C: CALL SAVE1
CALL EXPR3
CALL DOIT ;Must NOT be "JP DOIT"
RET
;
EXPR2S: EX AF,AF'
DEC A
AND 7
CALL PUSHS ;SAVE STRING ON STACK
PUSH AF ;SAVE OPERATOR
CALL EXPR3 ;SECOND STRING
EX AF,AF'
JP P,TYPE
POP AF
LD C,E ;LENGTH OF STRING #2
POP DE
LD HL,0
ADD HL,SP
LD B,E ;LENGTH OF STRING #1
PUSH DE
LD DE,ACCS
EX DE,HL
CALL DISPT2
POP DE
EX DE,HL
LD H,0
ADD HL,SP
LD SP,HL
EX DE,HL
XOR A ;NUMERIC MARKER
LD C,A ;INTEGER MARKER
EX AF,AF'
LD A,(IY)
RET
;
EXPR3: CALL EXPR4
EXPR3A: CP '-'
JR Z,EXPR3B
CP '+'
RET NZ
EX AF,AF'
JP M,EXPR3S
EX AF,AF'
EXPR3B: CALL SAVE
CALL EXPR4
CALL DOIT
JR EXPR3A
;
EXPR3S: EX AF,AF'
INC IY ;BUMP PAST '+'
CALL PUSHS ;SAVE STRING ON STACK
CALL EXPR4 ;SECOND STRING
EX AF,AF'
JP P,TYPE
LD C,E ;C=LENGTH
POP DE
PUSH DE
LD HL,ACCS
LD D,H
LD A,C
OR A
JR Z,EXP3S3
LD B,L
LD L,A ;SOURCE
ADD A,E
LD E,A ;DESTINATION
LD A,19
JP C,ERROR ;"String too long"
PUSH DE
DEC E
DEC L
LDDR ;COPY
POP DE
EXP3S3: EXX
POP BC
CALL POPS ;RESTORE FROM STACK
EXX
OR 80H ;FLAG STRING
EX AF,AF'
LD A,(IY)
JR EXPR3A
;
EXPR4: CALL EXPR5
EXPR4A: CP '*'
JR Z,EXPR4B
CP '/'
JR Z,EXPR4B
CP MOD
JR Z,EXPR4B
CP DIV
RET NZ
EXPR4B: CALL SAVE
CALL EXPR5
CALL DOIT
JR EXPR4A
;
EXPR5: CALL ITEM
OR A ;TEST TYPE
EX AF,AF' ;SAVE TYPE
EXPR5A: CALL NXT
CP '^'
RET NZ
CALL SAVE
CALL ITEM
OR A
EX AF,AF'
CALL DOIT
JR EXPR5A
;
EXPRN: CALL EXPR
EX AF,AF'
RET P
JR TYPE
;
EXPRI: CALL EXPR
EX AF,AF'
JP P,SFIX
JR TYPE
;
EXPRS: CALL EXPR
EX AF,AF'
RET M
JR TYPE
;
;
ITEMN: CALL ITEM
OR A
RET P
JR TYPE
;
ITEMI: CALL ITEM
OR A
JP P,SFIX
JR TYPE
;
ITEMS: CALL ITEM
OR A
RET M
TYPE: LD A,6
JP ERROR ;"Type mismatch"
;
ITEM1: CALL EXPR ;BRACKETED EXPR
CALL BRAKET
EX AF,AF'
RET
;
;HEX - Get hexadecimal constant.
; Inputs: ASCII string at (IY)
; Outputs: Integer result in H'L'HL, C=0, A7=0.
; IY updated (points to delimiter)
;
HEX: CALL ZERO
CALL HEXDIG
JR C,BADHEX
HEX1: INC IY
AND 0FH
LD B,4
HEX2: EXX
ADD HL,HL
EXX
ADC HL,HL
DJNZ HEX2
EXX
OR L
LD L,A
EXX
CALL HEXDIG
JR NC,HEX1
XOR A
RET
;
BADHEX: LD A,28
JP ERROR ;"Bad HEX"
;
;MINUS - Unary minus.
; Inputs: IY = text pointer
; Outputs: Numeric result, same type as argument.
; Result in H'L'HLC
;
MINUS: CALL ITEMN
MINUS0: DEC C
INC C
JR Z,NEGATE ;ZERO/INTEGER
LD A,H
XOR 80H ;CHANGE SIGN (FP)
LD H,A
XOR A ;NUMERIC MARKER
RET
;
NEGATE: EXX
LD A,H
CPL
LD H,A
LD A,L
CPL
LD L,A
EXX
LD A,H
CPL
LD H,A
LD A,L
CPL
LD L,A
ADD1: EXX
INC HL
LD A,H
OR L
EXX
LD A,0 ;NUMERIC MARKER
RET NZ
INC HL
RET
;
;ITEM - VARIABLE TYPE NUMERIC OR STRING ITEM.
;Item type is returned in A: Bit 7=0 numeric.
; Bit 7=1 string.
;Numeric item returned in HLH'L'C.
;String item returned in string accumulator,
; DE addresses byte after last (E=length).
;
ITEM: CALL CHECK
CALL NXT
INC IY
CP '&'
JR Z,HEX ;HEX CONSTANT
CP '-'
JR Z,MINUS ;UNARY MINUS
CP '+'
JR Z,ITEMN ;UNARY PLUS
CP '('
JR Z,ITEM1 ;EXPRESSION
CP '"'
JR Z,CONS ;STRING CONSTANT
CP TCMD
JP NC,SYNTAX ;SYNTAX ERROR
CP FUNTOK
JP NC,DISPAT ;FUNCTION
DEC IY
CP ':'
JR NC,ITEM2 ;VARIABLE?
CP '0'
JR NC,CON ;NUMERIC CONSTANT
CP '.'
JR Z,CON ;NUMERIC CONSTANT
ITEM2: CALL GETVAR ;VARIABLE
JR NZ,NOSUCH
OR A
JP M,LOADS ;STRING VARIABLE
LOADN: OR A
JR Z,LOAD1 ;BYTE VARIABLE
LD C,0
BIT 0,A
JR Z,LOAD4 ;INTEGER VARIABLE
LOAD5: LD C,(IX+4)
LOAD4: EXX
LD L,(IX+0)
LD H,(IX+1)
EXX
LD L,(IX+2)
LD H,(IX+3)
RET
;
LOAD1: LD HL,0
EXX
LD H,0
LD L,(IX+0)
EXX
LD C,H
RET
;
NOSUCH: JP C,SYNTAX
LD A,(LISTON)
BIT 5,A
LD A,26
JR NZ,ERROR0 ;"No such variable"
NOS1: INC IY
CALL RANGE
JR NC,NOS1
LD IX,PC
XOR A
LD C,A
JR LOAD4
;
;CON$ - Get string constant from ASCII string.
; Inputs: ASCII string at (IY)
; Outputs: Result in string accumulator.
; D = MS byte of ACC$, E = string length
; A7 = 1 (string marker)
; IY updated
;
CONS: LD DE,ACCS
CONS3: LD A,(IY)
INC IY
CP '"'
JR Z,CONS2
CONS1: LD (DE),A
INC E
CP CR
JR NZ,CONS3
LD A,9
ERROR0: JP ERROR ;"Missing """
;
CONS2: LD A,(IY)
CP '"'
INC IY
JR Z,CONS1
DEC IY
LD A,80H ;STRING MARKER
RET
;
;CON - Get unsigned numeric constant from ASCII string.
; Inputs: ASCII string at (IY).
; Outputs: Variable-type result in HLH'L'C
; IY updated (points to delimiter)
; A7 = 0 (numeric marker)
;
CON: PUSH IY
POP IX
LD A,36
CALL FPP
JR C,ERROR0
PUSH IX
POP IY
XOR A
RET
;
DLOAD5: LD B,(IX+4)
EXX
LD E,(IX+0)
LD D,(IX+1)
EXX
LD E,(IX+2)
LD D,(IX+3)
RET
;
LOADS: LD DE,ACCS
RRA
JR NC,LOADS2 ;FIXED STRING
CALL LOAD4
EXX
LD A,L
EXX
OR A
LD C,A
LD A,80H ;STRING MARKER
RET Z
LD B,0
LDIR
RET
LOADS2: LD A,(HL)
LD (DE),A
INC HL
CP CR
LD A,80H ;STRING MARKER
RET Z
INC E
JR NZ,LOADS2
RET ;RETURN NULL STRING
;
;VARIABLE-TYPE FUNCTIONS:
;
;Result returned in HLH'L'C (floating point)
;Result returned in HLH'L' (C=0) (integer)
;Result returned in string accumulator & DE (string)
;All registers destroyed.
;IY (text pointer) updated.
;Bit 7 of A indicates type: 0 = numeric, 1 = string.
;
;
;POS - horizontal cursor position.
;VPOS - vertical cursor position.
;EOF - return status of file.
;BGET - read byte from file.
;INKEY - as GET but wait only n centiseconds.
;GET - wait for keypress and return ASCII value.
;GET(n) - input from Z80 port n.
;ASC - ASCII value of string.
;LEN - length of string.
;LOMEM - location of dynamic variables.
;HIMEM - top of available RAM.
;PAGE - start of current text page.
;TOP - address of first free byte after program.
;ERL - line number where last error occurred.
;ERR - number of last error.
;COUNT - number of printing characters since CR.
;Results are integer numeric.
;
POS: CALL GETCSR
EX DE,HL
JR COUNT1
VPOS: CALL GETCSR
JR COUNT1
EOF: CALL CHANEL
CALL OSSTAT
JP Z,TRUE
JP ZERO
BGET: CALL CHANEL ;CHANNEL NUMBER
CALL OSBGET
LD L,A
JR COUNT0
INKEY: CALL INKEYS
JR ASC0
GET: CALL NXT
CP '('
JR NZ,GET0
CALL ITEMI ;PORT ADDRESS
EXX
LD B,H
LD C,L
IN L,(C) ;INPUT FROM PORT BC
JR COUNT0
GET0: CALL GETS
JR ASC1
ASC: CALL ITEMS
ASC0: XOR A
CP E
JP Z,TRUE ;NULL STRING
ASC1: LD HL,(ACCS)
JR COUNT0
LEN: CALL ITEMS
EX DE,HL
JR COUNT0
LOMEMV: LD HL,(LOMEM)
JR COUNT1
HIMEMV: LD HL,(HIMEM)
JR COUNT1
PAGEV: LD HL,(PAGE)
JR COUNT1
TOPV: LD A,(IY)
INC IY ;SKIP "P"
CP 'P'
JP NZ,SYNTAX ;"Syntax Error"
LD HL,(TOP)
JR COUNT1
ERLV: LD HL,(ERL)
JR COUNT1
ERRV: LD HL,(ERR)
JR COUNT0
COUNTV: LD HL,(COUNT)
COUNT0: LD H,0
COUNT1: EXX
XOR A
LD C,A ;INTEGER MARKER
LD H,A
LD L,A
RET
;
;OPENIN - Open a file for reading.
;OPENOUT - Open a file for writing.
;OPENUP - Open a file for reading or writing.
;Result is integer channel number (0 if error)
;
OPENOT: XOR A
DEFB 21H ;SKIP NEXT 2 BYTES
OPENUP: LD A,2
DEFB 21H ;SKIP NEXT 2 BYTES
OPENIN: LD A,1
PUSH AF ;SAVE OPEN TYPE
CALL ITEMS ;FILENAME
LD A,CR
LD (DE),A
POP AF ;RESTORE OPEN TYPE
ADD A,-1 ;AFFECT FLAGS
LD HL,ACCS
CALL OSOPEN
LD L,A
JR COUNT0
;
;EXT - Return length of file.
;PTR - Return current file pointer.
;Results are integer numeric.
;
EXT: CALL CHANEL
CALL GETEXT
JR TIME0
;
PTR: CALL CHANEL
CALL GETPTR
JR TIME0
;
;TIME - Return current value of elapsed time.
;Result is integer numeric.
;
TIMEV: LD A,(IY)
CP '$'
JR Z,TIMEVS
CALL GETIME
TIME0: PUSH DE
EXX
POP HL
XOR A
LD C,A
RET
;
;TIME$ - Return date/time string.
;Result is string
;
TIMEVS: INC IY ;SKIP $
CALL GETIMS
LD A,80H ;MARK STRING
RET
;
;String comparison:
;
SLT: CALL SCP
RET NC
JR TRUE
;
SGT: CALL SCP
RET Z
RET C
JR TRUE
;
SGE: CALL SCP
RET C
JR TRUE
;
SLE: CALL SCP
JR Z,TRUE
RET NC
JR TRUE
;
SNE: CALL SCP
RET Z
JR TRUE
;
SEQ: CALL SCP
RET NZ
TRUE: LD A,-1
EXX
LD H,A
LD L,A
EXX
LD H,A
LD L,A
INC A
LD C,A
RET
;
;PI - Return PI (3.141592654)
;Result is floating-point numeric.
;
PI: LD A,35
JR FPP1
;
;ABS - Absolute value
;Result is numeric, variable type.
;
ABS: LD A,16
JR FPPN
;
;NOT - Complement integer.
;Result is integer numeric.
;
NOT: LD A,26
JR FPPN
;
;DEG - Convert radians to degrees
;Result is floating-point numeric.
;
DEG: LD A,21
JR FPPN
;
;RAD - Convert degrees to radians
;Result is floating-point numeric.
;
RAD: LD A,27
JR FPPN
;
;SGN - Return -1, 0 or +1
;Result is integer numeric.
;
SGN: LD A,28
JR FPPN
;
;INT - Floor function
;Result is integer numeric.
;
INT: LD A,23
JR FPPN
;
;SQR - square root
;Result is floating-point numeric.
;
SQR: LD A,30
JR FPPN
;
;TAN - Tangent function
;Result is floating-point numeric.
;
TAN: LD A,31
JR FPPN
;
;COS - Cosine function
;Result is floating-point numeric.
;
COS: LD A,20
JR FPPN
;
;SIN - Sine function
;Result is floating-point numeric.
;
SIN: LD A,29
JR FPPN
;
;EXP - Exponential function
;Result is floating-point numeric.
;
EXP: LD A,22
JR FPPN
;
;LN - Natural log.
;Result is floating-point numeric.
;
LN: LD A,24
JR FPPN
;
;LOG - base-10 logarithm.
;Result is floating-point numeric.
;
LOG: LD A,25
JR FPPN
;
;ASN - Arc-sine
;Result is floating-point numeric.
;
ASN: LD A,18
JR FPPN
;
;ATN - arc-tangent
;Result is floating-point numeric.
;
ATN: LD A,19
JR FPPN
;
;ACS - arc-cosine
;Result is floating point numeric.
;
ACS: LD A,17
FPPN: PUSH AF
CALL ITEMN
POP AF
FPP1: CALL FPP
JP C,ERROR
XOR A
RET
;
;SFIX - Convert to fixed-point notation
;
SFIX: LD A,38
JR FPP1
;
;SFLOAT - Convert to floating-point notation
;
SFLOAT: LD A,39
JR FPP1
;
;VAL - Return numeric value of string.
;Result is variable type numeric.
;
VAL: CALL ITEMS
VAL0: XOR A
LD (DE),A
LD IX,ACCS
LD A,36
JR FPP1
;
;EVAL - Pass string to expression evaluator.
;Result is variable type (numeric or string).
;
EVAL: CALL ITEMS
LD A,CR
LD (DE),A
PUSH IY
LD DE,ACCS
LD IY,ACCS
LD C,0
CALL LEXAN2 ;TOKENISE
LD (DE),A
INC DE
XOR A
CALL PUSHS ;PUT ON STACK
LD IY,2
ADD IY,SP
CALL EXPR
POP IY
ADD IY,SP
LD SP,IY ;ADJUST STACK POINTER
POP IY
EX AF,AF'
RET
;
;RND - Random number function.
; RND gives random integer 0-&FFFFFFFF
; RND(-n) seeds random number & returns -n.
; RND(0) returns last value in RND(1) form.
; RND(1) returns floating-point 0-0.99999999.
; RND(n) returns random integer 1-n.
;
RND: LD IX,RANDOM
CALL NXT
CP '('
JR Z,RND5 ;ARGUMENT FOLLOWS
CALL LOAD5
RND1: RR C
LD B,32
RND2: EXX ;CALCULATE NEXT
ADC HL,HL
EXX
ADC HL,HL
BIT 3,L
JR Z,RND3
CCF
RND3: DJNZ RND2
RND4: RL C ;SAVE CARRY
CALL STORE5 ;STORE NEW NUMBER
XOR A
LD C,A
RET
RND5: CALL ITEMI
LD IX,RANDOM
BIT 7,H ;NEGATIVE?
SCF
JR NZ,RND4 ;SEED
CALL TEST
PUSH AF
CALL SWAP
EXX
CALL LOAD5
CALL NZ,RND1 ;NEXT IF NON-ZERO
EXX ;SCRAMBLE (CARE!)
LD C,7FH
RND6: BIT 7,H ;FLOAT
JR NZ,RND7
EXX
ADD HL,HL
EXX
ADC HL,HL
DEC C
JR NZ,RND6
RND7: RES 7,H ;POSITIVE 0-0.999999
POP AF
RET Z ;ZERO ARGUMENT
EXX
LD A,E
DEC A
OR D
EXX
OR E
OR D
RET Z ;ARGUMENT=1
LD B,0 ;INTEGER MARKER
LD A,10
CALL FPP ;MULTIPLY
JP C,ERROR
CALL SFIX
JP ADD1
;
;INSTR - String search.
;Result is integer numeric.
;
INSTR: CALL EXPRSC ;STRING TO SEARCH
CALL PUSHS ;SAVE STRING ON STACK
CALL EXPRS ;SUB-STRING
POP BC
LD HL,0
ADD HL,SP ;HL ADDRESSES MAIN
PUSH BC ;C = MAIN STRING LENGTH
LD B,E ;B = SUB-STRING LENGTH
CALL NXT
CP ','
LD A,0
JR NZ,INSTR1
INC IY ;SKIP COMMA
PUSH BC ;SAVE LENGTHS
PUSH HL ;SAVE MAIN ADDRESS
CALL PUSHS