MSP430
|
Modified: |
Embedded processors typically are limited in memory.
The C language corresponds closely to low-level machine language operations and C compilers can generate highly efficient code.
For comparison, below are several examples of hand written assembler language and C generated code.
Polling - LED controlled by Push button
.cdecls C,LIST, "msp430G2231.h"
.text
main mov.w #0280h,SP
mov.w #WDTPW+WDTHOLD,&WDTCTL
bis.b #01000000b,&P1DIR
bic.b #00001000b,&P1DIR
bic.b #00001000b,&P1SEL
bic.b #01000000b,&P1OUT
_while
_if mov.b &P1IN, R15
and.b #00001000b, R15
jz _then
jmp _else
_then bis.b #01000000b, &P1OUT
jmp _endif
_else bic.b #01000000b, &P1OUT
_endif
jmp _while
_endwhile
; Interrupt Vectors
.sect ".reset"
.short main
.end
|
#include <msp430g2231.h>
void main(void) {
WDTCTL = WDTPW + WDTHOLD;
P1DIR &= ~0x08;
P1DIR |= 0x40;
P1SEL &= ~0x08;
P1OUT &= ~0x40;
while( 1 ) {
if( (P1IN & 0x08) == 0)
P1OUT |= 0x40;
else
P1OUT &= ~0x40;
}
} |
Timer interrupt control of LED
.cdecls C,LIST, "msp430g2231.h"
.text
main mov.w #0280h,SP
mov.w #WDTPW+WDTHOLD,&WDTCTL
bis.b #01000001b,&P1DIR
bic.b #00000001b,&P1OUT
bis.b #01000000b,&P1OUT
mov.w #10h,&CCTL0
mov.w #12000,&CCR0
mov.w #TASSEL_1+MC_1,&TACTL
bis.w #CPUOFF+GIE,SR
TA0 xor.b #01000001b,&P1OUT
reti
; Interrupt Vectors
.sect ".reset"
.short main
.sect ".int09"
.short TA0
.end |
#include <msp430g2231.h> |
Bubble sort - C compiler generated code
#include <msp430g2231.h>
int A[] =
{ 400, 200, 100, 700, 500, 800, 600, 300 };
void bubbleSort( int x[], int n) {
for(int pass=1; pass < n; pass++)
for(int j=0; j < n-pass; j++)
if(x[ j ] > x[ j+1 ])
{
int temp=x[ j ];
x[ j ]=x[ j+1 ];
x[ j+1 ]=temp;
}
}
int main() {
WDTCTL = WDTPW + WDTHOLD;
bubbleSort( A, 8);
return 0;
}
|
?cstart_begin:
__program_start:
00F810 4031 0280 mov.w #0x280,SP
?cstart_init_copy:
00F814 403C 0200 mov.w #0x200,R12
00F818 403D F800 mov.w #0xF800,R13
00F81C 403E 0010 mov.w #0x10,R14
00F820 12B0 F8E6 call #__data16_memcpy
?cstart_call_main:
00F824 12B0 F8A6 call #main
00F828 12B0 F8E2 call #exit
void bubbleSort( int x[], int n) {
bubbleSort:
?cstart_end:
00F82C 120A push.w R10
00F82E 120B push.w R11
00F830 1208 push.w R8
00F832 1209 push.w R9
for(int pass=1; pass < n; pass++)
00F834 431A mov.w #0x1,R10
00F836 3C27 jmp 0xF886
if(x[ j ] > x[ j+1 ])
00F838 4B0F mov.w R11,R15
00F83A 5F0F rla.w R15
00F83C 4C08 mov.w R12,R8
00F83E 5F08 add.w R15,R8
00F840 4B0E mov.w R11,R14
00F842 5E0E rla.w R14
00F844 4C0F mov.w R12,R15
00F846 5E0F add.w R14,R15
00F848 9FA8 0002 cmp.w @R15,0x2(R8)
00F84C 3416 jge 0xF87A
int temp=x[ j ];
00F84E 4B0F mov.w R11,R15
00F850 5F0F rla.w R15
00F852 4C0E mov.w R12,R14
00F854 5F0E add.w R15,R14
00F856 4E28 mov.w @R14,R8
x[ j ]=x[ j+1 ];
00F858 4B0F mov.w R11,R15
00F85A 5F0F rla.w R15
00F85C 4C09 mov.w R12,R9
00F85E 5F09 add.w R15,R9
00F860 4B0E mov.w R11,R14
00F862 5E0E rla.w R14
00F864 4C0F mov.w R12,R15
00F866 5E0F add.w R14,R15
00F868 499F 0002 0000 mov.w 0x2(R9),0x0(R15)
x[ j+1 ]=temp;
00F86E 4B0F mov.w R11,R15
00F870 5F0F rla.w R15
00F872 4C0E mov.w R12,R14
00F874 5F0E add.w R15,R14
00F876 488E 0002 mov.w R8,0x2(R14)
for(int j=0; j < n-pass; j++)
00F87A 531B inc.w R11
for(int j=0; j < n-pass; j++)
00F87C 4D0F mov.w R13,R15
00F87E 8A0F sub.w R10,R15
00F880 9F0B cmp.w R15,R11
00F882 3BDA jl 0xF838
for(int pass=1; pass < n; pass++)
00F884 531A inc.w R10
for(int pass=1; pass < n; pass++)
00F886 9D0A cmp.w R13,R10
00F888 3402 jge 0xF88E
for(int j=0; j < n-pass; j++)
00F88A 430B clr.w R11
00F88C 3FF7 jmp 0xF87C
}
00F88E 4030 F8C2 br #0xF8C2
__exit:
00F892 120A push.w R10
00F894 8321 decd.w SP
00F896 4C0A mov.w R12,R10
00F898 4A81 0000 mov.w R10,0x0(SP)
00F89C 410D mov.w SP,R13
00F89E 435C mov.b #0x1,R12
00F8A0 12B0 F8EA call #__DebugBreak
00F8A4 3FF9 jmp 0xF898
WDTCTL = WDTPW + WDTHOLD;
main:
00F8A6 40B2 5A80 0120 mov.w #0x5A80,&WDTCTL
bubbleSort( A, 8);
00F8AC 423D mov.w #0x8,R13
00F8AE 403C 0200 mov.w #0x200,R12
00F8B2 12B0 F82C call #bubbleSort
return 0;
00F8B6 430C clr.w R12
00F8B8 4130 ret
?Epilogue8:
|