English
Language : 

AN669 Datasheet, PDF (1/7 Pages) Microchip Technology – Embedding Assembly Routines into C Language Using a Floating Point Routine as an Example
M
AN669
Embedding Assembly Routines into C Language Using a Floating Point
Routine as an Example
Authors: Rick Evans
Richard Fischer
Microchip Technology, Inc.
INTRODUCTION
With the advent of MPLAB-C, the Microchip C-com-
piler, many PICmicro™ users need to embed existing
assembly language routines and/or Microchip applica-
tion notes into C. This application note explains how to
embed an assembly language program into MPLAB-C,
version 1.10, and the issues therein. For example,
embedding interrupt save and restore must be done
using assembly language. Also, critical timing routines
may require assembly. The 32-bit floating point multiply
routine from AN575 is used to illustrate this process.
The remaining 32-bit floating point math routines are
embedded into individual C functions and are included
in the file accompanying this application note.
PROCEDURE
For this example, we’ll use a PIC16C74A with 4K
Program Memory, and 192 bytes of RAM.
Embedding assembly routines
In order to embed an assembly language routine in C
code place the #asm and #endasm directives around
the assembly routine. Furthermore, if this is a subrou-
tine, as is the case with the floating point multiply, then
embed the assembly code within a C function declara-
tion. The #asm construct is illustrated in Example 1 with
an excerpt from the 32-bit floating point routine.
EXAMPLE 1: #ASM, #ENDASM
CONSTRUCT
void fpm32(void)
{
#asm
FPM32
MOVF
BTFSS
MOVF
BTFSC
GOTO
AEXP,W
_Z
BEXP,W
_Z
RES032M
;test for zero
;arguements
M32BNE0
MOVF
XORWF
MOVWF
MOVF
ADDWF
MOVLW
AARGB0,W
BARGB0,W
SIGN
BEXP,W
EXP, F
EXPBIAS-1
;save sign
;in SIGN
;...etc.
#endasm
}
Locating the Routine in Program Memory,
GOTOs and CALLs
There are two 2K word pages of program memory in
the PIC16C74A. Program memory 000h to 7FFh is
page 0, 800h to FFFh is page 1. By making fpm32() a
C function, MPLAB-C initializes the appropriate page
bit in the PCLATH register before the subroutine call is
made. (See data sheet for more on PCLATH).
A potential problem could arise, however, if the new C
function, fpm32(), crosses the page boundary
(7FFh,800h). MPLAB-C does not insert code into the
assembly code to initialize the page bits (remember
MPLAB-C does take care of paging for function calls).
That means it is up to the programmer to either; 1) add
assembly language to initialize PCLATH appropriately,
or 2) move the entire #asm function within a single
page. Option 1 involves more work. The programmer
must first compile the C code, then analyze the listing
file to see if the assembly function crossed a page
boundary. Finally, add the appropriate assembly lan-
guage to initialize PCLATH then re-compile. This solu-
tion is not desirable since every time new C code is
added to or deleted from the program, the routine,
fpm32() can potentially move across the page bound-
ary. Option 2 is the simplest solution - to locate the C
function in a single page.
© 1997 Microchip Technology Inc.
DS00669A-page 1