[Index] [Previous] [Next]

2.11 SQR - Optional Function #1

As mentioned elsewhere, during initialisation the user was asked if he wanted the SIN, RND, and SQR functions to be available. The reason for this was to give the user a chance to save a bit more memory for their programs, by having Basic reclaim the memory used by those functions. That is why these functions are at the very top of the program code.

Sqr

We use Newton's method to get a close approximation to the square root.

If FACCUM is negative then error out with invalid Function Call (FC) error.
0C21 EF Sqr RST FTestSign
0C22 FA9804 JM FunctionCallError
Return immediately if FACCUM is zero, since SQR(0)==0.
0C25 C8 RZ
Get exponent div 2, plus 0x40 (from the bias of 0x80) and keep this value on the stack. Also preserve ptr to exponent byte.
0C26 217201 LXI H,FACCUM+3
0C29 7E MOV A,M
0C2A 1F RAR
0C2B F5 PUSH PSW
0C2C E5 PUSH H
Set exponent to 0.
0C2D 3E40 MVI A,40
0C2F 17 RAL
0C30 77 MOV M,A
Create a copy of FACCUM in FBUFFER.
0C31 217401 LXI H,FBUFFER
0C34 CD290A CALL FCopyToMem
Loop through Newton's method 4 times.
0C37 3E04 MVI A,04
0C39 F5 SqrLoop PUSH PSW
FACCUM = ( (Original FACCUM / FACCUM) + FACCUM) * 0.5. This is the essence of newton's method.
0C3A CD020A CALL FPush
0C3D 217401 LXI H,FBUFFER
0C40 CD200A CALL FLoadBCDEfromMem
0C43 CD3109 CALL FDiv+2
0C46 C1 POP B
0C47 D1 POP D
0C48 CD1208 CALL FAdd+2
0C4B 010080 LXI B,8000
0C4E 51 MOV D,C
0C4F 59 MOV E,C
0C50 CDE508 CALL FMul
Loop until done 4 iterations
0C53 F1 POP PSW
0C54 3D DCR A
0C55 C2390C JNZ SqrLoop
Restore ptr to exponent byte, and restore the original exponent-div-2-plus-0x40 to A.
0C58 E1 POP H
0C59 F1 POP PSW
Correct the bias, so now we have original exponent-div-2 in A. This is then added to the existing exponent and stored.
0C5A C6C0 ADI C0
0C5C 86 ADD M
0C5D 77 MOV M,A
0C5E C9 RET

 


[Index] [Previous] [Next]