
Out-of-line Assembly Code
Motorola
Mixing C and Assembly Language
For More Information On This Product,
Go to: www.freescale.com
5-35
Example 5-36. Run-time Stack Deallocation
Run-time stack deallocation: This code segment can be inserted in the general template
epilogue for out-of-line assembly code. Notice that “size” in the move statement below
should be replaced with the appropriate constant.
move
nop
move
#size
,n6; the stack size used before
; wait until n6 is available to operate on r6
(r6)-n6 ; deallocate the run-time stack
There are many ways to do this, one simple optimization would be to advance the n6 load
instruction in the program to eliminate the nop.
5.4.4 Calling C Routines
C routines are routines that are callable by a C program and may be written in either C or
assembly language. When writing assembly language subroutines, it may be necessary to
call library routines that have been provided or that have been written by the programmer
— e.g., a call to
sin()
or
printf()
. In order to do this, the programmer must follow 3 steps:
1. Push arguments onto the run-time stack in reverse order.
2. Make the subroutine call.
3. Restore the stack pointer.
The following example assumes that the four parameters to be passed to the C function
foo() are located in registers a1, b1, x0 and x1; respectively. The first four lines of
assembly code push the arguments onto the run-time stack in reverse order. The
jsr
statement makes the subroutine call and the last two statements restore the stack pointer.
Example 5-37. Calling C Routines
Calling C Routines. The C function,
foo()
, is called from the following assembly code.
Function
foo()
is declared as int
foo(int, int, int, int)
;
move
move
move
move
jsr
move
nop
move
x1,y:(r6)+
x0,y:(r6)+; run-time stack in reverse
b1,y:(r6)+; order
a1,y:(r6)+
Ffoo
; subroutine call
#4,n6
; the stack size restored.
; pushing arguments onto
(r6)-n6 ; restore it.
F
Freescale Semiconductor, Inc.
n
.