-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc-pi-st.asm
1286 lines (1238 loc) · 32.4 KB
/
calc-pi-st.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
;-------------------------------------------------------------
;
; SINGLE THREAD FLOATING POINT MULTI-PRECISION CALCULATOR
;
; Calculation of Pi using Stormer Formula
;
; File: calc-pi-st.asm
; Module: calc.asm, calc.o
; Exec: calc-pi
;
; Created 11/13/2014
; Last Edit 05/25/2015
;
;--------------------------------------------------------------
; MIT License
;
; Copyright 2014-2020 David Bolenbaugh
;
; Permission is hereby granted, free of charge, to any person obtaining a copy
; of this software and associated documentation files (the "Software"), to deal
; in the Software without restriction, including without limitation the rights
; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
; copies of the Software, and to permit persons to whom the Software is
; furnished to do so, subject to the following conditions:
; The above copyright notice and this permission notice shall be included in all
; copies or substantial portions of the Software.
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
; SOFTWARE.
;-------------------------------------------------------------
; Function_calc_pi_sto:
; Sub_Pi_ArcTan:
; Function_calc_pi_sto_FP:
;--------------------------
;-------------------------------------------------------------
;
;--------------------------
;
; Function_calc_pi_sto
;
; Pi = 176arctan(1/57) +
; + 28arctan(1/239)
; - 48arctan(1/682)
; + 96arctan(1/12943)
;
; ACC = Current Term
; OPR = Sum
; WorkA = x^(2n+1) term
;
; REG0 = 176arctan(1/57)
; REG1 = 28arctan(1/239) +
; REG2 = 48arctan(1/682) +
; REG3 = 96arctan(1/12943)
; XReg = Result
;
; Multi-tasking mode, run 4 copies of the program concurrently
; Each copy can run a separate summation, using i7 cores
; The time of calculation will be slowest sum, the first
;
; Each result saved: pi-st-1.num, pi-st-2.num, pi-st-3.num, pi-st-4.num
; Series 2,3 and 4 will exit the program when done
; Series 1 will load 3 other files and place sum in XReg
;
; Command for multitasking: c.pi.se <number> where number = 1,2,3 or 4
; If number omitted or 0, then all series summed normally
;
; For Multi-tasking, check rax on entry
; Case rax = 0, calculate all arctan series and sum
; Case rax = 1, calculate 176*arctan(1/57) Result Xreg
; Case rax = 2, calcualte 28*arctan(1/239)
; Caes rax = 3, calculate 48*arctan(1/682)
; Case rax = 4, calculate 96*arctan(1/12943)
; Case rax = 1234, retrieve sums from disk and add
;
;
;--------------------------------------------------------------
;
Function_calc_pi_sto:
; jmp Function_calc_pi_sto_FP
;
push rax
push rbx
push rcx
push rdx
push rsi
push rdi
push rbp
push r11 ; command argument (0 to 4)
push r12 ; sign flag
push r13 ; 1/X
push r14 ; 1/(X^2n-1)
push r15 ; 2n+1
;
mov r11, rax ; Save command line code
;
xor rax, rax ; clear RAX = 0
; ; Check for other (multitasking calls)
or r11, r11 ; Is R11 zero?
jnz .skip0 ; no
mov rax, .msg_pi ; Print calculaton description on screen
.skip0:
cmp r11, 1 ; Argument = 1?
jne .skip1
mov rax, .msg_pi1 ; Yes load message
.skip1:
cmp r11, 2 ; Argument = 2?
jne .skip2
mov rax, .msg_pi2 ; Yes load message
.skip2:
cmp r11, 3 ; Argument = 3?
jne .skip3
mov rax, .msg_pi3 ; Yes load message
.skip3:
cmp r11, 4 ; Argument = 4?
jne .skip4
mov rax, .msg_pi4 ; Yes load message
.skip4:
cmp r11, 1234 ; Argument = 1234?
jne .skip1234
mov rax, .msg_pi1234 ; Yes load message
.skip1234:
;
; check if message assigned, if not then invalid code
;
and rax, rax ; Is rax zero, is command invalid?
jz .invalid ; it is invalid, skip to error
call StrOut ; Else valid, print message
;
;^^^^^^^^^^^^^^^^^^^^^^ Watch Progress ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
mov rbx, [iShowCalcStep] ; print occasional samples
mov rax, 0x02000000 ; set skip counter from rbx
call ShowCalcProgress ; initialize
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
;
; If running multipart mode, then skip non-selected summations
;
cmp r11, 2 ; Skip to part 2?
je .part2
cmp r11, 3 ; Skip to part 2?
je .part3
cmp r11, 4 ; Skip to part 2?
je .part4
cmp r11, 1234 ; Skip forward to load sum from disk?
je .Code1234 ; Yes, skip to disk load
;
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;
; Summation #1 176 * arctan(1/57)
;
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
.part1:
;
; Initialize parameters of summation
mov r14, (57*57) ; 1/(x^2)
mov r13, 57 ; 1/x
; Perform Summation
call Sub_Pi_ArcTan ; Perform arctan summation
; Sum factor from formula
mov rsi, HAND_OPR
mov rax, 176
call FIX_US_Multiplication
; Save result of 1st summation
mov rsi, HAND_OPR
call Conv_FIX_to_FP ; Return X = last 1/n! term
;
cmp r11, 1 ; Only performing partial calculation?
je .MultiPart ; Yes, skip other 3 parts
;
mov rsi, HAND_OPR
mov rdi, HAND_REG0
call CopyVariable ; Reg0 = first summation
;
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;
; Summation #2 28 * arctan(1/239)
;
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
.part2:
;
; Initialize parameters of summation
mov r14, (239*239) ; 1/(x^2)
mov r13, 239 ; 1/x
; Perform Summation
call Sub_Pi_ArcTan ; Perform arctan summation
; Sum factor from formula
mov rsi, HAND_OPR
mov rax, 28
call FIX_US_Multiplication
; Save result of 1st summation
mov rsi, HAND_OPR
call Conv_FIX_to_FP ; Return X = last 1/n! term
;
cmp r11, 2 ; Only performing partial calculation?
je .MultiPart ; Yes, skip other 3 parts
;
mov rsi, HAND_OPR
mov rdi, HAND_REG1
call CopyVariable ; Reg1 = second summation
;
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;
; Summation #3 48 * arctan(1/682)
;
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
.part3:
;
; Initialize parameters of summation
mov r14, (682*682) ; 1/(x^2)
mov r13, 682 ; 1/x
; Perform Summation
call Sub_Pi_ArcTan ; Perform arctan summation
; Sum factor from formula
mov rsi, HAND_OPR
mov rax, 48
call FIX_US_Multiplication
; Save result of 1st summation
mov rsi, HAND_OPR
call Conv_FIX_to_FP ; Return X = last 1/n! term
call FP_TwosCompliment ; OPR = ( - Series 3)
;
cmp r11, 3 ; Only performing partial calculation?
je .MultiPart ; Yes, skip other 3 parts
;
mov rsi, HAND_OPR
mov rdi, HAND_REG2
call CopyVariable ; Reg2 = third summation
;
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;
; Summation #4 96 * arctan(1/12943)
;
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
.part4:
;
; Initialize parameters of summation
mov r14, (12943*12943) ; 1/(x^2)
mov r13, 12943 ; 1/x
; Perform Summation
call Sub_Pi_ArcTan ; Perform arctan summation
; Sum factor from formula
mov rsi, HAND_OPR
mov rax, 96
call FIX_US_Multiplication
; Save result of 1st summation
mov rsi, HAND_OPR
call Conv_FIX_to_FP ; Return X = last 1/n! term
;
cmp r11, 4 ; Only performing partial calculation?
je .MultiPart ; Yes, skip other 3 parts
;
mov rsi, HAND_OPR
mov rdi, HAND_REG3
call CopyVariable ; Reg3 = fourth summation
;
; Done collect summation of 4 registers
;
mov rsi, HAND_REG0
mov rdi, HAND_OPR
call CopyVariable ; OPR = Series 1
mov rsi, HAND_REG1
mov rdi, HAND_ACC
call CopyVariable ; ACC = Series 2
call FP_Addition ; ACC = Series 1+2
mov rsi, HAND_REG2
mov rdi, HAND_OPR
call CopyVariable ; OPR = Series 3
mov rsi, HAND_OPR
; call FP_TwosCompliment ; OPR = ( - Series 3)
call FP_Addition ; ACC = Series 1+2-3
mov rsi, HAND_REG3
mov rdi, HAND_OPR
call CopyVariable ; OPR = Series 3
call FP_Addition ; ACC = Series 1+2-3+4
;
; Save result in XREg
;
mov rsi, HAND_ACC
mov rdi, HAND_XREG
call CopyVariable ; X = sum (value of e)
;
; Clear temporary variables
;
; mov rsi, HAND_REG0
; call ClearVariable
; mov rsi, HAND_REG1
; call ClearVariable
; mov rsi, HAND_REG2
; call ClearVariable
; mov rsi, HAND_REG3
; call ClearVariable
jmp .Not_Multi ; calculation of all 4, skip
;-------------------------------------------
; Endi fo single calculation part
; next sections is cleanup from multi-part
;-------------------------------------------
;
; Case of calculate only 1 part, result in XReg
;
.MultiPart:
mov rsi, HAND_OPR
mov rdi, HAND_XREG
call CopyVariable ; Move to XReg to save to disk
;
; Print elapsed time of part before jmp ProgramExit
;
;^^^^^^^^^^^^^^^^^^^^^^ Watch Progress ^^^^^^^^^^^^^^^^^^^
; -Print-- Inc/Rset -Format-
mov rax, (0x000F0000 | 0x00000000 | 0x30000000 )
call ShowCalcProgress
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
cmp r11, 1 ; part 1 ?
jne .notFN1 ; Yes, case of 2nd sum
mov rax, .fname1
call SaveVariable ; Save the result in binary
jmp ProgramExit ; Exit the program
.notFN1:
cmp r11, 2 ; part 1 ?
jne .notFN2 ; Yes, case of 2nd sum
mov rax, .fname2
call SaveVariable ; Save the result in binary
jmp ProgramExit ; Exit the program
.notFN2:
cmp r11, 3 ; part 3 ?
jne .notFN3
mov rax, .fname3
call SaveVariable ; Save the result in binary
jmp ProgramExit ; Exit the program
.notFN3:
cmp r11, 4 ; part 4 ?
jne .notFN4
mov rax, .fname4
call SaveVariable ; Save the result in binary
jmp ProgramExit ; Exit the program
.notFN4:
.invalid:
mov rax, .ferrormsg2 ; get error message
call StrOut
call CROut
jmp .exit
.ferror:
mov rax, .ferrormsg ; get error message
call StrOut
call CROut
jmp .exit
.ferrormsg:
db 0xD, 0xA, "Error: c.pi.st loaded zero result, possible file error", 0xD, 0xA, 0
.ferrormsg2:
db 0xD, 0xA, "Error: c.pi.st invalid option code.", 0xD, 0xA, 0
jmp .exit ; should not reach this
;
; Case of rax code = 1234, tetrieve sum
;
;
; Get result #1
;
.Code1234:
mov rsi, HAND_ACC ; Clear ACC for input buffer
call ClearVariable ; Call clear variable
mov rax, .fname1 ; Get filename
call LoadVariable ; Load variable into ACC from file
mov rax, [FP_Acc+MAN_MSB_OFST] ; M.S.Byte
or rax, rax ; Error check, is MSByte zero?
jz .ferror ; No, non-zero number assumed valid
;
; Add result 2
;
mov rsi, HAND_ACC
mov rdi, HAND_OPR
call CopyVariable
mov rsi, HAND_ACC ; Clear ACC for input buffer
call ClearVariable ; Call clear variable
mov rax, .fname2 ; Get filename
call LoadVariable ; Load variable into ACC from file
mov rax, [FP_Acc+MAN_MSB_OFST] ; M.S.Byte
or rax, rax ; Error check, is MSByte zero?
jz .ferror ; No, non-zero number assumed valid
call FP_Addition ; Add ACC + OPR
mov rsi, HAND_ACC
mov rdi, HAND_OPR
call CopyVariable ; ACC --> OPR
mov rsi, HAND_ACC ; Clear ACC for input buffer
call ClearVariable ; Call clear variable
mov rax, .fname3 ; Get filename
call LoadVariable ; Load variable into ACC from file
mov rax, [FP_Acc+MAN_MSB_OFST] ; M.S.Byte
or rax, rax ; Error check, is MSByte zero?
jz .ferror ; No, non-zero number assumed valid
call FP_Addition ; Add ACC + OPR
mov rsi, HAND_ACC
mov rdi, HAND_OPR
call CopyVariable ; ACC --> OPR
mov rsi, HAND_ACC ; Clear ACC for input buffer
call ClearVariable ; Call clear variable
mov rax, .fname4 ; Get filename
call LoadVariable ; Load variable into ACC from file
mov rax, [FP_Acc+MAN_MSB_OFST] ; M.S.Byte
or rax, rax ; Error check, is MSByte zero?
jz .ferror ; No, non-zero number assumed valid
call FP_Addition ; Add ACC + OPR
mov rsi, HAND_ACC
mov rdi, HAND_XREG
call CopyVariable ; Final result ACC --> XREG
;^^^^^^^^^^^^^^^^^^^^^^ Watch Progress ^^^^^^^^^^^^^^^^^^^
; -Print-- Inc/Rset -Format-
mov rax, (0x000F0000 | 0x00000000 | 0x30000000 )
call ShowCalcProgress
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
jmp .exit
.Not_Multi:
;^^^^^^^^^^^^^^^^^^^^^^ Watch Progress ^^^^^^^^^^^^^^^^^^^
; -Print-- Inc/Rset -Format-
mov rax, (0x00033333 | 0x00000000 | 0x30000000 )
call ShowCalcProgress
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.exit:
pop r15
pop r14
pop r13
pop r12
pop r11
pop rbp
pop rdi
pop rsi
pop rdx
pop rcx
pop rbx
pop rax
ret
.msg_pi: db "Function_calc_pi_sto: Calculating pi Stormer method.", 0xD, 0xA, 0
.msg_pi1: db "Part 1 of 4 --> 176 * arctan(1/57)", 0xD, 0xA, 0
.msg_pi2: db "Part 2 of 4 --> 28 * arctan(1/239)", 0xD, 0xA, 0
.msg_pi3: db "Part 3 of 4 --> 48 * arctan(1/682)", 0xD, 0xA, 0
.msg_pi4: db "Part 4 of 4 --> 96 * arctan(1/12943)", 0xD, 0xA, 0
.msg_pi1234: db "c.pi.st loading sum from disk", 0xD, 0xA, 0
.fname1: db "pi-st-1", 0
.fname2: db "pi-st-2", 0
.fname3: db "pi-st-3", 0
.fname4: db "pi-st-4", 0
;------------------------------
; Subroutine for Arctan summation
;------------------------------
;
Sub_Pi_ArcTan:
;
;^^^^^^^^^^^^^^^^^^^^^^ Watch Progress ^^^^^^^^^^^^^^^^^^^
; -Print-- Inc/Rset -Format-
mov rax, (0x04000000 | 0x00008808 | 0x00000000 )
call ShowCalcProgress
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
;
; Initialize FIX Variables
;
mov r15, 1 ; n = 1
mov r12, 0 ; sign counter
mov rsi, HAND_ACC
mov rax, 0
call FIX_Load64BitNumber ; ACC = 1
;
mov rsi, HAND_WORKA
mov rax, 1
call FIX_Load64BitNumber ; ACC = 1
mov rax, r13
call FIX_US_Division ; ACC = 1/57 (or next value)
;
mov rsi, HAND_OPR
mov rax, 1
call FIX_Load64BitNumber ; ACC = 1
mov rax, r13
call FIX_US_Division ; ACC = 1/57 (or next value)
;
; Main loop
;
.loop:
;
; Increment n and check for limits
;
inc r12 ; Sign counter
inc r15 ; Increment (2n+1)
inc r15
mov rax, r15
rcl rax, 1 ; Bit 63 not allowed
jc .n_overflow_error
rcl rax, 1 ; Bit 62 not allowed
jc .n_overflow_error
;
; Calculate next X^(2n+1) numerator
;
mov rax, r14 ; Pass to division routine
mov rsi, HAND_WORKA
call FIX_US_Division ; Divide for numerator
; Calculate divide (2n+1)
;
mov rsi, HAND_WORKA
mov rdi, HAND_ACC
call CopyVariable ; Acc = X^(2n+1)
mov rax, r15 ; RAX = (2n+1)
mov rsi, HAND_ACC
call FIX_US_Division ; ACC = x^(2n+1)/(2n+1)
; Add term to summation
;
mov rax, r12 ; get sign counter
test rax, 1 ; L.S. bit 1?
jnz .skip_add
mov rsi, HAND_OPR
mov rdi, HAND_ACC
call FIX_Addition ; OPR = OPR + ACC
jmp .skip_sub
.skip_add:
mov rsi, HAND_OPR
mov rdi, HAND_ACC
call FIX_Subtraction ; OPR = OPR - ACC
.skip_sub:
;
;
;^^^^^^^^^^^^^^^^^^^^^^ Watch Progress ^^^^^^^^^^^^^^^^^^^
; -Print-- Inc/Rset -Format-
mov rax, (0x01311111 | 0x00000444 | 0x60000000 )
call ShowCalcProgress
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
;
; Check if done
;
mov rsi, HAND_ACC
call FIX_Check_Sum_Done
or rax, rax ; Result = RAX, 1 = done
jz .loop
;www
; cmp r12, 1
; jl .loop
;
; Loop is finished because last term added not significant
;
;^^^^^^^^^^^^^^^^^^^^^^ Watch Progress ^^^^^^^^^^^^^^^^^^^
; -Print-- Inc/Rset -Format-
mov rax, (0x00033333 | 0x00000000 | 0x30000000 )
call ShowCalcProgress
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ret
.n_overflow_error:
mov rax, .errorMsg1 ; Print the error message
call StrOut
mov rax, 0 ; Error message already printed
jmp FatalError ; Unrecoverble, exit program
.errorMsg1: db "Error: Summation error, n overflow", 0xD, 0xA, 0
;--------------------------------------------------------------
; Floating Point Method
;
; Pi = 176arctan(1/57) +
; 28arctan(1/239) +
; 48arctan(1/682) +
; 96arctan(1/12943)
;
; XREG = Sum
; YREG = Term
; ZREG = n
; REG0 = 176arctan(1/57)
; REG1 = 28arctan(1/239) +
; REG2 = 48arctan(1/682) +
; REG3 = 96arctan(1/12943)
;
;--------------------------------------------------------------
;
Function_calc_pi_sto_FP:
push rax
push rbx
push rcx
push rdx
push rbp
push rsi
push rdi
push r8 ; Used as (-1)^n counter
mov rax, .msg_title ; Print calculaton on screen
call StrOut
;
;^^^^^^^^^^^^^^^^^^^^^^ Watch Progress ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
mov rbx, [iShowCalcStep] ; print each 100 term
mov rax, 0x02000000 ; set skip counter from RBX
call ShowCalcProgress ; initialize
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
;
; Clear Registers REG0, REG1, REG2, and REG3
;
mov rsi, HAND_REG0
call ClearVariable
mov rsi, HAND_REG1
call ClearVariable
mov rsi, HAND_REG2
call ClearVariable
mov rsi, HAND_REG3
call ClearVariable
;
;-------------------------------
;
; 1st Summation starts here 176 * arctan(1/57)
;
;-------------------------------
;
; Used to reduce accuracy during divisions
;
call ClearGrabAccuracy ; initialize reduced accuracy variables
;
; Load 1/57 to XREG, YREG
mov rsi, HAND_OPR
call SetToOne ; OPR = 1
mov rsi, HAND_ACC
call ClearVariable ; ACC = 0
mov rbx, FP_Acc+MAN_MSW_OFST
mov rax, 0x7200000000000000 ; Mantissa for 57
mov [rbx], rax
mov rbx, FP_Acc+EXP_WORD_OFST
mov rax, 0x0000000000000006 ; Exponient for 57
mov [rbx], rax
call FP_Division ; ACC = 1/57
mov rsi, HAND_ACC
mov rdi, HAND_XREG
call CopyVariable ; XREG = 1/57 (sum)
mov rdi, HAND_YREG
call CopyVariable ; YREG = 1/57 (term)
;
; Set n counter in ZREG to 1, for 1,3,5 ...
;
mov rsi, HAND_ZREG
call SetToOne ; ZREG = 1 (n counter)
;
; Init sign counter for (-1)^n
;
mov r8, 0
;
; * * * * * * * * * * * * * * * * * * * * * * * * * *
;
; M A I N L O O P 1 176 * arctan(1/57)
;
; * * * * * * * * * * * * * * * * * * * * * * * * * *
;
.loop1:
;
; Get last term and multiply by X^2
;
mov rsi, HAND_YREG
mov rdi, HAND_OPR
call CopyVariable ; OPR = last term
mov rsi, HAND_ACC
call ClearVariable ; ACC = 0
mov rbx, FP_Acc+MAN_MSW_OFST
mov rax, 0x6588000000000000 ; Mantissa X*X
mov [rbx], rax
mov rbx, FP_Acc+EXP_WORD_OFST
mov rax, 0x000000000000000C ; Exponient X*X
mov [rbx], rax ; ACC = ((57*57)=3249
call ReduceSeriesAccuracy
call FP_Division ; ACC = term / (x*x)
call RestoreFullAccuracy
mov rsi, HAND_ACC
mov rdi, HAND_YREG
call CopyVariable ; YREG = (n+1) term
;
; Add 2 to n
;
mov rsi, HAND_ZREG
mov rdi, HAND_OPR
call CopyVariable ; OPR = last n
mov rsi, HAND_ACC
call SetToTwo ; ACC = 2
call FP_Addition ; ACC = n+1
mov rsi, HAND_ACC
mov rdi, HAND_ZREG
call CopyVariable ; ZREG = n+1
;
; Move last term and divide by n+2
;
mov rsi, HAND_YREG
mov rdi, HAND_OPR
call CopyVariable ; OPR = last term
call ReduceSeriesAccuracy
call FP_Division ; ACC = term / (2n-1)
call RestoreFullAccuracy
;
; If odd term change sign
;
inc r8 ; sign counter
test r8, 1 ; is bit 1 positive
jz .skip1
mov rsi, HAND_ACC
call FP_TwosCompliment ; Change sign
;
; get sum and add term
;
.skip1:
mov rsi, HAND_XREG
mov rdi, HAND_OPR
call CopyVariable
call FP_Addition
call GrabSeriesAccuracy
mov rsi, HAND_ACC
mov rdi, HAND_XREG
call CopyVariable
;
;^^^^^^^^^^^^^^^^^^^^^^ Watch Progress ^^^^^^^^^^^^^^^^^^^
; -Print-- Inc/Rset -Format-
mov rax, (0x01311101 | 0x00000844 | 0x60000000 )
call ShowCalcProgress
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
;
; Check if last term was not significant
;
mov rax, [Nearly_Zero] ; Check for done
or rax, rax
jnz .done1 ; Done go exit
jmp .loop1 ; Else loop back
;------------------------------------------------------------------
;
; E X I T L O O P 1
;
;------------------------------------------------------------------
.done1:
;
; Mult by 176 and save in REG0
;
mov rsi, HAND_ACC
mov rdi, HAND_OPR
call CopyVariable ; OPR = completed sum
mov rsi, HAND_ACC
call ClearVariable ; ACC = 0
mov rbx, FP_Acc+MAN_MSW_OFST
mov rax, 0x5800000000000000 ; Mantissa X*X
mov [rbx], rax
mov rbx, FP_Acc+EXP_WORD_OFST
mov rax, 0x0000000000000008 ; Exponient X*X
mov [rbx], rax ; ACC = 176
call FP_Multiplication ; ACC = sum * 176
mov rsi, HAND_ACC
mov rdi, HAND_REG0 ; REG0 = first result
call CopyVariable
;-------------------------------
;
; 2nd Summation Starts here 28arctan(1/239)
;
;-------------------------------
;
; Used to reduce accuracy during divisions
;
call ClearGrabAccuracy ; initialize reduced accuracy variables
;^^^^^^^^^^^^^^^^^^^^^^ Watch Progress ^^^^^^^^^^^^^^^^^^^
; -Print-- Inc/Rset -Format-
mov rax, (0x00033303 | 0x00008808 | 0x30000000 )
call ShowCalcProgress
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
;
; Load 1/239 to XREG, YREG
mov rsi, HAND_OPR
call SetToOne ; OPR = 1
mov rsi, HAND_ACC
call ClearVariable ; ACC = 0
mov rbx, FP_Acc+MAN_MSW_OFST
mov rax, 0x7780000000000000 ; Mantissa for X
mov [rbx], rax
mov rbx, FP_Acc+EXP_WORD_OFST
mov rax, 0x0000000000000008 ; Exponient for X
mov [rbx], rax
call FP_Division ; ACC = 1/239
mov rsi, HAND_ACC
mov rdi, HAND_XREG
call CopyVariable ; XREG = 1/X (sum)
mov rdi, HAND_YREG
call CopyVariable ; YREG = 1/X (term)
;
; Set n counter in ZREG to 1, for 1,3,5 ...
;
mov rsi, HAND_ZREG
call SetToOne ; ZREG = 1 (n counter)
;
; Init sign counter for (-1)^n
;
mov r8, 0
;
; * * * * * * * * * * * * * * * * * * * * * * * * *
;
; M A I N L O O P 2 28*arctan(1/239)
;
; * * * * * * * * * * * * * * * * * * * * * * * * *
;
.loop2:
;
; Get last term and multiply by X^2
;
mov rsi, HAND_YREG
mov rdi, HAND_OPR
call CopyVariable ; OPR = last term
mov rsi, HAND_ACC
call ClearVariable ; ACC = 0
mov rbx, FP_Acc+MAN_MSW_OFST
mov rax, 0x6F90800000000000 ; Mantissa X*X
mov [rbx], rax
mov rbx, FP_Acc+EXP_WORD_OFST
mov rax, 0x0000000000000010 ; Exponient X*X
mov [rbx], rax ; ACC = ((239*239)=56882
call ReduceSeriesAccuracy
call FP_Division ; ACC = term / (x*x)
call RestoreFullAccuracy
mov rsi, HAND_ACC
mov rdi, HAND_YREG
call CopyVariable ; YREG = (n+1) term
;
; Add 2 to n
;
mov rsi, HAND_ZREG
mov rdi, HAND_OPR
call CopyVariable ; OPR = last n
mov rsi, HAND_ACC
call SetToTwo ; ACC = 2
call FP_Addition ; ACC = n+1
mov rsi, HAND_ACC
mov rdi, HAND_ZREG
call CopyVariable ; ZREG = n+1
;
; Move last term and divide by n+2
;
mov rsi, HAND_YREG
mov rdi, HAND_OPR
call CopyVariable ; OPR = last term
call ReduceSeriesAccuracy
call FP_Division ; ACC = term / (2n-1)
call RestoreFullAccuracy
;
; If odd term change sign
;
inc r8 ; sign counter
test r8, 1 ; is bit 1 positive
jz .skip2
mov rsi, HAND_ACC
call FP_TwosCompliment ; Change sign
;
; get sum and add term
;
.skip2:
mov rsi, HAND_XREG
mov rdi, HAND_OPR
call CopyVariable
call FP_Addition
call GrabSeriesAccuracy
mov rsi, HAND_ACC
mov rdi, HAND_XREG
call CopyVariable
;
;^^^^^^^^^^^^^^^^^^^^^^ Watch Progress ^^^^^^^^^^^^^^^^^^^
; -Print-- Inc/Rset -Format-
mov rax, (0x01311101 | 0x00000844 | 0x60000000 )
call ShowCalcProgress
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
;
; Check if last term was not significant
;
mov rax, [Nearly_Zero] ; Check for done
or rax, rax
jnz .done2 ; Done go exit
jmp .loop2 ; Else loop back
;------------------------------------------------------------------
;
; E X I T L O O P 2
;
;------------------------------------------------------------------
.done2:
;
; Mult by 28 and save in REG1
;
mov rsi, HAND_ACC
mov rdi, HAND_OPR
call CopyVariable ; OPR = completed sum
mov rsi, HAND_ACC
call ClearVariable ; ACC = 0
mov rbx, FP_Acc+MAN_MSW_OFST
mov rax, 0x7000000000000000 ; Mantissa X*X
mov [rbx], rax
mov rbx, FP_Acc+EXP_WORD_OFST
mov rax, 0x0000000000000005 ; Exponient X*X
mov [rbx], rax ; ACC = 176
call FP_Multiplication ; ACC = sum * 176
mov rsi, HAND_ACC
mov rdi, HAND_REG1 ; ACC REG1 = second result
call CopyVariable
;
;
;
;
;-------------------------------
;
; 3rd Summation starts here -48 * arctan(1/682)
;
;-------------------------------
;
; Used to reduce accuracy during divisions
;
call ClearGrabAccuracy ; initialize reduced accuracy variables
;^^^^^^^^^^^^^^^^^^^^^^ Watch Progress ^^^^^^^^^^^^^^^^^^^
; -Print-- Inc/Rset -Format-
mov rax, (0x00033303 | 0x00008808 | 0x30000000 )
call ShowCalcProgress
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
;
; Load 1/682 to XREG, YREG
mov rsi, HAND_OPR
call SetToOne ; OPR = 1
mov rsi, HAND_ACC
call ClearVariable ; ACC = 0
mov rbx, FP_Acc+MAN_MSW_OFST
mov rax, 0x5540000000000000 ; Mantissa for X
mov [rbx], rax
mov rbx, FP_Acc+EXP_WORD_OFST
mov rax, 0x000000000000000A ; Exponient for X
mov [rbx], rax
call FP_Division ; ACC = 1/682
mov rsi, HAND_ACC
mov rdi, HAND_XREG
call CopyVariable ; XREG = 1/X (sum)
mov rdi, HAND_YREG
call CopyVariable ; YREG = 1/X (term)
;
; Set n counter in ZREG to 1, for 1,3,5 ...
;
mov rsi, HAND_ZREG
call SetToOne ; ZREG = 1 (n counter)
;
; Init sign counter for (-1)^n
;
mov r8, 0
;
; * * * * * * * * * * * * * * * * * * * * * * * * *
;
; M A I N L O O P 3 -48 * arctan(1/682)
;
; * * * * * * * * * * * * * * * * * * * * * * * * *
;
.loop3:
;
; Get last term and multiply by X^2
;
mov rsi, HAND_YREG
mov rdi, HAND_OPR
call CopyVariable ; OPR = last term
mov rsi, HAND_ACC
call ClearVariable ; ACC = 0
mov rbx, FP_Acc+MAN_MSW_OFST
mov rax, 0x718E400000000000 ; Mantissa X*X
mov [rbx], rax
mov rbx, FP_Acc+EXP_WORD_OFST
mov rax, 0x0000000000000013 ; Exponient X*X
mov [rbx], rax ; ACC = ((682*682)=465124
call ReduceSeriesAccuracy
call FP_Division ; ACC = term / (x*x)
call RestoreFullAccuracy
mov rsi, HAND_ACC
mov rdi, HAND_YREG
call CopyVariable ; YREG = (n+1) term
;
; Add 2 to n
;
mov rsi, HAND_ZREG