;+-----------------------------------------------------------------+ ;| Purpose: Find out the square root of the 16-bit number | ;| 8-Bit version by Joao Dartagnan Antunes Oliveira (04/20/1993) | ;| 16-Bit modification by Bruno Marcio Diogo Venancio (08/10/2007) | ;| Email: bruno.marcio@bol.com.br | ;| Brazil v1.1 | ;| Input: B (MSB) and A (LSB) (binary) | ;| Output: A | ;| Destroy : R2,R3,R4,R5,R6,R7 | ;| THIS CODE CAN BE FREELY DISTRIBUTED WITHOUT CHANGES | ;+-----------------------------------------------------------------+ ; COUNT_L EQU R2 COUNT_H EQU R3 ACC_L EQU R4 ACC_H EQU R5 X_L EQU R6 X_H EQU R7 ORG 0 ; ;Test Routine ; LOOP: mov a,#low(40000) mov b,#high(40000) call SQRTBI ; SQRT(40000)=200 sjmp $ ; ; METHOD: ; ; Add in one accumulator an odd number counter until it cotains a value great or ; equal than X , at this moment, the counter value divided for 2 will be SQRT(X) ; SQRTBI: mov X_L,a mov X_H,b mov COUNT_L,#1 ;COUNTER <- 1 mov COUNT_H,#0 mov ACC_L,#0 ;ACUMULATOR <- 0 mov ACC_H,#0 sqrtbi00: clr c mov a,ACC_L ;ACUMULATOR >= X ? subb a,X_L mov a,ACC_H subb a,X_H jnc sqrtbi01 ;IF YES, END clr C mov a,COUNT_L add a,#2 ;COUNTER <- COUNTER + 2 mov COUNT_L,a mov a,COUNT_H addc a,#0 mov COUNT_H,a clr C ;Sum odd numbers count mov a,ACC_L add a,COUNT_L mov ACC_L,a mov a,ACC_H addc a,COUNT_H mov ACC_H,a sjmp sqrtbi00 sqrtbi01: clr C mov a,COUNT_H ;COUNTER /2 = SQRT(X) rrc a mov a,COUNT_L rrc a ret END