Digital Signal Processing Reference
In-Depth Information
// Factorial.c Finds factorial of n. Calls function factfunc
#include <stdio.h>
//for print statement
void main()
{
short n = 7; //set value
short result; //result from asm function
result = factfunc(n); //call ASM function factfunc
printf("factorial = %d", result);//print result from asm function
}
FIGURE 3.10. C program that calls an ASM function to find the factorial of a number
( factorial.c ).
; Factfunc.asm Assembly function called from C to find factorial
.def _factfunc
;ASM function called from C
_factfunc: MV
A4,A1
;setup loop count in A1
SUB A1,1,A1
;decrement loop count
LOOP:
MPY A4,A1,A4
;accumulate in A4
NOP
;for 1 delay slot with MPY
SUB A1,1,A1
;decrement for next multiply
[A1] B LOOP
;branch to LOOP if A1 # 0
NOP 5
;five NOPs for delay slots
B B3
;return to calling routine
NOP 5
;five NOPs for delay slots
.end
FIGURE 3.11. ASM function called from C that finds the factorial of a number
( factfunc.asm ).
are not specified in this program. The resulting factorial is returned to the calling C
program through A4.
Build and run this project as factorial . Verify that factorial and its value
of 5040 (7!) are printed. Note that the maximum value of n is 7, since result is
casted as a short and 8! is greater than 2 15 .
Example 3.4: 32-bit Pseudorandom Noise Generation Using C Calling an
Assembly Function (Noisegen_casm)
The C source program Noisegen_casm.c in Figure 3.12 calls the function noisefunc
located in the file Noisegen_casmfunc.asm (Figure 3.13) to generate a 32-bit pseudo-
random noise sequence using the following scheme:
1. A 32-bit seed value such as 0
7E521603 is chosen.
2. A modulo 2 summation is performed between bits 17, 28, 30, and 31.
¥
Search WWH ::




Custom Search