📄 sqrtv.asm
字号:
;***********************************************************
; Version 2.10.02
;***********************************************************
;********************************************************************************
; Function: sqrtv
; Description: square root of a 16-bit number
;
; Copyright Texas instruments Inc, 1998
;--------------------------------------------------------------------------------
; Revision History:
; 1.00, A. Aboagye, 8/31/98 - Original release. Started from code by
; Jeff Axelrod.
;********************************************************************************
.mmregs
.def _sqrt_16
.if __far_mode
OFFSET .set 1
.else
OFFSET .set 0
.endif
REG_SAVE_SZ .set 3
; Set Number Of Iterations Of Sqrt Algorithm (N-1):
; Note: N = 5, accuracy = 100 * 2/32767 = 0.0061%
;------------------------------------------------
.asg 5, N
.asg N-1, ITERATIONS
; Command-line stack arguments
;-----------------------------
.asg 3 + OFFSET, ARG_Z
.asg 4 + OFFSET, ARG_N
; ARXY_ means Xmem/Ymem register
;-------------------------------
.asg ar0, AR_INDEX ; Points to SP_INDEX
.asg ar1, AR_FRAC ; Points to SP_FRAC
; Xmem/Ymem are registers AR2-5
;------------------------------
.asg ar2, ARXY_Z
.asg ar3, ARXY_TBL
.asg ar4, ARXY_X
.asg ar5, ARXY_HEX5E00
.asg BRC, AR_N
; Stack local variables
;----------------------
.asg 0, SP_INDEX
.asg 1, SP_FRAC
.asg 2, SP_LO
; These constants get stored on the stack to increase speed.
;-----------------------------------------------------------
.asg 3, SP_TBL
.asg 4, SP_HEX7FFF
.asg 5, SP_ONE
.asg 6, SP_HEX4000
.asg 7, SP_XNORM
.asg 8, SP_HEX5E00
;
; Frame size for local variables
; Reserve space for 9 local variables
;------------------------------------
FSZ .set 9
_sqrt_16:
PSHM ST0 ; 1 cycle
PSHM ST1 ; 1 cycle
RSBX OVA ; 1 cycle
RSBX OVB ; 1 cycle
; Far-mode adjustment
;--------------------
; Process command-line arguments
;-------------------------------
stlm a, ARXY_X
mvdk *sp(ARG_Z), ARXY_Z
; N=N-1
;------
ld *sp(ARG_N), a
sub #1, a
stlm a, AR_N
; Save AR1 & AR6 to maintian compatability
; with normal TMS320C54x C environment
;----------------------------------------------------------------
pshm ar1
frame #-FSZ ; Can't access command-line arguments after this instruction
; Initialize constants
;---------------------
ld #SP_HEX5E00, b
add *(sp), b
stlm b, ARXY_HEX5E00
st #05E00h, *sp(SP_HEX5E00)
st #SqrtTable, *sp(SP_TBL)
LOOP:
RPTBD END_LOOP-1
st #01h,*sp(SP_ONE) ; delay slot 2 cycles
; Get next input value in array, x
; and round input value by adding 0x8000
;----------------------------------------------------------------
ld *ARXY_X+, 16, a
add *sp(SP_ONE), 15, a ; Round input value
; Normalize input value
;----------------------
exp a ; t=exp
nop
norm a
; Use lookup table to find SQRT of exponent
; lookup table index == exponent
;------------------------------------------
ld *sp(SP_TBL), b
add *(t), b
stlm b, ARXY_TBL ; ARXY_TBL=exp+SqrtTable
; Use the sqrt of
;----------------
mvdd *ARXY_HEX5E00, *ARXY_Z ; Initial value for Ynorm
sth a, *sp(SP_XNORM)
; The following code will be expanded 5 times by the
; assembler, so that the sequnce below is executed N
; times
;----------------------------------------------------
.loop N
; Load normalized estimate of square root
; Ynorm(new) = Ynorm(old) - (Ynorm(old)^2 - Xnorm)/2
;---------------------------------------------------
ld *sp(SP_XNORM), 15, a
squrs *ARXY_Z, a
add *ARXY_Z,16, a
sth a, *ARXY_Z
.endloop
; Multiply sqrt(Ynorm) * sqrt(normalized_exponent)
; And round the result
;-------------------------------------------------
mpy *ARXY_TBL, *ARXY_Z, a
add *sp(SP_ONE), 15, a
; Store result
;-------------
sth a, 1, *ARXY_Z+
END_LOOP:
ld #0, a
xc 1, AOV
ld #1, a
frame #FSZ
popm ar1
POPM ST1 ; 1 cycle
POPM ST0 ; 1 cycle
.if __far_mode
fret
.else
ret
.endif
;---------------------------------------------------------------
; Square root lookup table
;---------------------------------------------------------------
; Ytable = 1/sqrt(2^n) values:
;
.data
.def SqrtTable
SqrtTable:
.word 7FFFh ; 1/sqrt(2^0) = 0.99997
.word 5A82h ; 1/sqrt(2^1) = 0.70711
.word 4000h ; 1/sqrt(2^2) = 0.50000
.word 2D41h ; 1/sqrt(2^3) = 0.35355
.word 2000h ; 1/sqrt(2^4) = 0.25000
.word 16A1h ; 1/sqrt(2^5) = 0.17678
.word 1000h ; 1/sqrt(2^6) = 0.12500
.word 0B50h ; 1/sqrt(2^7) = 0.08839
.word 0800h ; 1/sqrt(2^8) = 0.06250
.word 05A8h ; 1/sqrt(2^9) = 0.04419
.word 0400h ; 1/sqrt(2^10) = 0.03125
.word 02D4h ; 1/sqrt(2^11) = 0.02210
.word 0200h ; 1/sqrt(2^12) = 0.01563
.word 016Ah ; 1/sqrt(2^13) = 0.01105
.word 0100h ; 1/sqrt(2^14) = 0.00781
.word 0000h
;end of file. please do not remove. it is left here to ensure that no lines of code are removed by any editor
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -