07.04.2013 Views

MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip

MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip

MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Mixing Assembly Language <strong>and</strong> C Modules<br />

EXAMPLE 9-11: USING REQUIRED REGISTERS<br />

Some instructions in the <strong>dsPIC</strong> DSC instruction set require oper<strong>and</strong>s to be in a particular<br />

register or groups of registers. Table 9-1 lists some constraint letters that may be<br />

appropriate to satisfy the constraints of the instruction that you wish to generate.<br />

If the constraints are not sufficient or you wish to nominate particular registers <strong>for</strong> use<br />

inside asm statements, you may use the register-nominating extensions provided by<br />

the compiler to support you (<strong>and</strong> reduce the need to mark registers as clobbered) as<br />

the following code snippet shows. This snippet uses a fictitious instruction that has<br />

some odd register requirements:<br />

{ register int in1 asm("w7");<br />

register int in2 asm("w9");<br />

register int out1 asm("w13");<br />

register int out2 asm("w0");<br />

}<br />

in1 = some_input1;<br />

in2 = some_input2;<br />

__asm__ volatile ("funky_instruction %2,%3,%0; = %1" :<br />

/* outputs */ "=r"(out1), "=r"(out2) :<br />

/* inputs */ "r"(in1), "r"(in2));<br />

/* use out1 <strong>and</strong> out2 in normal C */<br />

In this example, funky_instruction has one explicit output, out1, <strong>and</strong> one implicit<br />

output, out2. Both have been placed in the asm template so that the compiler can<br />

track the register usage properly (though the implicit output is in a comment statement).<br />

The input shown is normal. Otherwise, the extended register declarator syntax is used<br />

to nominate particular hard registers which satisfy the constraints of our fictitious<br />

funky_instruction.<br />

EXAMPLE 9-12: HANDLING VALUES LARGER THAN INT<br />

Constraint letters <strong>and</strong> modifiers may be used to identify various entities with which it is<br />

acceptable to replace a particular oper<strong>and</strong>, such as %0 in:<br />

asm("mov %1, %0" : "r"(foo) : "r"(bar));<br />

This example indicates that the value stored in foo should be moved into bar. The<br />

example code per<strong>for</strong>ms this task unless foo or bar are larger than an int.<br />

By default, %0 represents the first register <strong>for</strong> the oper<strong>and</strong> (0). To access the second,<br />

third, or fourth register, use a modifier letter specified in Table 9-2.<br />

© 2008 <strong>Microchip</strong> Technology Inc. DS51284H-page 127

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

Saved successfully!

Ooh no, something went wrong!