|
Advanced Mathematical Routines in Assembly Language
Normal is a demo of how to transfer a fairly complex calculation from BB4W into ASM, using the Normal Distribution as the example. The Normal Distribution involves estimating the value of an integral, which cannot be performed analytically anyway. That means it's a numerical integration according to the formula shown at the bottom of the page, where mu is the mean and sigma is the standard deviation. Since the integral is essentially zero below a lower integration limit of -6.0 (and is almost static at 1 above an upper limit of 6.0), I approximate the integral with a finite sum starting at the lower limit of -6.0
The approximation is easy in BASIC; I just add up a lot of numbers of the type EXP(X). It's not so easy in Assembler - since there is NO straightforward EXP function. To achieve EXP(X) in Assembler, you have to do the following rather perverse looking calculation;
1) Notice that EXP(X) = 2^{X.log2(e)} and that there is the Floating Point (FP) instruction FLDL2E which pushes "LOG-TO-BASE-TWO-OF-e" onto the FP stack.
2) Notice that the FP instruction to perform 2^X ONLY WORKS if -1<X<+1, so notice that 2^X=2^(K+Y) where K is an integer, and Y is between -1 and +1.
3) Work out 2^K using integer arithmetic (easy using the shift-left or shift-right instructions), and work out 2^Y using the FP instruction F2XM1 which calculates 2^number - 1. (No, I have no idea why it does this. It just does!) Then you add 1 to get 2^Y.
4) Finally, multiply together 2^K and 2^Y, and that will be EXP(X), as required.
|
|