编写一个C程序来调用另一个程序,而不使用任何内置的库 [英] Writing a C program to call another program without using any built-in libraries

查看:215
本文介绍了编写一个C程序来调用另一个程序,而不使用任何内置的库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为引导加载程序编写一个简单的go命令,将我带到RAM中的特定地址0x18000000,它应该执行闪烁的程序。我有两个.c文件,说led.c和go.c,其中led.c闪烁两个LED。但我想知道,不知道如何通过控制/调用它的main()到这个go.c文件去去那个地址,并开始闪烁leds?但是它应该是没有包括其他头文件,库等等。请帮助我!提前致谢。以下代码是为led.c

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆