In diesem Beispiel-Programm wird eine einfache Kopierfunktion in C und ASM geschrieben. Die Funktion/Unterprogramm soll Daten aus dem Flash in den RAM kopieren bis ein bestimmtes Zeichen bzw. ein bestimmter Wert gefunden wurde (in diesem Beispiel wird ein String kopiert bis das Stringende-Zeichen gefunden wurde). Sind die Daten für den reservierten Buffer zu groß, wird eine 0 zurückgegeben, bei einem erfolgreichen Kopiervorgang eine 1.
ASM-Programm
FILE: main.asm
;*********************************************** ; ___ _ _ ; | |_ _ _| |___ ___ _| |___ ; | | |_'_| . | -_| _|_| . | -_| ; |___|_,_|___|___|___|_|___|___| ; ; FILE: main.asm ; Author: declis (xdec.de) ;*********************************************** .cdecls "msp430g2231.h" .cdecls "lib_func.h" .bss buffer,size ;allocate 16 bytes in RAM .sect .const data: .cstring "omg ASM rox kthx" ;define string with '�' (in flash) .ref copy_data_ram .global main .text main: mov.w #0x280,SP ;initialize stack pointer mov.w #WDTPW+WDTHOLD,&WDTCTL ;stop watchdog timer mov.w #data,R4 ;sourcepointer for data in R4 mov.w #buffer,R5 ;sourcepointer for buffer in R5 call #copy_data_ram jmp $ .end
FILE: lib_func.asm
;*********************************************** ; ___ _ _ ; | |_ _ _| |___ ___ _| |___ ; | | |_'_| . | -_| _|_| . | -_| ; |___|_,_|___|___|___|_|___|___| ; ; FILE: lib_func.asm ; Author: declis (xdec.de) ;*********************************************** .cdecls "msp430g2231.h" .cdecls "lib_func.h" .def copy_data_ram ;global entry of function .text ;------------------------------------------ ; Name: copy_data_ram ; Description: copy data into RAM/Buffer till ; termination value found ; Input: R4 - sourcepointer of source ; R5 - sourcepointer of buffer ; Returns: R8 - 1 = success, 0 = overflow ; Destroys: R8 ;------------------------------------------ copy_data_ram: push.w SR push.w R4 push.w R5 push.w R6 mov.w #size,R8 ;copy size into R8 mov.w #0,R6 ;index=0 loop: cmp.b #value,0(R4) ;termination value found? jz success cmp.w R8,R6 ;overflow? jz over_error mov.b @R4+,0(R5) ;copy value of "source" into buffer and increment ;sourcepointer of "source" ;C: buffer[index]=source[index]; index++; inc.w R5 ;increment sourcepointer of "buffer" inc.w R6 ;increment index jmp loop over_error: mov.w #0,R8 ;return 0 jmp done success: mov.w #1,R8 ;return 1 done: pop.w R6 pop.w R5 pop.w R4 pop.w SR ret .end
FILE: lib_func.h
/************************************************* * ___ _ _ * | |_ _ _| |___ ___ _| |___ * | | |_'_| . | -_| _|_| . | -_| * |___|_,_|___|___|___|_|___|___| * * FILE: lib_func.h * Author: declis (xdec.de) ************************************************/ #ifndef LIB_FUNC_H_ #define LIB_FUNC_H_ #define size 16 /* define size=16 */ #define value 0 /* define termination value */ #endif /*LIB_FUNC_H_*/
C-Programm
FILE: main.c
- /*************************************************
- * ___ _ _
- * | |_ _ _| |___ ___ _| |___
- * | | |_'_| . | -_| _|_| . | -_|
- * |___|_,_|___|___|___|_|___|___|
- *
- * FILE: main.c
- * Author: declis (xdec.de)
- ************************************************/
- #include <msp430g2231.h>
- #include "lib_func_c.h"
- void main(void)
- {
- const char data[]={"omg ASM rox kthx"}; /* data in flash (const char) */
- char buffer[size]; /* allocate 16 bytes in RAM */
- WDTCTL=WDTPW+WDTHOLD; /* stop watchdog timer */
- copy_data_ram(data,buffer);
- for(;;);
- }
/************************************************* * ___ _ _ * | |_ _ _| |___ ___ _| |___ * | | |_'_| . | -_| _|_| . | -_| * |___|_,_|___|___|___|_|___|___| * * FILE: main.c * Author: declis (xdec.de) ************************************************/ #include <msp430g2231.h> #include "lib_func_c.h" void main(void) { const char data[]={"omg ASM rox kthx"}; /* data in flash (const char) */ char buffer[size]; /* allocate 16 bytes in RAM */ WDTCTL=WDTPW+WDTHOLD; /* stop watchdog timer */ copy_data_ram(data,buffer); for(;;); }
FILE: lib_func.c
- /*************************************************
- * ___ _ _
- * | |_ _ _| |___ ___ _| |___
- * | | |_'_| . | -_| _|_| . | -_|
- * |___|_,_|___|___|___|_|___|___|
- *
- * FILE: lib_func.c
- * Author: declis (xdec.de)
- ************************************************/
- #include <msp430g2231.h>
- #include "lib_func_c.h"
- char copy_data_ram(const char *data,char *buffer)
- {
- char index=0;
- for(;data[index]!=value;index++) /* termination value found? */
- {
- if(index==size) return 0; /* overflow occurred */
- buffer[index]=data[index]; /* copy flash-data in RAM/Buffer */
- }
- return 1; /* success */
- }
/************************************************* * ___ _ _ * | |_ _ _| |___ ___ _| |___ * | | |_'_| . | -_| _|_| . | -_| * |___|_,_|___|___|___|_|___|___| * * FILE: lib_func.c * Author: declis (xdec.de) ************************************************/ #include <msp430g2231.h> #include "lib_func_c.h" char copy_data_ram(const char *data,char *buffer) { char index=0; for(;data[index]!=value;index++) /* termination value found? */ { if(index==size) return 0; /* overflow occurred */ buffer[index]=data[index]; /* copy flash-data in RAM/Buffer */ } return 1; /* success */ }
FILE: lib_func_c.h
- /*************************************************
- * ___ _ _
- * | |_ _ _| |___ ___ _| |___
- * | | |_'_| . | -_| _|_| . | -_|
- * |___|_,_|___|___|___|_|___|___|
- *
- * FILE: lib_func_c.h
- * Author: declis (xdec.de)
- ************************************************/
- #ifndef LIB_FUNC_C_H_
- #define LIB_FUNC_C_H_
- #define size 16 /* define size=16 */
- #define value 0 /* define termination value */
- char copy_data_ram(const char*,char*);
- #endif /*LIB_FUNC_C_H_*/
/************************************************* * ___ _ _ * | |_ _ _| |___ ___ _| |___ * | | |_'_| . | -_| _|_| . | -_| * |___|_,_|___|___|___|_|___|___| * * FILE: lib_func_c.h * Author: declis (xdec.de) ************************************************/ #ifndef LIB_FUNC_C_H_ #define LIB_FUNC_C_H_ #define size 16 /* define size=16 */ #define value 0 /* define termination value */ char copy_data_ram(const char*,char*); #endif /*LIB_FUNC_C_H_*/