Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- TMS320C2x DSP - the tms320c2x has a 16-bit program counter (pc) and a four-level http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
      The TMS320C2x has a 16-bit program counter (PC) and a four-level
      (TMS32020) or eight-level (TMS320C25) hardware stack for PC storage.
      The CALL and CALA subroutine calls store the current contents of the
      program counter on the top of the stack. The RET (return from
      subroutine) instruction then pops the top of the stack to the
      program counter.

      The following example uses a subroutine to determine the square root
      of a 16-bit number. Processing proceeds in the main routine to the
      point where the square root of a number should be taken. At this
      point a CALL is made to the subroutine, transferring control to that
      section of the program memory for execution and then returning to
      the calling routine via the RET instruction when execution has
      completed.

*
* AUTO CORRELATION
*   This routine performs a correlation of two vectors and
*   then calls a SQRT subroutine that will determine the
*   the RMS amplitude of the waveform.
*
AUTOC .
      .
      .
      LAC   ENERGY
      CALL  SQRT
      SACL  ENERGY
      .
      .
      .
*
* SQUARE ROOT
*   This subroutine determines the square root of a number X
*   that is located in the low half of the accumulator when
*   the routine is called.  The fractional square root of X
*   is taken, where 0 < X < 1 and where 1 is represented by
*   07fffH.  The result is returned to the calling routine in
*   the accumulator.
*
ST0:  .set   060h             ; saved status register 0 address
ST1:  .set   061h             ; saved status register 1 address
NUMBER: .set       062h       ; number X whose square-root is taken
TEMPR:      .set   063h       ; intermediate roots
GUESS:      .set   064h       ; square root of X
*
SQRT  SST   ST0               ; save status register 0
      SST1  ST1               ; save status register 1
      LDPK  0                 ; load data page pointer = 0
      SSXM                    ; set sign extension mode
      SPM   1                 ; left shift PR output to ACC
      SACL  NUMBER            ; save X
      LARP  AR1               ; initialize variables for square-root
      LARK  AR1,11            ; 12 iterations
      LALK  0800h             ; assume X is less than >200
      SACL  GUESS             ; set initial guess to >800
      SACL  TEMPR             ; set first intermediate root to >800
      SACH  ROOT              ; set square-root value to 0
      LAC   NUMBER            ; load X into the accumulator
      SBLK  0200h             ; test if X is less than >200
      BLZ   SQRTLP            ; if yes, take the root;
      LAC   GUESS,3           ; if no, then reinitialize
      SACL  GUESS             ; set initial guess to >4000
      SACL  TEMPR             ; set first intermediate root to >4000
      LARK  AR1,14            ; 15 iterations
*
*     SQUARE ROOT LOOP
*
SQRTLP      SQRA  TEMPR       ; square temporary (intermediate) root
      ZALH  NUMBER            ; check if result is less than X
      SPAC
      BLZ   NEXTLP            ; if it's not, skip root update
      ZALH  TEMPR             ; if it is, set root equal tempr
      SACH  ROOT
NEXTLP      LAC   GUESS,15    ; scale down guess by 2 to converge
      SACH  GUESS
      ADDH  ROOT              ; add current root estimate
      SACH  TEMPR             ; update temporary root value
      BANZ  SQRTLP            ; repeat for specified number of iterations
      LAC   ROOT              ; load the root of X
      LST1  ST1               ; restore status register 1
      LST   ST0               ; restore status register 0
      RET

See Also: context switching hardware stack call cala ret

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson