MSP430 Buffer erstellen und initialisieren

In diesem Beispiel-Programm soll ein Buffer mit einer definierten Größe erstellt werden und komplett initialisiert werden (mit einem bestimmten Wert). Weiterhin wird das Initialisierungs-Unterprogramm in einer eigenen Datei ausgelagert.

ASM-Programm

FILE: main.asm

;***********************************************
;     ___       _             _     
;    |   |_ _ _| |___ ___   _| |___
;    | | |_'_| . | -_|  _|_| . | -_|
;    |___|_,_|___|___|___|_|___|___|
;
; FILE:     main.asm
; Author:   declis (xdec.de)
;***********************************************

    .cdecls "msp430g2231.h"
    .cdecls "lib_func_asm.h"
    .bss buffer,size               ;allocate 16 bytes in RAM
    .ref buf_init
    .global main
    .text

main:
    mov.w #0x280,SP                ;initialize stack pointer
    mov.w #WDTPW+WDTHOLD,&WDTCTL   ;stop watchdog timer

    mov.w #buffer,R4               ;startaddress of buffer in R4

    call #buf_init

    jmp $

    .end

FILE: lib_func.asm

;***********************************************
;     ___       _             _     
;    |   |_ _ _| |___ ___   _| |___
;    | | |_'_| . | -_|  _|_| . | -_|
;    |___|_,_|___|___|___|_|___|___|
;
; FILE:     lib_func.asm
; Author:   declis (xdec.de)
;***********************************************

    .cdecls "msp430g2231.h"
    .cdecls "lib_func_asm.h"
    .def buf_init              ;global entry of function
    .text

;------------------------------------------
; Name:         buf_init
; Description:  initialize memory/buffer
; Input:        R4 - sourcepointer of buffer
; Returns:      nothing
; Destroys:     nothing
;------------------------------------------
buf_init:
    push.w SR          ;save status register
    push.w R4          ;save other registers
    push.w R5

    mov.w #size,R5
    cmp.w #0,R5        ;index=0?
loop:
    jz done            ;index=0, return
    mov.b #value,0(R4) ;copy value into buffer -> buffer[index]=value;
    inc.w R4           ;sourcepointer+1
    dec.w R5           ;index-1
    jmp loop
done:
    pop.w R5
    pop.w R4
    pop.w SR
    ret

    .end

FILE: lib_func_asm.h

;***********************************************
;     ___       _             _     
;    |   |_ _ _| |___ ___   _| |___
;    | | |_'_| . | -_|  _|_| . | -_|
;    |___|_,_|___|___|___|_|___|___|
;
; FILE:     lib_func_asm.h
; Author:   declis (xdec.de)
;***********************************************

#ifndef LIB_FUNC_ASM_H_
#define LIB_FUNC_ASM_H_

#define size 16         /* define size=16 */
#define value 0         /* initialize buffer with "value" */

#endif /*LIB_FUNC_ASM_H_*/
buffer_init_asm01buffer_init_asm02
Buffer vor der InitialisierungBuffer nach der Initialisierung (mit Null)

C-Programm

Und nochmal das gleiche Programm in C übersetzt:

FILE: main.c

/*************************************************
 *     ___       _             _     
 *    |   |_ _ _| |___ ___   _| |___
 *    | | |_'_| . | -_|  _|_| . | -_|
 *    |___|_,_|___|___|___|_|___|___|
 *
 * FILE:     main.c
 * Author:   declis (xdec.de)
 ************************************************/

#include <msp430g2231.h>
#include "lib_func.h"

void main(void)
{
    char buffer[size];

    WDTCTL=WDTPW+WDTHOLD;           /* stop watchdog timer */

    buf_init(buffer);

    for(;;);
}

FILE: lib_func.c

/*************************************************
 *     ___       _             _     
 *    |   |_ _ _| |___ ___   _| |___
 *    | | |_'_| . | -_|  _|_| . | -_|
 *    |___|_,_|___|___|___|_|___|___|
 *
 * FILE:     lib_func.c
 * Author:   declis (xdec.de)
 ************************************************/

#include <msp430g2231.h>
#include "lib_func.h"

void buf_init(char *buffer)
{
    char index=0;

    while(index<size)
        buffer[index++]=value;

}

FILE: lib_func.h

/*************************************************
 *     ___       _             _     
 *    |   |_ _ _| |___ ___   _| |___
 *    | | |_'_| . | -_|  _|_| . | -_|
 *    |___|_,_|___|___|___|_|___|___|
 *
 * FILE:     lib_func.h
 * Author:   declis (xdec.de)
 ************************************************/

#ifndef LIB_FUNC_H_
#define LIB_FUNC_H_

#define size_def 16
#define value_def 2

void buf_init(char*);

#endif /*LIB_FUNC_H_*/
buffer_init_c01
Buffer mit “2” gefüllt, interessant zu sehen: Das C-Programm ist ein paar Bytes kleiner als das ASM-Programm.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

This site uses Akismet to reduce spam. Learn how your comment data is processed.