The MASM Forum Archive 2004 to 2012

Miscellaneous Forums => 16 bit DOS Programming => Topic started by: carlottagp on March 23, 2011, 04:33:15 PM

Title: Calculation of square roots
Post by: carlottagp on March 23, 2011, 04:33:15 PM
I would like a reference to some 16-bit code for determining square roots of large
integers.  Can anyone help?

Michael
Title: Re: Calculation of square roots
Post by: dedndave on March 23, 2011, 05:07:04 PM
how large ?
;-----------------------------------------------
;
SQINT   PROC    NEAR
;
;  This is a 24-bit integer squareroot routine.
; It calculates the squareoroot, then rounds
; it to the nearest integer.
;
; Call With: DL:AX = 24 bit integer (0-FF:FFFFh)
;
;   Returns: AX = rounded squareroot (0-1000h)
;            BX = DX = CH = 0
;            CF = clear
;            CL, BP, SI, DI invalid
;
        MOV     DH,DL
        MOV     DL,AH
        MOV     BH,AL
        MOV     AX,0C000h
        MOV     BL,AL
        MOV     CX,8
;
SQINT0: TEST    AX,DX
        JNZ     SQINT3
;
        ROR     AX,1
        ROR     AX,1
        LOOP    SQINT0
;
        MOV     CL,4
;
SQINT1: TEST    AH,BH
        JNZ     SQINT2
;
        SHR     AH,1
        SHR     AH,1
        LOOP    SQINT1
;
        RET
;
SQINT2: SUB     CL,4
;
SQINT3: SHL     CL,1
        ADD     CL,9
        XOR     AX,AX
        MOV     SI,AX
        MOV     DI,AX
        MOV     BP,AX
;
SQINT4: SHL     BX,1
        RCL     DX,1
        RCL     DI,1
        RCL     BP,1
        SHL     BX,1
        RCL     DX,1
        RCL     DI,1
        RCL     BP,1
        STC
        RCL     SI,1
        RCL     AX,1
        SUB     DI,SI
        SBB     BP,AX
        JNC     SQINT6
;
        ADD     DI,SI
        ADC     BP,AX
        DEC     SI
        LOOP    SQINT4
;
SQINT5: SHL     SI,1
        RCL     AX,1
        SHL     SI,1
        RCL     AX,1
        SHL     SI,1
        ADC     AX,CX
        RET
;
SQINT6: INC     SI
        LOOP    SQINT4
;
        JMP     SQINT5
;
SQINT   ENDP
;
;-----------------------------------------------
Title: Re: Calculation of square roots
Post by: raymond on March 24, 2011, 01:26:05 AM
If you don't find anything else, you may want to download the .zip file at the bottom of the following page:
http://www.ray.masmcode.com/BCDtut.html

One of the sub-folders contains the source code (and executable) for computing the square root of numbers with as many as 35 digits (any combination of integer and decimal digits). The tutorial itself may help you understand some of the code.

The source code uses 32-bit registers. However, that program was originally developed on a TRS-80 which had one 16-bit register for adressing and four 8-bit registers. It was later re-written for a PC-XT having the usual Intel 16-bit registers. You should thus be able to easily rework the 32-bit code to use only 16-bit registers. And,
- you could also expand the range of input to whatever quantity of digits you need (there's no limit, the 35 limit was arbitrary)
- you could also expand the precision to any limit you need (limited by available memory); the 9999 I used was because it was easier to count the number of digits of the input (i.e. maximum 4) than to compare the input to an actual limit; I also only had a total of 64kb of memory on the TRS-80, including the BIOS and video areas!
Title: Re: Calculation of square roots
Post by: carlottagp on March 24, 2011, 11:40:59 AM
Many thanks.  I will study these ideas and try to learn something.

Michael