Digital Signal Processing Reference
In-Depth Information
; Sumfunc.asm Assembly function to find n + (n-1) + ... + 1
.def
_sumfunc
;function called from C
_sumfunc: MV .L1 A4,A1
;setup n as loop counter
SUB .S1 A1,1,A1
;decrement n
LOOP: ADD .L1 A4,A1,A4
;accumulate in A4
SUB .S1 A1,1,A1
;decrement loop counter
[A1] B .S2 LOOP
;branch to LOOP if A1#0
NOP
5
;five NOPs for delay slots
B .S2 B3
;return to calling routine
NOP
5
;five NOPs for delay slots
.end
FIGURE 3.9. ASM function called from C in the project sum ( sumfunc.asm ).
LOOP and ends with the first branch statement B. The first addition adds n
+
2) . The branch
statement is conditional based on register A1, and since A1 is not zero, branching
takes place and execution returns to the instruction at the address LOOP , where
A4
( n
-
1) with the result in A4. A1 is again decremented to ( n
-
=
n
+
( n
-
1) is added to A1
=
( n
-
2). This process continues until register
A1
0.
The second branch instruction is to the returning address B3 (by convention) of
the C calling program. The resulting sum is contained or accumulated in A4, which
is passed to result in the C program. The five NOP s (no operation) are used to
account for the five delay slots associated with a branch instruction.
The functional units .S and .L selected are shown but are not required in the
program. They can be useful for debugging and analyzing which of the functional
units are used in order to improve the efficiency of the program. Similarly, the two
colons after the label LOOP and the function name are not required.
Build and run this project as sum . With a value of n set to 6 in the C program,
verify that sum and its value of 21 are printed.
=
Example 3.3: Factorial of a Number Using C Calling an
Assembly Function ( factorial )
This example finds the factorial of a number n
2)...(1).
It further illustrates the syntax of assembly code. It is very similar to Example 3.2.
The value of n is set in the C source program factorial.c , shown in Figure 3.10,
which calls the assembly function factfunc.asm , shown in Figure 3.11. It is
instructive to read the comments.
Register A1 is again set as a loop counter. Within the loop section of code start-
ing with the address LOOP , the first multiply is n ( n
£
7 with n !
=
n ( n
-
1)( n
-
1) and accumulates in register
A4. The initial value of n is passed to the asm function through A4. The MPY
instruction has one delay slot, which accounts for the NOP following it. Processing
continues within the loop section of code until A1
-
=
0. Note that the functional units
Search WWH ::




Custom Search