⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 Freescale Code Warrior中C的编程
💻 C
字号:
#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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -