main.c

来自「Freescale Code Warrior中C的编程」· C语言 代码 · 共 71 行

C
71
字号
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#include <stddef.h> /* for size_t type */

extern char __SEG_START_ToCopyToStack[];
extern char __SEG_SIZE_ToCopyToStack[];

typedef int(* my_funtype)(int);

static void foo(void);

#pragma CODE_SEG ToCopyToStack


int MyStackFunction(int a)
{
  /* this function will be copied to stack and executed there */
  foo();
  return a+1; /* just doing something */
}

#pragma CODE_SEG DEFAULT /* switch back to default code section */

static void foo(void)
{
  
}

void DummyFunc(void)
{
  unsigned char array[0x10] = {0};
}

int CopyToStackandExecute(void)
{
  /* this function will copy the function 'MyStackFunction' to stack and
  execute it */
  int res; /* return value of 'MyStackFunction' */
  #define RAM_BUF_SIZE 14 /* enough to keep 'MyStackFunction' */
  char ramBuf[RAM_BUF_SIZE];
  short counter;
  char *srcPtr;
  
  if (sizeof(ramBuf) < (size_t)__SEG_SIZE_ToCopyToStack)
  {
    /* our buffer is too small to keep the function! */
    return 0; /* failure */
  }
  
  srcPtr = (char *)__SEG_START_ToCopyToStack;
  
  for(counter=0; counter<(short)__SEG_SIZE_ToCopyToStack; counter++)
  {
    ramBuf[counter] = *srcPtr++;
  }
  
  /* call it! */
  res = ((my_funtype)ramBuf)(3);  /* warning: Non standard conversion used */
  return res;
}


void main(void)
{
  do
  {
    DummyFunc();
    CopyToStackandExecute();
  }while(1);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?