-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexec_arithm.S
971 lines (792 loc) · 26 KB
/
exec_arithm.S
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
; ****************************************************************************
;
; Execute arithmetics
;
; ****************************************************************************
#include "include.inc"
.text
; bridges
.global _DispFlags
_DispFlags: jmp DispFlags
.global _EditStopNoX
_EditStopNoX: jmp EditStopNoX
; ----------------------------------------------------------------------------
; Get current operation
; ----------------------------------------------------------------------------
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
; ----------------------------------------------------------------------------
.global ExecLevel
ExecLevel:
ldd r24,Y+DATA_LEVEL ; current level of arithmetics operations
ldi r30,lo8(OperStack)
ldi r31,hi8(OperStack)
add r30,r24
adc r31,R_ZERO
ld r24,Z
ret
; ----------------------------------------------------------------------------
; Calculate one level of arithmetics operations
; ----------------------------------------------------------------------------
.global ExecCalc
ExecCalc:
; edit stop
rcall _EditStopNoX ; edit stop (not validating X)
; load X into stack
ldi r24,REG_X ; register X
; INPUT: R24 = index of the number
; OUTPUT: R1 = 0
; DESTROYS: R31, R30, R27..R24, R0
; CALCULATOR STACK: +1
call CalcGetMem ; load X into stack
; check if current operation is valid to save it
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
rcall ExecLevel ; get current operation
andi r24,OPER_MASK | LEVEL_MASK ; check current operation
breq 2f ; current operation is not valid
; copy X into LAST, save last operation
std Y+DATA_LAST,r24 ; save last operation (LOWER = 0)
ldi r24,REG_LAST ; register Last
; INPUT: R24 = index of a number
; OUTPUT: R1 = 0
; DESTROYS: R31, R30, R27..R24, R0
call CalcSetMem ; save Last (does not delete it from the stack)
; ----- switch by current operation
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
rcall ExecLevel ; get current operation
ExecCalcOn:
2: andi r24,OPER_MASK ; mask operation
; +
cpi r24,OPER_PLUS
brne 3f
; DESTROYS: all
; CALCULATOR STACK: -1
call CalcAdd ; add
rjmp 9f
; -
3: cpi r24,OPER_MINUS
brne 4f
; DESTROYS: all
; CALCULATOR STACK: -1
call CalcSub ; sub
rjmp 9f
; *
4: cpi r24,OPER_MUL
brne 5f
; DESTROYS: all
; CALCULATOR STACK: -1
call CalcMul ; multiply
rjmp 9f
; :
5: cpi r24,OPER_DIV
brne 6f
; DESTROYS: all
; CALCULATOR STACK: -1
call CalcDiv ; divide
rjmp 9f
; ^
6: cpi r24,OPER_POWER
brne 7f
call CalcPow ; power
rjmp 9f
; root
7: cpi r24,OPER_ROOT
brne 8f
call CalcRec ; reciprocal
call CalcPow ; power
rjmp 9f
; &
8: cpi r24,OPER_AND
breq 2f
; |
cpi r24,OPER_OR
breq 2f
; ~
cpi r24,OPER_XOR
brne 4f
2: call CalcBit
rjmp 9f
; mod (trunc)
4: cpi r24,OPER_MOD
brne 5f
call CalcMod
rjmp 9f
; mod2 (floor)
5: cpi r24,OPER_MOD2
brne 6f
call CalcMod2
rjmp 9f
; %
6: cpi r24,OPER_PERC
brne 7f
call CalcMul ; multiply
ldi r24,CONST_001 ; constant 0.01
rcall CalcConst ; load constant
call CalcMul ; multiply result by 0.01
rjmp 9f
; <
7: cpi r24,OPER_LEFT
brne 2f
call CalcLeft
rjmp 9f
; >
2: cpi r24,OPER_RIGHT
brne 8f
call CalcRight
rjmp 9f
; no operation - only delete 2nd operand
; DESTROYS: R31, R30
; CALCULATOR STACK: -1
8: call CalcDel ; delete 2nd operand
; save result into X
9: ldi r24,REG_X ; register X
; INPUT: R24 = index of a number
; OUTPUT: R1 = 0
; DESTROYS: R31, R30, R27..R24, R0
call CalcSetMem ; set X
SET_XVALID ; set X valid
; clear current operation
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
rcall ExecLevel ; get current level
andi r24,LOWER_MASK
st Z,r24 ; clear current operation
; DESTROYS: R0
rjmp _Disp ; display all
; ----------------------------------------------------------------------------
; Increase arithmetics level
; ----------------------------------------------------------------------------
; OUTPUT: R31:R30 = pointer into OperStack
; ----------------------------------------------------------------------------
.global ExecLevelInc
ExecLevelInc:
; increase operation level (with overflow check)
ldd r24,Y+DATA_LEVEL ; current level of arithmetics operations
cpi r24,LEVEL_MAX ; check level
brcc ExecLevelDec2 ; fatal error
inc r24 ; increase level
std Y+DATA_LEVEL,r24 ; set new level
.global ExecLevelInit
ExecLevelInit:
; load X into stack
ldi r24,REG_X ; register X
; INPUT: R24 = index of the number
; OUTPUT: R1 = 0
; DESTROYS: R31, R30, R27..R24, R0
; CALCULATOR STACK: +1
call CalcGetMem ; load X into stack
; clear current operation
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
rcall ExecLevel ; get current level
st Z,R_ZERO ; clear flag
ret
; ----------------------------------------------------------------------------
; Decrease arithmetics level
; ----------------------------------------------------------------------------
; OUTPUT: R31:R30 = pointer into OperStack
; ----------------------------------------------------------------------------
.global ExecLevelDec
ExecLevelDec:
ldd r24,Y+DATA_LEVEL ; current number of arithmetics operations
tst r24 ; already base level?
brne 2f
ExecLevelDec2:
jmp Fatal ; fatal error
; DESTROYS: R31, R30
; CALCULATOR STACK: -1
2: call CalcDel ; delete number from stack
; ldd r24,Y+DATA_LEVEL ; current number of arithmetics operations
dec r24 ; increase level
std Y+DATA_LEVEL,r24 ; set new level
ExecLevelDec9:
ret
; ----------------------------------------------------------------------------
; ( (0x53)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecLpar
ExecLpar:
; stop edit mode (not validating X)
rcall _EditStopNoX ; stop edit mode
CLR_XVALID ; register X is NOT valid
rcall ExecLevelInc ; increate level
; DESTROYS: -
rjmp _DispFlags ; disp flags
; ----------------------------------------------------------------------------
; ) (0x54)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecRpar
ExecRpar:
; stop edit mode (not validating X)
rcall _EditStopNoX ; edit stop (not validating X)
; ----- close levels of one parenthesis
2: rcall ExecCalc ; calculate current level
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
rcall ExecLevel ; get current level
push r24
ldd r24,Y+DATA_LEVEL ; current level of arithmetics operations
tst r24 ; already base level?
breq 4f
rcall ExecLevelDec ; decrease level
4: pop r24
andi r24,LOWER_MASK ; mask lower level
cpi r24,LOWER_NONE ; level () ?
brne 2b ; close up to () level
; DESTROYS: -
rjmp _DispFlags ; disp flags
; ----------------------------------------------------------------------------
; + (0x85)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecAdd
ExecAdd:
rcall _EditStopNoX ; edit stop (not validating X)
IFN_XVALID ; if X is not valid
rjmp 8f ; only update operation
; ----- close higher level - power
; close higher level - power
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
rcall ExecLevel ; get current level
andi r24,LEVEL_MASK ; mask current level
cpi r24,LEVEL_POWER ; higher level - power?
brne 4f
rcall ExecCalc ; calculate current level
; decrease level, if lower level is */ or +-
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
rcall ExecLevel ; get current level
andi r24,LOWER_MASK ; mask lower level
cpi r24,LOWER_MULDIV ; level */ ?
breq 3f ; decrease level
cpi r24,LOWER_PLUSMINUS ; level +- ?
brne 4f
3: rcall ExecLevelDec ; decrease level
; ----- close higher level - multiply
; close higher level - multiply
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
4: rcall ExecLevel ; get current level
andi r24,LEVEL_MASK ; mask operation
cpi r24,LEVEL_MULDIV ; higher level - multiply?
brne 6f
rcall ExecCalc ; calculate current level
; decrease level, if lower level is +-
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
rcall ExecLevel ; get current level
andi r24,LOWER_MASK ; mask lower level
cpi r24,LOWER_PLUSMINUS ; level +- ?
brne 6f
rcall ExecLevelDec ; decrease level
; ----- close this level
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
6: rcall ExecLevel ; get current level
andi r24,LEVEL_MASK ; mask operation
cpi r24,LEVEL_PLUSMINUS ; this level - add?
brne 7f
rcall ExecCalc ; calculate current level
; ----- update this level
; DESTROYS: R31, R30
; CALCULATOR STACK: -1
7: call CalcDel ; delete accumulator
ldi r24,REG_X ; register X
; INPUT: R24 = index of the number
; OUTPUT: R1 = 0
; DESTROYS: R31, R30, R27..R24, R0
; CALCULATOR STACK: +1
call CalcGetMem ; load X into stack
CLR_XVALID ; register X is NOT valid
; update this level - set +- operation
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
8: rcall ExecLevel ; get current level
andi r24,LOWER_MASK ; mask lower operation
ori r24,LEVEL_PLUSMINUS | OPER_PLUS
st Z,r24 ; set current operation
; display flags with current operation
; DESTROYS: -
rjmp _DispFlags ; disp flags
; ----------------------------------------------------------------------------
; - (0x75)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecSub
ExecSub:
rcall ExecAdd ; close +/- operations
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
; rcall ExecLevel ; get current level
andi r24,LOWER_MASK ; mask lower operation
ori r24,LEVEL_PLUSMINUS | OPER_MINUS
ExecSub2:
st Z,r24 ; set current operation
; DESTROYS: -
rjmp _DispFlags ; disp flags
; ----------------------------------------------------------------------------
; & (0x6E)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecAnd
ExecAnd:
rcall ExecAdd ; close +/- operations
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
; rcall ExecLevel ; get current level
andi r24,LOWER_MASK ; mask lower operation
ori r24,LEVEL_PLUSMINUS | OPER_AND
rjmp ExecSub2
; ----------------------------------------------------------------------------
; ~ (0x7E)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecXor
ExecXor:
rcall ExecAdd ; close +/- operations
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
; rcall ExecLevel ; get current level
andi r24,LOWER_MASK ; mask lower operation
ori r24,LEVEL_PLUSMINUS | OPER_XOR
rjmp ExecSub2
; ----------------------------------------------------------------------------
; | (0x8E)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecOr
ExecOr:
rcall ExecAdd ; close +/- operations
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
; rcall ExecLevel ; get current level
andi r24,LOWER_MASK ; mask lower operation
ori r24,LEVEL_PLUSMINUS | OPER_OR
rjmp ExecSub2
; ----------------------------------------------------------------------------
; x (0x65)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecMul
ExecMul:
rcall _EditStopNoX ; edit stop (not validating X)
IFN_XVALID ; if X is not valid
rjmp 8f ; only update operation
; ----- close higher level - power
; close higher level - power
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
rcall ExecLevel ; get current level
andi r24,LEVEL_MASK ; mask current level
cpi r24,LEVEL_POWER ; higher level - power?
brne 4f
rcall ExecCalc ; calculate current level
; decrease level, if lower level is */
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
rcall ExecLevel ; get current level
andi r24,LOWER_MASK ; mask lower level
cpi r24,LOWER_MULDIV ; level */ ?
brne 4f
rcall ExecLevelDec ; decrease level
; ----- close this level - mul
; close this level - mul
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
4: rcall ExecLevel ; get current level
andi r24,LEVEL_MASK ; mask operation
cpi r24,LEVEL_MULDIV ; this level - mul?
brne 6f
rcall ExecCalc ; calculate current level
; ----- increase lower level +-
; increase lower level +-
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
6: rcall ExecLevel ; get current level
andi r24,LEVEL_MASK ; mask current level
cpi r24,LEVEL_PLUSMINUS ; lower level - plus?
brne 7f
; increase level
rcall ExecLevelInc ; increase level
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
rcall ExecLevel ; get current level
ldi r24,LOWER_PLUSMINUS
st Z,r24 ; ser lower level
; ----- update this level
; DESTROYS: R31, R30
; CALCULATOR STACK: -1
7: call CalcDel ; delete accumulator
ldi r24,REG_X ; register X
; INPUT: R24 = index of the number
; OUTPUT: R1 = 0
; DESTROYS: R31, R30, R27..R24, R0
; CALCULATOR STACK: +1
call CalcGetMem ; load X into stack
CLR_XVALID ; register X is NOT valid
; update this level - set */ operation
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
8: rcall ExecLevel ; get current level
andi r24,LOWER_MASK ; mask lower operation
ori r24,LEVEL_MULDIV | OPER_MUL
st Z,r24 ; set current operation
; display flags
; DESTROYS: -
rjmp _DispFlags ; disp flags
; ----------------------------------------------------------------------------
; : (0x55)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecDiv
ExecDiv:
rcall ExecMul ; close */ operations
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
; rcall ExecLevel ; get current level
andi r24,LOWER_MASK ; mask lower operation
ori r24,LEVEL_MULDIV | OPER_DIV
ExecDiv2:
st Z,r24 ; set current operation
; DESTROYS: -
rjmp _DispFlags ; disp flags
; ----------------------------------------------------------------------------
; mod trunc (0x5e)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecMod
ExecMod:
rcall ExecMul ; close */ operations
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
; rcall ExecLevel ; get current level
andi r24,LOWER_MASK ; mask lower operation
ori r24,LEVEL_MULDIV | OPER_MOD
rjmp ExecDiv2
; ----------------------------------------------------------------------------
; mod2 floor (0x4e)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecMod2
ExecMod2:
rcall ExecMul ; close */ operations
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
; rcall ExecLevel ; get current level
andi r24,LOWER_MASK ; mask lower operation
ori r24,LEVEL_MULDIV | OPER_MOD2
rjmp ExecDiv2
; ----------------------------------------------------------------------------
; % (0x9e)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecPerc
ExecPerc:
; edit stop
rcall _EditStopNoX ; edit stop (not validating X)
; load X into stack
ldi r24,REG_X ; register X
; INPUT: R24 = index of the number
; OUTPUT: R1 = 0
; DESTROYS: R31, R30, R27..R24, R0
; CALCULATOR STACK: +1
call CalcGetMem ; load X into stack
; ----- save last operation
; check if current operation is valid to save it
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
rcall ExecLevel ; get current operation
andi r24,OPER_MASK | LEVEL_MASK ; check current operation
breq 1f ; current operation is not valid
; copy X into LAST, save last operation
std Y+DATA_LAST,r24 ; save last operation (LOWER = 0)
ldi r24,REG_LAST ; register Last
; INPUT: R24 = index of a number
; OUTPUT: R1 = 0
; DESTROYS: R31, R30, R27..R24, R0
call CalcSetMem ; save Last (does not delete it from the stack)
rjmp 2f
; ----- operation is not valid, save as operator a % b
1: call CalcDel ; delete X
rcall ExecMul ; close */ operations
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
andi r24,LOWER_MASK ; mask lower operation
ori r24,LEVEL_MULDIV | OPER_PERC
rjmp ExecDiv2
; ----- switch by current operation
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
2: rcall ExecLevel ; get current operation
andi r24,OPER_MASK ; mask operation
; +
cpi r24,OPER_PLUS
brne 3f
rcall Calc ; calculator (x,y)
.byte C_DUP2 ; pre-duplicate (x,y,x)
.byte C_MUL ; multiply (x,y*x)
.byte C_CONST(CONST_001) ; load constant 0.01 (x,y*x,0.01)
.byte C_MUL ; multiply (x,y*x*0.01)
.byte C_SETMEM(REG_LAST) ; save last operand
.byte C_ADD ; add (x + y*x*0.01)
.byte C_END
.balign 2
rjmp 9f
; -
3: cpi r24,OPER_MINUS
brne 4f
rcall Calc ; calculator (x,y)
.byte C_DUP2 ; pre-duplicate (x,y,x)
.byte C_MUL ; multiply (x,y*x)
.byte C_CONST(CONST_001) ; load constant 0.01 (x,y*x,0.01)
.byte C_MUL ; multiply (x,y*x*0.01)
.byte C_SETMEM(REG_LAST) ; save last operand
.byte C_SUB ; sub (x - y*x*0.01)
.byte C_END
.balign 2
rjmp 9f
; *
4: cpi r24,OPER_MUL
brne 5f
rcall Calc ; calculator (x,y)
.byte C_CONST(CONST_001) ; load constant 0.01 (x,y,0.01)
.byte C_MUL ; multiply (x,y*0.01)
.byte C_SETMEM(REG_LAST) ; save last operand
.byte C_MUL ; multiply (x * y*0.01)
.byte C_END
.balign 2
rjmp 9f
; :
5: cpi r24,OPER_DIV
brne 6f
rcall Calc ; calculator (x,y)
.byte C_CONST(CONST_001) ; load constant 0.01 (x,y,0.01)
.byte C_MUL ; multiply (x,y*0.01)
.byte C_SETMEM(REG_LAST) ; save last operand
.byte C_DIV ; multiply (x / (y*0.01))
.byte C_END
.balign 2
rjmp 9f
; no operation - only delete 2nd operand
; DESTROYS: R31, R30
; CALCULATOR STACK: -1
6: call CalcDel ; delete 2nd operand
; save result into X
9: ldi r24,REG_X ; register X
; INPUT: R24 = index of a number
; OUTPUT: R1 = 0
; DESTROYS: R31, R30, R27..R24, R0
call CalcSetMem ; set X
SET_XVALID ; set X valid
; clear current operation
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
rcall ExecLevel ; get current level
andi r24,LOWER_MASK
st Z,r24 ; clear current operation
; DESTROYS: R0
rjmp _Disp ; display all
; ----------------------------------------------------------------------------
; left << (0x5b)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecLeft
ExecLeft:
rcall ExecMul ; close */ operations
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
; rcall ExecLevel ; get current level
andi r24,LOWER_MASK ; mask lower operation
ori r24,LEVEL_MULDIV | OPER_LEFT
rjmp ExecDiv2
; ----------------------------------------------------------------------------
; right >> (0x5c)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecRight
ExecRight:
rcall ExecMul ; close */ operations
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
; rcall ExecLevel ; get current level
andi r24,LOWER_MASK ; mask lower operation
ori r24,LEVEL_MULDIV | OPER_RIGHT
rjmp ExecDiv2
; ----------------------------------------------------------------------------
; y^x (0x45)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecYx
ExecYx:
push r23 ; push INV flag
rcall _EditStopNoX ; edit stop (not validating X)
IFN_XVALID ; if X is not valid
rjmp 8f ; only update operation
; ----- close this level
; close this level - power
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
2: rcall ExecLevel ; get current level
andi r24,LEVEL_MASK ; mask operation
cpi r24,LEVEL_POWER ; this level - power?
brne 4f
rcall ExecCalc ; calculate current level
; ----- increase lower level */
; increase lower level */
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
4: rcall ExecLevel ; get current level
andi r24,LEVEL_MASK ; mask current level
cpi r24,LEVEL_MULDIV ; lower level - mul?
brne 5f
rcall ExecLevelInc ; increase level
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
rcall ExecLevel ; get current level
ldi r24,LOWER_MULDIV
st Z,r24 ; ser lower level
; ----- increase lower level +-
; increase lower level +-
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
5: rcall ExecLevel ; get current level
andi r24,LEVEL_MASK ; mask current level
cpi r24,LEVEL_PLUSMINUS ; lower level - plus?
brne 6f
rcall ExecLevelInc ; increase level
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
rcall ExecLevel ; get current level
ldi r24,LOWER_PLUSMINUS
st Z,r24 ; ser lower level
; ----- update this level - set ^ operation
; DESTROYS: R31, R30
; CALCULATOR STACK: -1
6: call CalcDel ; delete accumulator
ldi r24,REG_X ; register X
; INPUT: R24 = index of the number
; OUTPUT: R1 = 0
; DESTROYS: R31, R30, R27..R24, R0
; CALCULATOR STACK: +1
rcall CalcGetMem ; load X into stack
CLR_XVALID ; register X is NOT valid
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
8: rcall ExecLevel ; get current level
andi r24,LOWER_MASK ; mask lower operation
pop r23 ; pop INV flag
tst r23 ; INV flag?
ldi r23,LEVEL_POWER | OPER_POWER
breq 7f ; power
ldi r23,LEVEL_POWER | OPER_ROOT
7: or r24,r23
st Z,r24 ; set current operation
; DESTROYS: -
rjmp _DispFlags ; disp flags
; ----------------------------------------------------------------------------
; = (0x95)
; ----------------------------------------------------------------------------
; INPUT: R24 = key HEX code 0x00..0xFF
; R23 = INV flag (1=set)
; Z = clear if INV flag was set (=brne)
; ----------------------------------------------------------------------------
.global ExecRes
ExecRes:
; stop edit mode (not validating X)
rcall _EditStopNoX ; edit stop (not validating X)
; level = 0 ? (condition to repeat last operation)
ldd r24,Y+DATA_LEVEL ; current level of arithmetics operations
tst r24 ; base level?
brne 2f ; not base level, no repeat last operation
; check open operation
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
rcall ExecLevel ; get current operation
andi r24,OPER_MASK ; mask operation
breq 4f ; no operation - repeat last operation
; close all levels
2: rcall ExecRpar ; close one level )
ldd r24,Y+DATA_LEVEL ; current level of arithmetics operations
tst r24 ; valid?
brne 2b
rjmp ExecRpar ; close last level
; ===== repeat last operation (Level must be 0 and no open operation, OPER_NONE)
; check if last operation is valid
4: ldd r24,Y+DATA_LAST ; valid last operation?
tst r24
breq 9f ; not valid
; delete accumulator and load X
; DESTROYS: R31, R30
; CALCULATOR STACK: -1
call CalcDel ; delete accumulator
ldi r24,REG_X ; register X
; INPUT: R24 = index of the number
; OUTPUT: R1 = 0
; DESTROYS: R31, R30, R27..R24, R0
; CALCULATOR STACK: +1
rcall CalcGetMem ; load X into stack
; load LAST as second operand
ldi r24,REG_LAST
; INPUT: R24 = index of the number
; OUTPUT: R1 = 0
; DESTROYS: R31, R30, R27..R24, R0
; CALCULATOR STACK: +1
rcall CalcGetMem ; load register Last
; set last operation as current operation
; OUTPUT: R31:R30 = pointer into OperStack
; R24 = current operation
rcall ExecLevel ; get current operation
ldd r24,Y+DATA_LAST
st Z,r24
rjmp ExecCalcOn ; calculate operation
9: ret