11.07.2015 Views

PicC 9.50 dsPIC Manual.pdf

PicC 9.50 dsPIC Manual.pdf

PicC 9.50 dsPIC Manual.pdf

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

C Language FeaturesMixing C and Assembler Codeproduced by the compiler. The psect’s name is text, will be linked in the CODE class,which will reside in a memory space that has 2 bytes per addressable location and muststart on a word boundary:PSECT text,reloc=4,local,class=CODE,delta=2Now we would like to call this routine add. However in assembly we must choose thename _add as this then maps to the C identifier add since the compiler prepends anunderscore to all C identifiers when it creates assembly labels. If the name add waschosen for the assembler routine the it could never be called from C code. The name ofthe assembly routine is the label that we will assocaite with the assembly code:_add:We need to be able to call this from other modules, som make this label globally accessable:GLOBAL _addArguments, or parameters, to this routine will passed via W0 and W2 registers and theresult returned in W0.By compiling a dummy C function with a similar prototype to how we will be callingthis assembly routine, we can determine the signature value. We add a assemblerdirective to make this signature value known:SIGNAT _add,8250Now to actually writing the function, remembering that the first byte parameter is alreadyin the accumulator and the second paramater is already in this routine’s paramtersarea – placed there by the calling function elsewhere. The result is placed back in to theparamater area ready to be returnedadd w0,w2,w0 ;add W0 to W2 and put the result in W0returnTo call an assembly routine from C code, a declaration for the routine must be provided. Thisensures that the compiler knows how to encode the function call in terms of paramters and returnvalues, however no other code is necessary.If a signature value is present in the assembly code routine, its value will be checked by the linkerwhen the calling and called routines’ signatures can be compared.TUT•RIALTo continue the previous example, here is a code snippet that declares the operation ofthe assembler routine, then calls the routine.51

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!