I will still need to use some assembly language somewhere, won't I?
This is no more or less true for an MSP430 C program, than for a C program anywhere
else. For example, a "Where is the first 1 in this word?" instruction exists on many
large modern processors (x86, Alpha, etc.). You don't have to use it, but it can really
speed up certain types of code. You can only make use of it with assembly language
programming. However, that can be as simple as a single line of embedded assembly
language in the middle of a C routine. This is no different with the MSP430.
As an example, an area where you might choose to use assembly language in
this way is to access the BCD facilities of the MSP430. Many people like to use
BCD for a variety of things, in small embedded projects. Like most other
processors, the MSP430 has decimal adjust instructions to make this
straightforward. You can only gain access to those through assembly language.
However, the assembly language need only be a few simple routines you create
once and reuse. They can be simple functions, like:
void bin2bcd16(register uint8_t bcd[3], register uint16_t bin)
{
register int i;
register uint16_t decimal_0;
register uint16_t decimal_1;
i = 16;
decimal_0 =
decimal_1 = 0;
__asm__ __volatile__(
"1: \n"
" rla %[bin] \n"
" dadd %[decimal_0],%[decimal_0] \n"
" dadd %[decimal_1],%[decimal_1] \n"
" dec %[i] \n"
" jnz 1b \n"
" mov.b %[decimal_1],0(%[bcd]) \n"
" mov.b %[decimal_0],2(%[bcd]) \n"
" swpb %[decimal_0] \n"
" mov.b %[decimal_0],1(%[bcd]) \n"
: [bcd] "+r"(bcd), [decimal_0] "+r"(decimal_0), [decimal_1] "+r"(decimal_1)
: [bin] "r"(bin), [i] "r"(i));
}
|
which will convert a 16 bit binary value to a BCD string stored in a byte array.
They could be simple inlined routines, for very arithmetic operations. Being
inlined means there will be no call overhead, so it is clean, compact, and fast.
For example:
static __inline__ uint16_t bcd_add16(register uint16_t bcd_a, register uint16_t bcd_b)
{
__asm__(
" clrc \n"
" dadd %[bcd_b],%[bcd_a] \n"
: [bcd_a] "+ro" (bcd_a)
: [bcd_b] "g" (bcd_b));
return bcd_a;
}
|
will add two 4 digit BCD values, stored as unsigned integers, and return the
4 digit BCD result, as an unsigned integer. Create a few routines like this,
and you need not touch any assembly language for the rest of your project.