-
Notifications
You must be signed in to change notification settings - Fork 1
/
calc_arithm.S
1866 lines (1538 loc) · 53.8 KB
/
calc_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
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
; ****************************************************************************
;
; Calculator arithmetics and bitwise operations (2 operands)
;
; ****************************************************************************
#include "include.inc"
.text
; ----------------------------------------------------------------------------
; Prepare number for multiplication and division
; ----------------------------------------------------------------------------
; INPUT: R23 = previous sign flag
; R31:R30 (Z) = pointer to the number
; R_ZERO = 0
; OUTPUT: R23 = new sign flag
; flag Z = number is zero (and C is not set)
; flag C = number is overflow (and Z is not set)
; DESTROYS: R24, R25
; ----------------------------------------------------------------------------
; Get sign bit and restore hidden bit '1' of mantissa.
CalcPrepMul:
; ----- get exponent
ldd r25,Z+0 ; fetch exponent HIGH
ldd r24,Z+1 ; fetch exponent LOW
; ----- test for overflow (set C) and zero (set Z)
adiw r24,1
sbiw r24,1 ; check for zero (0x0001->0x0000->ZY) and overflow (0x0000->0xFFFF->CY)
breq CalcPrepMul9 ; number is zero
; ----- check sign and restore highest bit of the mantissa
; C will be not affected
ldd r24,Z+2 ; highest byte of the mantissa
eor r23,r24 ; flip sign flag
ori r24,B7 ; restore hidden highest bit of the mantissa
std Z+2,r24 ; save new highest byte
; here is Z flag cleared
CalcPrepMul9:
ret
; ===== here is rest of CalcMul function
; ----- 2nd number is overflow
CalcMul6:
; INPUT and OUTPUT: R31:R30 (Z), R27:R26 (X) = registers to exchange
; DESTROYS: -
rcall ExcXZ ; exchange pointers
; INPUT: R23 = previous sign flag
; R31:R30 (Z) = pointer to the number
; R_ZERO = 0
; OUTPUT: R23 = new sign flag
; flag Z = number is zero (and C is not set)
; flag C = number is overflow (and Z is not set)
; DESTROYS: R24, R25
rcall CalcPrepMul ; prepare 1st number
breq CalcMul7 ; 1st number is 0, result will be 1
; ----- 1st number is not 0, result will be overflow
CalcMul62:
; OUTPUT: CY = error flag
; DESTROYS: -
rcall CalcError ; set error flag
; OUTPUT: R31:R30 (Z) = pre-last number on calculator stack
; DESTROYS: -
rcall CalcPreTop ; get pre-last number -> Z
; INPUT: R31:R30 = float number
; DESTROYS: R_M1,...R_M10
rcall CalcZOver ; set number Z to overflow
rjmp CalcDel ; delete 2nd operand
; ----- result is 1 with error
CalcMul7:
; OUTPUT: CY = error flag
; DESTROYS: -
rcall CalcError ; set error flag
; OUTPUT: R31:R30 (Z) = pre-last number on calculator stack
; DESTROYS: -
rcall CalcPreTop ; get pre-last number -> Z
; INPUT: R31:R30 = float number
; DESTROYS: R_M1,...R_M10
push r23
rcall CalcZ1 ; set number Z to 1
pop r23
tst r23 ; check sign
brpl 2f ; result is positive
; DESTROYS: R31, R30, R25, R24
rcall CalcNeg ; result will be -1
; DESTROYS: R31, R30
; CALCULATOR STACK: -1
2: rjmp CalcDel ; delete 2nd operand
; ----- 2nd number is 0
CalcMul81:
; INPUT and OUTPUT: R31:R30 (Z), R27:R26 (X) = registers to exchange
; DESTROYS: -
rcall ExcXZ ; exchange pointers
; INPUT: R23 = previous sign flag
; R31:R30 (Z) = pointer to the number
; R_ZERO = 0
; OUTPUT: R23 = new sign flag
; flag Z = number is zero (and C is not set)
; flag C = number is overflow (and Z is not set)
; DESTROYS: R24, R25
rcall CalcPrepMul ; prepare 1st number
brcs CalcMul7 ; 1st number is overflow, result will be 1
; ----- result will be 0
CalcMul82:
; OUTPUT: R31:R30 (Z) = pre-last number on calculator stack
; DESTROYS: -
rcall CalcPreTop ; get pre-last number -> Z
; INPUT: R31:R30 = float number
; DESTROYS: R_M1,...R_M10
rcall CalcZ0 ; clear number Z
rjmp CalcDel ; delete 2nd operand
; ----------------------------------------------------------------------------
; Multiplicate two numbers on top of stack (C_MUL)
; ----------------------------------------------------------------------------
; DESTROYS: all
; CALCULATOR STACK: -1
; ----------------------------------------------------------------------------
.global CalcMul
CalcMul:
; ----- get last 2 numbers (1st -> X, 2nd -> Z)
; OUTPUT: R27:R26 (X) = pre-last number on calculator stack
; R31:R30 (Z) = last number on calculator stack
; DESTROYS: -
rcall CalcTop2
; ----- prepare numbers for multiplication; -> 1st number Z, 2nd number X, sign bit R_MS (not masked)
; INPUT: R23 = previous sign flag
; R31:R30 (Z) = pointer to the number
; R_ZERO = 0
; OUTPUT: R23 = new sign flag
; flag Z = number is zero (and C is not set)
; flag C = number is overflow (and Z is not set)
; DESTROYS: R24, R25
clr r23 ; prepare sign flag = 0
rcall CalcPrepMul ; prepare 2nd number
brcs CalcMul6 ; 2nd number is overflow
breq CalcMul81 ; 2nd number is zero
; INPUT and OUTPUT: R31:R30 (Z), R27:R26 (X) = registers to exchange
; DESTROYS: -
rcall ExcXZ ; exchange pointers
; INPUT: R23 = previous sign flag
; R31:R30 (Z) = pointer to the number
; R_ZERO = 0
; OUTPUT: R23 = new sign flag
; flag Z = number is zero (and C is not set)
; flag C = number is overflow (and Z is not set)
; DESTROYS: R24, R25
rcall CalcPrepMul ; prepare 1st number
brcs CalcMul62 ; 1st number is overflow, result is overflow
breq CalcMul82 ; 1st number is zero, result will be 0
mov R_MS,r23 ; sign flag
; ----- fetch 2 numbers and delete 2nd number:
; 1st number Z -> R_M1..R_M10
; 2nd number X -> R_N1..R_N10
; INPUT: R31:R30 (Z) = pointer to 1st number
; R27:R26 (X) = pointer to 2nd number
; OUTPUT: R_M1..R_M10 = 1st number (from Z)
; R_N1..R:N10 = 2nd number (from X)
; DESTROYS: R31, R30, R27, R26
; CALCULATOR STACK: -1
rcall CalcFetch2
; ----- sum exponents -> R_MT:R_M1:R_M2
clr R_MT ; exponent HIGH
add R_M2,R_N2 ; sum both exponents
adc R_M1,R_N1
adc R_MT,R_MT ; high carry
; ----- multiplication of mantissas, R_M3..R_M10 * R_N3..R_N10 -> R_M3..R_M10, R_M11
; INPUT: R_M3..R_M10 = 1st multiplier
; R_N3..R_N10 = 2nd multiplier
; OUTPUT: R_M3..R_M10, R_M11 = result (not rounded)
; R1 = 0
; DESTROYS: R_A3 (R31), R_A4 (R30), R_A7 (R27), R_A8 (R26), R0
rcall MulMant
; ----- prepare exponent -> R_MT:R_EXH:R_EXL
movw R_EXL,R_M2 ; exponent
subi R_EXL,lo8(EXP_BIAS-1) ; subtract exponent bias
sbci R_EXH,hi8(EXP_BIAS-1)
sbc R_MT,R_ZERO
; ----- normalize number: R_MS sign, R_M3..R_M10(:R_M11) mantissa, R_MT:R_EXH:R_EXL exponent
CalcMulNorm:
ldi R_M1,MANT_BITS-TRIM ; max. number of shifts (trim last bits for case of small differences)
CalcMulNorm2:
sbrc R_M3,7 ; check highest bit of mantissa
rjmp CalcMulNorm5 ; highest bit is on the position, number is normalized
lsl R_M11 ; get lower bit to round
; INPUT: R_M3..R_M10 mantissa
; C = input carry
; OUTPUT: R_M3..R_M10 mantissa shifted left
; C = output carry
; DESTROYS: -
rcall CalcMantRol ; rotate mantissa left (with carry)
sbiw R_EXL,1 ; decrement exponent
sbc R_MT,R_ZERO ; carry
dec R_M1 ; bit counter
brne CalcMulNorm2
; ----- underflow, reset result to 0
; OUTPUT: R_M1..R_M10 number (= 0)
; DESTROYS: -
CalcMulUnder:
rcall CalcClearNum ; clear result
rjmp CalcMulSave ; save result
; ----- round up
CalcMulNorm5:
lsl R_M11 ; check lowest bit
brcc CalcMulNorm6 ; no carry
; INPUT: R_M3..R_M10 = mantissa
; R_ZERO = 0
; OUTPUT: returns Z flag if result is 0 (overflow from 0xFF 0xFF 0xFF...)
; DESTROYS: -
rcall CalcMantInc ; increment mantissa
brne CalcMulNorm6 ; result is not 0
ldi R_M3,0x80 ; overflow, set value to 0x80 0x00 0x00 0x00 0x00 0x00
adiw R_EXL,1 ; increment exponent R_EXH:R_EXL
adc R_MT,R_ZERO ; carry
; ----- check exponent
CalcMulNorm6:
tst R_MT ; check exponent HIGH
brmi CalcMulUnder ; underflow
brne CalcMulOver ; overflow
cp R_EXL,R_ZERO ; check minimal exponent
cpc R_EXH,R_ZERO
breq CalcMulUnder ; underflow
ldi R_M1,hi8(EXP_MAX+1)
cpi R_EXL,lo8(EXP_MAX+1) ; check maximal exponent
cpc R_EXH,R_M1
brcc CalcMulOver ; overflow
; ----- prepare 1st byte of mantissa -> R_M3
andi R_M3,0x7f ; reset hidden highest bit "1"
andi R_MS,0x80 ; mask sign flag
or R_M3,R_MS ; put bits together
; ----- exponent -> R_M1:R_M2
movw R_M2,R_EXL
; ----- save result R_M1..R_M9
; OUTPUT: R31:R30 (Z) = last number on calculator stack
; DESTROYS: -
CalcMulSave:
rcall CalcTop ; get pointer to top number -> Z
; INPUT: R31:R30 (Z) = pointer to number
; R_M1..R_M10 number
; DESTROYS: -
rjmp CalcSaveNum ; save number
; ----- overflow error
CalcMulOver:
; OUTPUT: R31:R30 (Z) = last number on calculator stack
; DESTROYS: -
rcall CalcTop ; get pointer to top number -> Z
rcall CalcZOver ; set overflow value
; OUTPUT: CY = error flag
; DESTROYS: -
rjmp CalcError ; set error flag
; ----------------------------------------------------------------------------
; divide/2/4/8/16 (C_DIV2)
; ----------------------------------------------------------------------------
; DESTROYS: R31, R30, R25, R24
; ----------------------------------------------------------------------------
.global CalcDiv16
CalcDiv16:
rcall CalcDiv2
.global CalcDiv8
CalcDiv8:
rcall CalcDiv2
.global CalcDiv4
CalcDiv4:
rcall CalcDiv2
.global CalcDiv2
CalcDiv2:
; OUTPUT: R31:R30 (Z) = last number on calculator stack
; R25:R24 = exponent (0 = number is zero, 0xFFFF = overflow)
; ZY = number is 0
; CY = number is overflow
; DESTROYS: -
rcall CalcTopCheck ; get top number -> Z, R24
brcs CalcMulOver ; number is overflow
breq CalcDiv29 ; number is zero
sbiw r24,1 ; decrement exponent
CalcDiv26:
std Z+1,r24 ; set new exponent
std Z+0,r25
CalcDiv29:
ret
; ----------------------------------------------------------------------------
; multiply*2/4/8/16 (C_MUL2)
; ----------------------------------------------------------------------------
; DESTROYS: R31, R30, R25, R24
; ----------------------------------------------------------------------------
.global CalcMul16
CalcMul16:
rcall CalcMul2
.global CalcMul8
CalcMul8:
rcall CalcMul2
.global CalcMul4
CalcMul4:
rcall CalcMul2
.global CalcMul2
CalcMul2:
; OUTPUT: R31:R30 (Z) = last number on calculator stack
; R25:R24 = exponent (0 = number is zero, 0xFFFF = overflow)
; ZY = number is 0
; CY = number is overflow
; DESTROYS: -
rcall CalcTopCheck ; get top number -> Z, R24
brcs CalcMulOver ; number is overflow
breq CalcDiv29 ; number is zero
adiw r24,1 ; increment exponent
rjmp CalcDiv26 ; set new exponent
; ----------------------------------------------------------------------------
; < (C_LEFT)
; ----------------------------------------------------------------------------
.global CalcLeft
CalcLeft:
; ----- load 2nd operand - number of shifts
; OUTPUT: R25:R24 = unsigned integer
; R_M3 = negative flag (0 or B7)
; C flag is set = overflow valid range
; Z flag is set = number is positive or 0 (breq), NZ = number is negative (brne)
; DESTROYS: R31, R30, R_M1..R_M10
; CALCULATOR STACK: -1
rcall CalcUnstackW
brcs CalcMulOver ; number is overflow
movw r22,r24 ; save number of shifts
; ----- load current exponent
; OUTPUT: R31:R30 (Z) = last number on calculator stack
; R25:R24 = exponent (0 = number is zero, 0xFFFF = overflow)
; ZY = number is 0
; CY = number is overflow
; DESTROYS: -
rcall CalcTopCheck ; get top number -> Z, R24
brcs CalcMulOver ; number is overflow
breq CalcDiv29 ; number is zero
; ----- shift exponent
add r24,r22
adc r25,r23
brcs CalcMulOver ; overflow
rjmp CalcDiv26 ; set new exponent
; ----------------------------------------------------------------------------
; > (C_RIGHT)
; ----------------------------------------------------------------------------
.global CalcRight
CalcRight:
; ----- load 2nd operand - number of shifts
; OUTPUT: R25:R24 = unsigned integer
; R_M3 = negative flag (0 or B7)
; C flag is set = overflow valid range
; Z flag is set = number is positive or 0 (breq), NZ = number is negative (brne)
; DESTROYS: R31, R30, R_M1..R_M10
; CALCULATOR STACK: -1
rcall CalcUnstackW
brcs CalcMulOver ; number is overflow
movw r22,r24 ; save number of shifts
; ----- load current exponent
; OUTPUT: R31:R30 (Z) = last number on calculator stack
; R25:R24 = exponent (0 = number is zero, 0xFFFF = overflow)
; ZY = number is 0
; CY = number is overflow
; DESTROYS: -
rcall CalcTopCheck ; get top number -> Z, R24
brcs CalcMulOver ; number is overflow
breq CalcDiv29 ; number is zero
; ----- shift exponent
sub r24,r22
sbc r25,r23
brcc CalcDiv26 ; set new exponent
clr r24
clr r25 ; number will be 0
rjmp CalcDiv26 ; set new exponent
; ===== here is rest of CalcDiv function
; ----- 2nd number is 0
CalcDiv6:
; INPUT and OUTPUT: R31:R30 (Z), R27:R26 (X) = registers to exchange
; DESTROYS: -
rcall ExcXZ ; exchange pointers
; INPUT: R23 = previous sign flag
; R31:R30 (Z) = pointer to the number
; R_ZERO = 0
; OUTPUT: R23 = new sign flag
; flag Z = number is zero (and C is not set)
; flag C = number is overflow (and Z is not set)
; DESTROYS: R24, R25
rcall CalcPrepMul ; prepare 1st number
breq CalcDiv7 ; 1st number is 0, result will be 1
; ----- 1st number is not zero, result will be overflow
CalcDiv62:
rjmp CalcMul62
; ----- result is 1 with error
CalcDiv7:
rjmp CalcMul7
; ----- 2nd number is overflow
CalcDiv81:
; INPUT and OUTPUT: R31:R30 (Z), R27:R26 (X) = registers to exchange
; DESTROYS: -
rcall ExcXZ ; exchange pointers
; INPUT: R23 = previous sign flag
; R31:R30 (Z) = pointer to the number
; R_ZERO = 0
; OUTPUT: R23 = new sign flag
; flag Z = number is zero (and C is not set)
; flag C = number is overflow (and Z is not set)
; DESTROYS: R24, R25
rcall CalcPrepMul ; prepare 1st number
brcs CalcDiv7 ; 1st number is overflow, result will be 1
; ----- result will be 0
CalcDiv82:
rjmp CalcMul82
; ----------------------------------------------------------------------------
; Division (C_DIV)
; ----------------------------------------------------------------------------
; DESTROYS: all
; CALCULATOR STACK: -1
; ----------------------------------------------------------------------------
.global CalcDiv
CalcDiv:
; ----- get last 2 numbers (1st -> X, 2nd -> Z)
; OUTPUT: R27:R26 (X) = pre-last number on calculator stack
; R31:R30 (Z) = last number on calculator stack
; DESTROYS: -
rcall CalcTop2
; ----- prepare numbers for division; -> 1st number Z, 2nd number X, sign bit R_MS (not masked)
; INPUT: R23 = previous sign flag
; R31:R30 (Z) = pointer to the number
; R_ZERO = 0
; OUTPUT: R23 = new sign flag
; flag Z = number is zero (and C is not set)
; flag C = number is overflow (and Z is not set)
; DESTROYS: R24, R25
clr r23 ; prepare sign flag = 0
rcall CalcPrepMul ; prepare 2nd number
brcs CalcDiv81 ; 2nd number is overflow
breq CalcDiv6 ; 2nd number is zero
; INPUT and OUTPUT: R31:R30 (Z), R27:R26 (X) = registers to exchange
; DESTROYS: -
rcall ExcXZ ; exchange pointers
; INPUT: R23 = previous sign flag
; R31:R30 (Z) = pointer to the number
; R_ZERO = 0
; OUTPUT: R23 = new sign flag
; flag Z = number is zero (and C is not set)
; flag C = number is overflow (and Z is not set)
; DESTROYS: R24, R25
rcall CalcPrepMul ; prepare 1st number
brcs CalcDiv62 ; 1st number is overflow, result is overflow
breq CalcDiv82 ; 1st number is zero, result will be 0
mov R_MS,r23 ; sign flag
; ----- fetch 2 numbers and delete 2nd number:
; 1st number Z -> R_M1..R_M10
; 2nd number X -> R_N1..R_N10
; INPUT: R31:R30 (Z) = pointer to 1st number
; R27:R26 (X) = pointer to 2nd number
; OUTPUT: R_M1..R_M10 = 1st number (from Z)
; R_N1..R_N10 = 2nd number (from X)
; DESTROYS: R31, R30, R27, R26
; CALCULATOR STACK: -1
rcall CalcFetch2
; ----- division of mantissas, dividend R_M3..R_M10 / divisor R_N3..R_N10 -> quotient R_R3..R_R10
push R_MS ; push result sign
; push registers
push r28
push r29
; prepare loop counter
ldi r31,-(MANT_BITS+1) ; negative 'number of loops + 1'
mov r1,r31 ; loop counter
; clear quotient result LOW
clr R_R10
clr r0 ; accumulator of 2 extra bits
rjmp CalcDivLoop4 ; start loop (carry is undefined, but not needed now)
CalcDivLoop:
; shift quotient left, adding result carry from right
rol R_R10
rol R_R9
rol R_R8
rol R_R7
rol R_R6
rol R_R5
rol R_R4
rol R_R3
CalcDivLoop2:
; shift dividend left
clc ; clear carry
; INPUT: R_M3..R_M10 mantissa
; C = input carry
; OUTPUT: R_M3..R_M10 mantissa shifted left
; C = output carry
; DESTROYS: -
rcall CalcMantRol ; rotate mantissa left
brcs CalcDivLoop6 ; carry - dividend is higher than divisor, subtract only (result bit will be 1)
CalcDivLoop4:
; try to subtract divisor from dividend
; INPUT: R_M3..R_M10 mantissa 1
; R_N3..R_N10 mantissa 2
; OUTPUT: R_M3..R_M10 result
; C = output borrow
; DESTROYS: -
rcall CalcMantSub
brcc CalcDivLoop8 ; no cary, no restore (dividend is not smaller than divisor, result bit will be 1)
; carry is set - dividend is smaler than divisor, restore dividend (result bit will be 0)
; INPUT: R_M3..R_M10 mantissa 1
; R_N3..R_N10 mantissa 2
; OUTPUT: R_M3..R_M10 result
; C = output carry
; DESTROYS: -
rcall CalcMantAdd
clc ; clear carry flag
rjmp CalcDivLoop9 ; result bit will be 0
CalcDivLoop6:
; subtract only, result bit will be 1
; INPUT: R_M3..R_M10 mantissa 1
; R_N3..R_N10 mantissa 2
; OUTPUT: R_M3..R_M10 result
; C = output borrow
; DESTROYS: -
rcall CalcMantSub
CalcDivLoop8:
; result bit is 1
sec ; set carry flag, result bit is 1
CalcDivLoop9:
; loop counter
inc r1 ; loop counter (C stays untouched)
brmi CalcDivLoop ; do MANT_BITS loops, while loop counter is negativ
; one extra loop, to get one extra bit (we already have one bit in carry)
rol r0 ; save last 2 bits
tst r1 ; check loop counter
breq CalcDivLoop2 ; one extra loop
; ----- difference of exponents (warning R_M11 = R_N1)
sub R_M2,R_N2 ; difference of exponents
sbc R_M1,R_N1
sbc r1,r1 ; high carry
; ----- prepare extra result byte -> R_M11 (extra 2 bits)
mov R_M11,r0 ; extra bits accumulator
ror R_M11
ror R_M11
ror R_M11 ; sihft last 2 bits to position 6 and 7
; ----- prepare mantissa -> R_M3..R_M10
movw R_M10,R_R10
movw R_M8,R_R8
movw R_M6,R_R6
movw R_M4,R_R4
; ----- prepare exponent -> R1:R_EXH:R_EXL
ldi r28,-1
movw R_EXL,R_M2 ; exponent
subi R_EXL,lo8(-EXP_BIAS) ; add exponent bias
sbci R_EXH,hi8(-EXP_BIAS)
sbc r1,r28
mov R_MT,r1 ; exponent HIGH
; ----- pop registers
pop r29
pop r28
clr R_ZERO ; restore R1
; ----- prepare sign -> R_MS
pop R_MS ; pop result sign
; ----- normalize number: R_MS sign, R_M3..R_M10(:R_M11) mantissa, R_MT:R_EXH:R_EXL exponent
rjmp CalcMulNorm
; ----------------------------------------------------------------------------
; Modulus with trunc rounding (C_MOD)
; ----------------------------------------------------------------------------
; DESTROYS: stack, R31, R30
; ----------------------------------------------------------------------------
; - result has same sign as divisor
.global CalcMod
CalcMod:
rcall Calc
.byte C_DUP2 ; pre-duplicate (a,b,a)
.byte C_DUP2 ; pre-duplicate (a,b,a,b)
.byte C_DIV ; division (a,b,a/b)
.byte C_TRUNC ; trunc (a,b,trunc(a/b))
.byte C_MUL ; multiply (a,b*trunc(a/b))
.byte C_SUB ; subtract (a-b*trunc(a/b))
.byte C_END ; end
.balign 2 ; align
ret
; ----------------------------------------------------------------------------
; Modulus with floor rounding (C_MOD2)
; ----------------------------------------------------------------------------
; DESTROYS: stack, R31, R30
; ----------------------------------------------------------------------------
; - result has same sign as dividend
.global CalcMod2
CalcMod2:
rcall Calc
.byte C_DUP2 ; pre-duplicate (a,b,a)
.byte C_DUP2 ; pre-duplicate (a,b,a,b)
.byte C_DIV ; division (a,b,a/b)
.byte C_FLOOR ; floor (a,b,floor(a/b))
.byte C_MUL ; multiply (a,b*floor(a/b))
.byte C_SUB ; subtract (a-b*floor(a/b))
.byte C_END ; end
.balign 2 ; align
ret
; ----------------------------------------------------------------------------
; Prepare number for addition and bit operation
; ----------------------------------------------------------------------------
; INPUT: R31:R30 (Z) = pointer to the number
; OUTPUT: R_R9:R_R10 = exponent
; DESTROYS: R_M1..R_M10
; ----------------------------------------------------------------------------
; Clear exponent and expand mantissa to 9 bytes signed.
CalcPrepAdd:
; ----- load number -> R_M1..R_M10
; INPUT: R31:R30 (Z) = pointer to number
; OUTPUT: R_M1..R_M10 number
; DESTROYS: -
rcall CalcLoadNum
; ----- get exponent and test for zero, clear 2nd byte - next highest byte of mantissa
movw R_R10,R_M2 ; exponent LOW and HIGH
clr R_M2 ; clear exponent LOW byte
adiw R_R10,0 ; zero number?
breq CalcPrepAdd9 ; number is zero
; ----- check sign and restore highest bit of the mantissa
bst R_M3,7 ; load sign bit into T flag
ori R_M3,B7 ; restore hidden highest bit of the mantissa
brtc CalcPrepAdd8 ; number is positive, all ok
; ----- negate extended mantissa (R_M2..R_M10)
; INPUT: R_M2..R_M10 extended mantissa
; R_ZERO = 0
; OUTPUT: R_M2..R_M10 extended mantissa negated
; CY = carry set if result is not 0
; DESTROYS: -
rcall CalcMantNeg ; negate number R_M2..R_M10
; ----- save number
CalcPrepAdd8:
; INPUT: R31:R30 (Z) = pointer to number
; R_M1..R_M10 number
; DESTROYS: -
rjmp CalcSaveNum
; ----- number is 0 (need to clear - mantissa may be not zeroed)
; OUTPUT: R_M1..R_M10 number (= 0)
; DESTROYS: -
CalcPrepAdd9:
rcall CalcClearNum ; clear number R_M1..R_M10
; INPUT: R31:R30 (Z) = pointer to number
; R_M1..R_M10 number
; DESTROYS: -
rjmp CalcSaveNum
; ----------------------------------------------------------------------------
; Shift 1st number right
; ----------------------------------------------------------------------------
; INPUT: R_R9:R_R10 = number of shifts
; R_M2..R_M10 = extended mantissa (R_M2=0x00 or 0xFF)
; DESTROYS: R_R10
; ----------------------------------------------------------------------------
CalcAddShift:
; ----- check number of shifts
adiw R_R10,0 ; check zero
breq CalcAddShift9 ; no shift needed
; ----- check overflow
tst R_R9 ; check number of shifts HIGH
brne CalcAddShift6 ; overflow, clear number R_M2..R_M10
cpi R_R10,MANT_BITS+1 ; max. size of mantissa
brcc CalcAddShift6 ; overflow difference, clear the number R_M2..R_M10
; ----- rotate mantissa right
CalcAddShift2:
asr R_M2 ; bit 7 unchanged
; INPUT: R_M3..R_M10 mantissa
; C = input carry
; OUTPUT: R_M3..R_M10 mantissa shifted right
; C = output carry
; DESTROYS: -
rcall CalcMantRor ; rotate mantissa right (with carry)
dec R_R10
brne CalcAddShift2
; ----- round up, if last shiftet bit was set
; INPUT: R_M3..R_M10 = mantissa
; OUTPUT: returns Z flag if result is 0 (overflow from 0xFF 0xFF 0xFF 0xFF 0xFF)
brcc CalcAddShift9 ; no carry bits
; INPUT: R_M3..R_M10 = mantissa
; R_ZERO = 0
; OUTPUT: returns Z flag if result is 0 (overflow from 0xFF 0xFF 0xFF...)
; DESTROYS: -
rcall CalcMantInc ; increment mantissa
sbci R_M2,0xff
brne CalcAddShift9 ; no overflow
CalcAddShift6:
; OUTPUT: R_M2..R_M10 number (= 0)
; DESTROYS: -
rjmp CalcClearNum2 ; clear result R_M2..R_M10
CalcAddShift9:
ret
; ----------------------------------------------------------------------------
; Subtraction (C_SUB)
; ----------------------------------------------------------------------------
; DESTROYS: all
; CALCULATOR STACK: -1
; ----------------------------------------------------------------------------
.global CalcSub
CalcSub:
; ----- negate second number
; DESTROYS: R31, R30, R25, R24
rcall CalcNeg
; CalcAdd must follow
; ----------------------------------------------------------------------------
; Addition (C_ADD)
; ----------------------------------------------------------------------------
; DESTROYS: all
; CALCULATOR STACK: -1
; ----------------------------------------------------------------------------
.global CalcAdd
CalcAdd:
; ----- get last 2 numbers (1st -> X, 2nd -> Z)
; OUTPUT: R27:R26 (X) = pre-last number on calculator stack
; R31:R30 (Z) = last number on calculator stack
; DESTROYS: -
rcall CalcTop2
; ----- prepare numbers for addition; 1st exponent -> R_R9:R_R10/Z, 2nd exponent -> R_N9:R_N10/X
; INPUT: R31:R30 (Z) = pointer to the number
; OUTPUT: R_R9:R_R10 = exponent
; DESTROYS: R_M1..R_M10
rcall CalcPrepAdd ; prepare 2nd number
movw R_N10,R_R10 ; save 2nd exponent -> R_N9:R_N10
; INPUT and OUTPUT: R31:R30 (Z), R27:R26 (X) = registers to exchange
; DESTROYS: -
rcall ExcXZ ; exchange pointers
rcall CalcPrepAdd ; prepare 1st number, 1st exponent -> R_R9:R_R10
; ----- sort numbers - 2nd exponent must not be less than 1st one
cp R_N10,R_R10 ; compare exponents
cpc R_N9,R_R9 ; compare exponents
brcc CalcAdd2 ; 2nd exponent is higher or equal, it is OK
; INPUT and OUTPUT: R31:R30 (Z), R27:R26 (X) = registers to exchange
; DESTROYS: -
rcall ExcXZ ; exchange pointers
eor R_R10,R_N10 ; exchange exponents
eor R_N10,R_R10
eor R_R10,R_N10
eor R_R9,R_N9 ; exchange exponents
eor R_N9,R_R9
eor R_R9,R_N9
CalcAdd2:
; ----- fetch 2 numbers and delete 2nd number:
; 1st number Z -> (R_R9:R_R10) R_M1..R_M10
; 2nd number X -> (R_R7:R_R8) R_N1..R_N10
; INPUT: R31:R30 (Z) = pointer to 1st number
; R27:R26 (X) = pointer to 2nd number
; OUTPUT: R_M1..R_M10 = 1st number (from Z)
; R_N1..R_N10 = 2nd number (from X)
; DESTROYS: R31, R30, R27, R26
; CALCULATOR STACK: -1
push R_N9
push R_N10
rcall CalcFetch2
pop R_R8
pop R_R7
; ----- save new exponent (= exponent of 2nd number) -> R_EXH:R_EXL
; ... already R_EXH:R_EXL = R_R7:R_R8
; movw R_EXL,R_R8
; ----- difference of exponents (= 2nd exponent - 1st exponent; result is >= 0) -> R_R9:R_R10
sub R_R10,R_R8 ; 1st exponent - 2nd exponent
sbc R_R9,R_R7
com R_R9
neg R_R10
sbci R_R9,0xff ; negate difference
; ----- shift 1st number right to normalize numbers to the same exponent
; INPUT: R_R9:R_R10 = number of shifts
; R_M2..R_M10 = extended mantissa (R_M2=0x00 or 0xFF)
; DESTROYS: R_R10
rcall CalcAddShift
; ----- add both numbers (here is R_M2=0x00 or 0xFF, R_N2=0x00 or 0xFF)
; INPUT: R_M3..R_M10 mantissa 1
; R_N3..R_N10 mantissa 2
; OUTPUT: R_M3..R_M10 result
; C = output carry
; DESTROYS: -
rcall CalcMantAdd ; add mantissas
adc R_M2,R_N2
; ----- negate result
mov R_MS,R_M2 ; get sign
andi R_MS,B7 ; isolate sign sign
breq CalcAdd3 ; number is not negative
; INPUT: R_M2..R_M10 extended mantissa
; R_ZERO = 0
; OUTPUT: R_M2..R_M10 extended mantissa negated
; CY = carry set if result is not 0
; DESTROYS: -
rcall CalcMantNeg ; negate mantissa
brcs CalcAdd3 ; result is not 0
rjmp CalcMulUnder ; underflow (difference is 0)
; ----- check overflow = highest byte is 0x01
CalcAdd3:
clr R_MT ; exponent HIGH = 0
tst R_M2
breq CalcAdd6 ; no overflow
; ----- overflow - shift result right and increment exponent
; INPUT: R_R9:R_R10 = number of shifts
; R_M2..R_M10 = normalized number (mantissa, R_M2=0x00 or 0xFF)
; DESTROYS: R_R10
ldi R_R10,1 ; number of shifts = 1
rcall CalcAddShift2 ; shift result right
adiw R_EXL,1 ; increment exponent R_EXH:R_EXL
adc R_MT,R_MT ; exponent HIGH
; ----- normalize number: R_MS sign, R_M3..R_M10(:R_M11) mantissa, R_MT:R_EXH:R_EXL exponent
CalcAdd6:
clr R_M11 ; R_M11 <- 0, mantissa extra lowest byte
rjmp CalcMulNorm ; normalize result
; ----------------------------------------------------------------------------
; Square root (C_SQRT)
; ----------------------------------------------------------------------------
; DESTROYS: all
; USES: TEMP_1, TEMP_2, TEMP_3, TEMP_4
; ----------------------------------------------------------------------------
.global CalcSqrt
CalcSqrt:
rcall Calc
; offset 0: number is zero, result will be zero
.byte C_JUMPZ ; jump if zero
.byte 9 ; jump to offset 5 of CalcPow (5 + 6 - 2 = 9)
; offset 2
.byte C_CONST(CONST_05) ; load constant 0.5 (x,0.5)
.byte C_END ; end