编写一个C程序来调用另一个程序,而不使用任何内置的库 [英] Writing a C program to call another program without using any built-in libraries
问题描述
void delay()
{
volatile int i; (i = 0; i< 1000000; i ++)
int main(void)
{
* LED0 = 0;
* led1 = 0;
while(1)
{
* led0 = 1;
delay();
* led0 = 0;
* led1 = 1;
delay();
* led1 = 0;
}
}
在我的go.c文件中,我想传递一个控制调用这个led.c main()func
GCC有一个扩展,允许跳转到任意地址,所以如果你知道你的led.c main的地址,你可以这样做:
void * func_ptr =(void *)0x1234567 ; //你的led例程的地址
goto * func_ptr;
但是,您可能没有led例程的地址,并不是很安全操作。跳到一些未知的地址可能会导致崩溃!
I am trying to write a simple 'go-command' for boot-loader that takes me to specific address in RAM say 0x18000000 and it should execute a program that blinks led. I have two .c files say led.c and go.c in which led.c blinks two leds. But I am wondering and don't know that how can I pass a control/invoke its main() to this go.c file to go to that address and start blinking leds? But it should be done without including other header files, libraries, etc. Kindly help me!! Thanks in advance. Below code is for led.c
void delay ()
{
volatile int i;
for(i=0;i<1000000;i++)
{}
}
int main(void)
{
*led0=0;
*led1=0;
while(1)
{
*led0=1;
delay();
*led0=0;
*led1=1;
delay();
*led1=0;
}
}
In my go.c file I want to pass a control to invoke this led.c main() func
GCC has an extension that allows jumping to an arbitrary address, so if you know the address of your led.c main you could do something like that:
void *func_ptr = (void *)0x1234567; // address of your led routine
goto *func_ptr;
However, you probably don't have the address of the led routine and it is not a very safe operation. Jumping to some unknown address will probably result in a crash!!
这篇关于编写一个C程序来调用另一个程序,而不使用任何内置的库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!