-
Notifications
You must be signed in to change notification settings - Fork 3
/
eval.mac
1321 lines (1308 loc) · 27.6 KB
/
eval.mac
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
;**********************************************************************;
; ;
; This file is part of ZSM4, a Z80/Z180/Z280 relocatable macro- ;
; assembler written in Z80 assembly. ;
; Copyright (C) 2017-2020, Hector Peraza. ;
; ;
; This work is derived from Z80ASM, originally written by Michael ;
; G. Lehman (1977) and with modifications by Ray Halls (1992) and ;
; Neil Harrison (1983). ;
; ;
; This program is free software; you can redistribute it and/or ;
; modify it under the terms of the GNU General Public License as ;
; published by the Free Software Foundation; either version 2 of ;
; the License, or (at your option) any later version. ;
; ;
; This program is distributed in the hope that it will be useful, ;
; but WITHOUT ANY WARRANTY; without even the implied warranty of ;
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;
; GNU General Public License for more details. ;
; ;
; You should have received a copy of the GNU General Public License ;
; along with this program; if not, write to the Free Software ;
; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. ;
; ;
;**********************************************************************;
TITLE Z80/Z180/Z280 Macro-Assembler
SUBTTL Expression Evaluation
.Z80
include ZSM.INC
CSEG
public EVAL,EVALNEW,EVALREG,EVALSRG,EVALCND,EVBRKT
public REGVAL,CCONST,SYMPT,RELERR,INT,INTBUF
extrn SYMTBL,IDBUF,IDLEN,VALID1,OERROR,ADDEXT
extrn ERRFLG,PCFLAG,EVFLGS,PC,CURSEG,CURCMN
extrn CPU,SYMADR,BACKUP,GNC,UFLAG,PTR1,VAL,ID
extrn SYMLUK,UCASE,LEN,DBWFLG,EVMODE,EXTCHN
extrn RADIX,GETPPC,CURCMN,CMNPTR,CMPHD,CHK8U
extrn VALERR,REQCHR,UMODE,SYMMOD,ADDSYM
;
; Evaluate expression in () or <> brackets, assumes the opening
; bracket has been already scanned, and is passed to the routine
; in reg A.
;
; Returns a code in regs A and C indicating the type of operand:
;
; Code Operand Notes
; ---- ------- -----
; 0 (addr) address stored in variable VAL,
; mode in EVMODE
; 1 (r) r can only be reg C
; 2 (rp) rp can be BC, DE, HL or SP
; register value stored in REGVAL
; 3 (rp+d8) rp is either IX or IY
; displacement stored in VAL,
; mode in EVMODE
; * in Z80 and Z180 modes a 'V' error is
; automatically generated if displacement
; is not a signed 8-bit value.
; * in Z280 mode code 4 is returned if
; displacement is a 16-bit quantity.
; 4 (rp+d16) Z280 mode only, rp can be HL, IX, IY or SP
; displacement stored in VAL,
; mode stored in EVMODE
; 5 (x+y) Z280 mode only, returns B = 0 for (HL+IX),
; 1 for (HL+IY) or 2 for (IX+IY)
; 6 (PC+d16) Z280 mode only
; displacement stored in VAL,
; mode in EVMODE
; 7 <addr> Z280 mode only
; address stored in VAL, mode in EVMODE
;
EVBRKT: cp '<'
jr z,EVREL
call EVALSRG ; include special regs in search
ld (REGVAL),hl
cp RPNAME
jr z,EVRP ; branch if rpair
cp RNAME
jr z,EVSG ; branch if single reg
cp RSNAME
jr z,EVRS ; branch if special reg (Z280)
ld c,0 ; code 0 = (addr), value already in VAL
EVRET: call REQCHR
db ')'
ld a,c
ret
;
EVREL: ld c,7 ; code 7 = <addr>
ld a,(CPU)
cp 2
jr nz,EVERR ; Z280 only
push bc
call EVALNEW ; get address
pop bc
call REQCHR
db '>'
ld a,c
ret
;
EVERR: call OERROR ; set error flag
ld a,c
ret
;
EVSG: ld a,l
dec a
ld c,1 ; code 1 = (C)
jr nz,EVERR ; single register can only be C
jr EVRET
;
EVRS: ld bc,EVRET
push bc ; push return address
ld a,l
cp 3
jr nz,EVERR ; special register can only be PC
ld c,6 ; code 6 = (PC+d16)
ld a,(CPU)
cp 2
jr nz,EVERR ; Z280 only
push bc
call EVALNEW ; get index
pop bc
ret
;
EVRP: ld bc,EVRET
push bc ; push return address
ld a,l
cp 4
jr nz,EVRP2 ; branch if not HL/IX/IY
ld a,(CPU)
cp 2 ; Z280 mode?
jr nz,EVRP2 ; skip if not
ld hl,(PTR1) ; else check for X+Y modes
push hl ; save record pointer
call GNC
cp '+'
jr nz,EVRP1 ; branch if (HL), (IX) or (IY)
call EVALREG ; else get second register
cp RPNAME
jr nz,EVRP1 ; branch if not (x+y)
ld a,l
cp 4 ; must be HL, IX or IY
jr nz,EVRP1
ld c,5 ; code 5 = (x+y)
ld a,(REGVAL+1)
xor h
pop hl
cp 0DDh ; HL+IX
ld b,1
ret z
inc b
cp 0FDh ; HL+IY
ret z
inc b ; else is IX+IY
ret
;
EVRP1: pop hl
ld (PTR1),hl ; restore record pointer
EVRP2: ld hl,0
ld (VAL),hl ; default index is 0
xor a
ld (EVMODE),a
ld c,2 ; code 2 = (rp)
ld hl,(REGVAL)
ld a,l
or a
ret z ; return if (BC)
cp 2
ret z ; return if (DE)
ld a,h
or a
jr nz,EVRP3 ; skip next test if (IX) or (IY)
call GNC
cp ')'
pop de
ld a,c
ret z ; return if no expression follows
push de
call BACKUP
ld a,(CPU)
cp 2 ; neither (SP) nor (HL) can have index
ret nz ; in Z80/Z180 mode
EVRP3: call EVALNEW ; get index
ld c,4 ; code 4 = (rp+d16)
ld de,(REGVAL)
ld a,e
cp 6
ret z ; SP requires 16-bit index
ld a,d
or a
jr nz,EVRP4 ; index is always returned for index register
ld a,(EVMODE)
or a
ld c,4 ; code 4 = (rp+d16)
ret nz
ld a,h
or l
ret nz
ld c,2 ; code 2 = (rp)
ret ; return code 2 if HL displacement is zero
EVRP4: ld c,3 ; code 3 = (rp+d8)
ld a,(EVMODE)
ld b,a
call CHK8U ; CHK8S? depends... I've seen code with
ret z ; (IX+0FFh) in place of (IX-1)
inc c ; code 4 = (rp+d16)
ld a,(CPU)
cp 2 ; Z280 only
ret z
dec c ; back to code 3 = (rp+d8)
ld a,b
or a
jp nz,RELERR
jp VALERR ; no 16-bit IX/IY index in Z80/Z180 mode
;
; Evaluate a register expression. If no valid register
; is found, evaluate the expression as usual.
;
EVALREG:ld de,REGS ; register names
EVAL0: call GNC ; skip blanks and get next char
push af
call BACKUP ; backup to start of word
pop af
call VALID1
jp c,EVALNEW
xor a
ld (PCFLAG),a ; reset PC relative flag
ld (EVFLGS),a ; reset flags from last search
ld (EVMODE),a
ld hl,0
ld (VAL),hl ; set val to 0
ld hl,(PTR1)
push hl
call ID ; get name
ld c,2
call SYMLUK ; lookup in table
ld a,(hl) ; fetch value
inc hl
ld h,(hl)
ld l,a
ld (INTBUF),hl
ld (VAL),hl
pop bc
ld a,(EVFLGS)
ret nc ; return if found with HL=(VAL) and A=(EVFLGS)
ld (PTR1),bc
jp EVALNEW
;
; Like EVALREG, but also searches for special register names.
;
EVALSRG:ld a,(CPU)
ld de,SREGS
cp 2
jr z,EVAL0 ; full table for Z280 special registers
ld de,SRGZ80
jr EVAL0 ; reduced for Z80/Z180
;
SREGS: db 2+RSNAME,'PC',3,0
db 4+RSNAME,'DEHL',1,0
db 3+RSNAME,'USP',2,0
SRGZ80: db 2+RSNAME,'AF',6,0
REGS: db 1+RNAME,'A',7,0
db 1+RNAME,'B',0,0
db 1+RNAME,'C',1,0
db 1+RNAME,'D',2,0
db 1+RNAME,'E',3,0
db 1+RNAME,'H',4,0
db 1+RNAME,'L',5,0
db 3+RNAME,'IXH',4,0DDh
db 3+RNAME,'IXL',5,0DDh
db 3+RNAME,'IYH',4,0FDh
db 3+RNAME,'IYL',5,0FDh
db 2+RPNAME,'HL',4,0
db 2+RPNAME,'BC',0,0
db 2+RPNAME,'DE',2,0
db 2+RPNAME,'SP',6,0
db 2+RPNAME,'IX',4,0DDh
db 2+RPNAME,'IY',4,0FDh
db 0
;
; Evaluate a jump/call/return conditional expression.
; If no conditional is found, evaluate a label expression.
;
EVALCND:ld de,CONDS ; conditionals table
jp EVAL0
;
CONDS: db 1+COND,'Z',1,0
db 2+COND,'NZ',0,0
db 1+COND,'C',3,0
db 2+COND,'NC',2,0
db 2+COND,'PO',4,0
db 2+COND,'PE',5,0
db 2+COND,'NV',4,0
db 1+COND,'V',5,0
db 1+COND,'P',6,0
db 1+COND,'M',7,0
db 2+COND,'NS',6,0
db 1+COND,'S',7,0
db 0
;
; Evaluate an expression. If a symbol is found, search
; symbol table only.
;
EVALNEW:ld hl,(SYMTBL) ; start of symbol table
ld (SYMPT),hl
; continue below
;
; Evaluate an expression. If a symbol is found, search
; using default symbol table pointer in (SYMPT).
;
; Valid operators are: +, -, *, /, %, brackets and
; extended ops (see table further below)
;
; Valid elements are: ID's, numbers, string constants
; and '$' for PC.
;
EVAL: xor a
ld (PCFLAG),a ; reset PC relative flag
ld (EVFLGS),a ; reset flags from last search
ld (EVMODE),a ; reset mode bits
ld (OPMODE),a
ld (VOID),a
ld (OPSTK),a ; initialize operator
ld (VALSTK),a ; and operand stacks
inc a
ld (UNARY),a ; set unary flag
ld hl,0
ld (VAL),hl ; init val to 0
ld (OPCOMN),hl
EVAL1: call GNC ; get next element or operator
or a
jr z,EVAL5 ; end of line
cp ',' ; or comma
jr z,EVAL4 ; terminates expression
ld (VOID),a
cp '0'
jr c,EVAL3
cp '9'+1
jp c,NUMBER ; branch if number
EVAL3: call VALID1
jp nc,EVAL13 ; branch if ID or PC ref
cp '('
jp z,LPAR
cp ')'
jp z,RPAR
cp '+'
jp z,PLUS
cp '-'
jp z,MINUS
cp '*'
jp z,MULTPLY
cp '/'
jp z,DIVIDE
cp '%' ; modulo
jp z,MODULO
cp "'"
jp z,QCONST ; process quoted char constant
cp '"'
jp z,QCONST ; process quoted char constant
EVAL4: call BACKUP
EVAL5: ld a,(VOID)
or a
jp z,OERROR
EVAL6: ld hl,OPSTK
call STKPOP
jr c,EVAL7 ; exit loop if operator stack empty
ld a,e
cp OPLPAR
jp z,EXPERR ; error if operator is left bracket
ld b,c
ld c,e
call APPLY ; apply operator
jr EVAL6 ; loop to process next
EVAL7: ld hl,VALSTK
call STKPOP ; pop result from value stack
jr nc,EVAL7A
ld de,0 ; if stack was empty, result is zero
ld c,0
EVAL7A: ld a,(VALSTK)
or a ; stack must be empty
jp nz,EXPERR ; else we have an unbalanced expression
ld hl,(OPCOMN)
ld (CMNPTR),hl
ex de,hl
ld a,c
ld (EVMODE),a
ld a,(EVFLGS)
ld (VAL),hl
ret ; get out of here, we're done (note CY clear)
;
; Process number
;
NUMBER: call BACKUP
call INT ; convert to binary using the current base
ASTORE: xor a
ld (OPMODE),a ; mode = Absolute
STORE: ld hl,VALSTK
ld de,(INTBUF)
ld a,(OPMODE)
ld c,a
call STKPUSH ; push value and mode
jp c,EXPERR ; test for stack overflow
xor a
ld (UNARY),a ; clear unary flag
jp EVAL1
;
; Process '$' ref
;
PCREF: call GETPPC ; get effective PC value
ld a,(DBWFLG) ; are we processing DB/DW statement?
or a
jr z,NODB ; branch if not
ld a,(LEN) ; get length so far
ld e,a
ld d,0
add hl,de ; adjust PC value accordingly
NODB: ld (INTBUF),hl
ld a,(CURSEG)
ld (OPMODE),a
cp 0C0h
ld de,(CURCMN)
call z,COMNM
ld a,(PCFLAG) ; fetch PC relative value flag
inc a
ld (PCFLAG),a ; set flag
jp STORE
;
; Process ID
;
EVAL13: call BACKUP
call ID ; get identifier name
ld a,(IDLEN) ; check length
dec a ; single char?
jr nz,EVAL14 ; branch if not
ld a,(IDBUF)
cp '$'
jr z,PCREF ; branch if PC ref
EVAL14: call CHKEXT ; check for external##
jr z,EVAL15
ld de,(SYMPT)
ld c,5
call SYMLUK ; lookup identifier
jr c,EXOPS ; try extended ops if undefined
EVAL15: ld a,(PCFLAG) ; fetch PC relative flag
inc a
ld (PCFLAG),a ; set flag to mark as PC-relative
ld c,l
ld b,h
ld e,(hl) ; get value
inc hl
ld d,(hl)
inc hl
ld a,(hl) ; and mode
ld (OPMODE),a
and EXTSYM
jr z,EVAL16
ld (EXTCHN),bc
ld de,0
EVAL16: ld (INTBUF),de
inc hl
ld e,(hl) ; get pointer to COMMON block, if any
inc hl
ld d,(hl)
call COMNM
ld a,(OPMODE)
and UNDEF ; undefined bit set?
jp z,STORE ; store value if not
EUNDEF: ld hl,0 ; else the respression result is undefined
ld (INTBUF),hl ; set value to 0
ld a,UNDEF
ld (OPMODE),a
ld a,(UFLAG)
or 1
ld (UFLAG),a ; set undefined flag
jp STORE
;
; Check for External declaration of type 'label##'
;
CHKEXT: ld hl,(PTR1)
ld a,(hl)
cp '#'
ret nz
inc hl
ld a,(hl)
cp '#'
ret nz
inc hl
ld (PTR1),hl
call ADDEXT ; add new external reference
ld hl,(SYMADR) ; TODO: handle error? ADDEXT may fail!
xor a
ret ; return with Z flag set if found
;
; Decode extended operators
;
EXOPS: ld de,EXTOPS ; extended operands
ld c,3
call SYMLUK ; search in table
jr c,EXOPER ; branch if not found
ld e,(hl) ; get op number
inc hl
ld d,(hl) ; get precedence
inc hl
ld b,(hl) ; get unary flag
ld a,e
cp 21 ; NUL is special
jr z,BNUL
ex de,hl
ld a,b
or a
jp z,EVAL27 ; jump if not unary (binary)
ld a,(UNARY)
or a ; if unary, see if allowed by syntax rules
jp nz,EVL27B ; go process it if yes
jp EXPERR ; else error
;
BNUL: ld hl,-1 ; preset true
BN1: call GNC
or a
jr z,BN2
ld hl,0 ; false
jr BN1 ; NUL consumes everything up to end of line
BN2: ld (INTBUF),hl
jp ASTORE
;
EXOPER: ld a,(UMODE) ; treat undefined labels as External?
or a
jp z,EUNDEF ; branch if not, leave undefined
ld hl,0
ld (VAL),hl
ld a,UNDEF ; else enter symbol as undefined label
ld (SYMMOD),a ; (will be converted to External later)
call ADDSYM ; TODO: handle error?
ld hl,(SYMADR)
jp EVAL15
;
; Extended operators
;
; db length,name,value,precedence,unary_flag
;
EXTOPS: db 3,'NOT',6,3,1
db 3,'MOD',5,7,0
db 3,'SHR',7,7,0
db 3,'SHL',8,7,0
db 3,'AND',9,2,0
db 2,'OR',10,1,0
db 3,'XOR',11,1,0
db 2,'EQ',12,4,0
db 2,'NE',13,4,0
db 2,'LT',14,4,0
db 2,'LE',15,4,0
db 2,'GT',16,4,0
db 2,'GE',17,4,0
db 3,'LOW',18,8,1
db 4,'HIGH',19,8,1
db 4,'LESS',20,4,0
db 3,'NUL',21,9,0
db 0
OPLPAR equ 22
;
; Process operators
;
PLUS: ld a,(UNARY)
or a
jp nz,EVAL1 ; unary +, ignore
ld hl,5*256+1 ; +
ld b,0
jr EVAL27
;
MINUS: ld a,(UNARY)
or a
ld hl,6*256+0 ; unary -
ld b,1
jr nz,EVL27B
ld hl,5*256+2 ; -
ld b,0
jr EVAL27
;
MULTPLY:ld hl,7*256+3 ; *
ld b,0
jr EVAL27
;
DIVIDE: ld hl,7*256+4 ; /
ld b,0
jr EVAL27
;
MODULO: ld hl,7*256+5 ; % or MOD
ld b,0
jr EVAL27
;
; Here with:
; H = operator precedence
; L = operator code
; B = unary flag
;
EVAL27: push hl
ld hl,OPSTK
call STKPOP ; pop operator
pop hl
jr c,EVL27B ; exit loop if stack empty
ld a,e
cp OPLPAR ; left bracket?
jr z,EVL27A ; exit loop if yes
ld a,d ; get precedence
cp h ; compare with current operator
jr c,EVL27A ; exit loop if lower
push hl
push bc
ld b,c
ld c,e
call APPLY ; else apply current operator
pop bc
pop hl
jr EVAL27 ; and loop to test another one
EVL27A: push hl
ld hl,OPSTK
call STKPUSH ; push operator back
pop hl
EVL27B: ex de,hl
ld c,b
ld hl,OPSTK
call STKPUSH ; push new operator
ld a,1
ld (UNARY),a ; next +/- is unary
jp EVAL1
;
; Process left bracket
;
LPAR: ld de,OPLPAR ; (
ld c,1
ld hl,OPSTK
call STKPUSH
ld a,1
ld (UNARY),a ; next +/- is unary
jp EVAL1
;
; Process right bracket
;
RPAR: ld hl,OPSTK
call STKPOP ; pop operator
jp c,EVAL4 ; exit EVAL if one ) too many (probably OK!)
ld a,e
cp OPLPAR ; left bracket?
jr z,RP1 ; exit loop if yes
ld b,c
ld c,e
call APPLY ; apply operator
jr RPAR ; loop until left bracket found
RP1: xor a
ld (UNARY),a ; next +/- is binary
jp EVAL1
;
; INT - Convert characters to binary
;
; Allow trailing 'H' for hex, 'O' or 'Q' for octal, 'D' for decimal
; and 'B' for binary. Default base is the current .RADIX setting.
;
INT: ld hl,(RADIX)
ld (BASE),hl ; set up default base
ld b,0 ; set up length counter
call GNC ; skip blanks
ld hl,(PTR1)
dec hl ; HL = begin of string
push hl ; save pointer
INT1: cp '0'
jr c,INT3 ; exit loop if terminator
cp '9'+1
jr c,INT2
cp 'A'
jr c,INT3
cp 'Z'+1
jr nc,INT3
INT2: inc hl
ld a,(hl) ; get next character
call UCASE
inc b
jr INT1 ; increment counter and continue loop
;
INT3: ld (PTR1),hl
dec hl
ld a,(hl)
call UCASE
cp 'H'
ld hl,16 ; base 16
jr z,INT4 ; branch if hex
cp 'D'
ld hl,10 ; base 10
jr z,INT4 ; branch if decimal
cp 'Q'
ld hl,8 ; base 8
jr z,INT4 ; branch if octal
cp 'O'
jr z,INT4 ; branch if octal
cp 'B'
ld hl,2 ; base 2
jr nz,INT5 ; branch if not binary
INT4: dec b ; decrement counter
ld (BASE),hl ; set up base
INT5: ld de,0 ; set up accumulator
INT6: pop hl ; get saved pointer to string
ld a,(hl)
inc hl
push hl
call UCASE
cp 'A'
jr c,INT7
sub 7 ; for A-F
INT7: sub '0'
push af ; save digit
ld hl,(BASE)
cp l ; ensure that digit value is < BASE
call nc,VALERR ; else set error flag, but continue anyway
call MULT ; HL = accumulator * BASE
pop af ; restore digit
ld e,a
ld d,0
add hl,de ; add in new digit
ex de,hl
djnz INT6 ; go back if more to do
pop hl ; drop pointer to string
ex de,hl
ld (INTBUF),hl ; save value
ret
;
; Apply operator to value(s) from stack
; On entry, C contains operator code and B the unary flag
;
APPLY: xor a
ld (EVMODE),a
ld hl,VALSTK
push hl
push bc
call STKPOP
ld a,c
pop bc
pop hl
jp c,EXPERR
ld (INTBUF),de ; INTBUF = second operand
ld (OPMODE),a
ld a,b
or a
jr nz,BSKIP ; jump if unary (only one operand)
push bc
call STKPOP
ld a,c
pop bc
jp c,EXPERR
ld (VAL),de ; VAL = first operand
ld (EVMODE),a
BSKIP: ld hl,OPRET ; where to return to after function
push hl ; put return address on stack
ld a,c ; get operator number
add a,a ; double for word index
ld e,a ; index into table with it
ld d,0
ld hl,OPTAB ; table of operation addreses
add hl,de
ld e,(hl)
inc hl
ld d,(hl)
push de ; we are going there via a return
; so push address on stack
ld de,(VAL) ; now get the operands
ld hl,(INTBUF)
ret ; and go to it!
;
OPRET: ex de,hl
ld hl,VALSTK
ld a,(EVMODE)
ld c,a
; fall thru - push result value and mode
;
; Push word value onto stack
; On entry, HL points to stack structure (OPSTK or VALSTK),
; DE contains value to push, C contains mode
; Preserves BC
;
STKPUSH:ld a,(hl) ; get index
cp OSTKSZ ; test for stack overflow
scf
ret z ; on error, return with CY set
push bc
ld c,a
ld b,0
inc a
ld (hl),a
inc hl ; point to stack area
add hl,bc ; index into stack
add hl,bc
add hl,bc
ld (hl),e ; push value
inc hl
ld (hl),d
pop bc
inc hl
ld (hl),c ; push mode
xor a
ret
;
; Pop word value from stack
; On entry, HL points to stack structure (OPSTK or VALSTK)
; Upon return, DE and C contains value and mode respectively
; Preserves B
;
STKPOP: ld a,(hl) ; get index
or a ; test for stack underflow
scf
ret z ; on error, return with CY set
push bc
dec a
ld (hl),a
inc hl ; point to stack area
ld c,a
ld b,0
add hl,bc ; index into stack
add hl,bc
add hl,bc
ld e,(hl) ; pop value
inc hl
ld d,(hl)
inc hl
pop bc
ld c,(hl)
xor a
ret
;
; Operator dispatch table
;
OPTAB: dw UMIN ; 0: unary minus
dw BADD ; 1: add
dw BSUB ; 2: subtract
dw BMULT ; 3: multiply
dw BDIV ; 4: divide
dw BMOD ; 5: modulo
dw UNOT ; 6: unary NOT
dw BSHR ; 7: shift right
dw BSHL ; 8: shift left
dw BAND ; 9: logical AND
dw BOR ; 10: logical OR
dw BXOR ; 11: logical exclusive OR
dw BEQ ; 12: equal
dw BNE ; 13: not equal
dw BLT ; 14: less than
dw BLE ; 15: less than or equal
dw BGT ; 16: greater than
dw BGE ; 17: greater than or equal to
dw ULOW ; 18: unary low byte (MOD 256)
dw UHIGH ; 19: unary high byte (SHR 8)
dw BLESS ; 20: signed less than
;
; Unary minus
;
UMIN: call UNOT
inc hl
ret
;
; Add
;
BADD: call ADDM
add hl,de
ret
;
; Subtract
;
BSUB: call SUBM
ex de,hl
or a
sbc hl,de ; HL = DE-HL
ret
;
; Multiply: HL <= HL * DE (16 bits only)
;
BMULT: call MIXM
MULT: push bc
ld c,l
ld b,h
ld hl,0
ld a,16
MU1: add hl,hl
rl e
rl d
jr nc,MU2
add hl,bc
jr nc,MU2
inc de
MU2: dec a
jr nz,MU1
pop bc
ret
;
; Divide
;
; Divides 16-bit dividend by 16-bit divisor
; On entry: DE = dividend, HL = divisor
; On return: HL = quotient, DE = remainder
;
BDIV: call MIXM
ld a,h ; test for 0 divisor
or l
jr nz,BDIV0 ; jp if not /0
ld a,'Z'
ld (ERRFLG),a ; flag divide by 0 error
scf ; mark 0 divisor as error
ret ; ...and exit
;
BDIV0: push bc
ld c,l
ld b,h
ld hl,0
ld a,16
DIV1: rl e
rl d
adc hl,hl
sbc hl,bc
jr nc,DIV2
add hl,bc
DIV2: ccf
dec a
jr nz,DIV1
rl e
rl d
ex de,hl
pop bc
ret
;
EXPERR: ld a,'E'
ld (ERRFLG),a ; flag expression error
scf
ret ; exit this line
;
; Modulo
;
BMOD: call BDIV
ex de,hl ; DE has modulo
ret
;
; Process single/double byte in quotes
;
QCONST: ld c,a ; save quote char in C
ld hl,(PTR1)
call CCONST ; get character constant
jp c,EXPERR
ld (PTR1),hl
ld (INTBUF),de ; store result
jp STORE ; and go process value normally
;
; Get single/double byte character constant
;
CCONST: ld de,0 ; init result
ld a,(hl) ; get character
or a
scf
ret z
inc hl
cp c
jr nz,CC1 ; not double quotes
ld a,(hl) ; fetch next char
cp c
jr nz,CC3 ; not double quotes
inc hl
CC1: ld e,a
ld a,(hl) ; fetch second byte
or a
scf
ret z
inc hl
cp c
jr nz,CC2
ld a,(hl) ; fetch next char
cp c
jr nz,CC3 ; not double quotes -> 1 byte value
inc hl
CC2: ld d,a ; make 2 byte value
ld a,(hl)
or a
scf
ret z
inc hl
cp c ; check for closing quote
scf
ret nz
CC3: xor a ; return success
ret
;
; UNOT - Logical complement
;
UNOT: call UNARYM
ld a,h ; complement result
cpl
ld h,a
ld a,l
cpl
ld l,a
ret
;