Rob*_*Rob 5 c assembly scratch-memory
这可能是一个异国情调的问题,但我希望有人能帮助我一点;).我想执行标准的C程序,但是,在程序执行期间的某个时刻,我希望执行存储在本地便笺式RAM中的一定数量的指令.暂存器内存可供所有进程访问.让我们假设这个本地内存从地址0x80000000开始,我将它集成在下面的C代码片段中
int main {
int a=1;
int b=2;
int c=3;
c = a + b;
%goto address 0x80000000 and execute three instructions before continuing
%program execution here
return(0);
Run Code Online (Sandbox Code Playgroud)
}
假设main加载到0x40000000,程序计数器将经历以下阶段
0x40000000 a=5;
0x40000004 b=2;
0x40000008 c=1;
0x4000000C c=a+b;
0x80000000 first instruction in the local scratch pad
0x80000004 second instruction in the local scratch pad
0x80000008 third instruction in the local scratch pad
0x40000010 return(0);
Run Code Online (Sandbox Code Playgroud)
有人知道如何做到这一点?我是否需要使用汇编程序跳转指令或是否有更优雅的东西.
非常感谢,安迪
假设指令的行为类似于普通函数,您可以:
#include <stdio.h>
void (*scratchpad_func)(void) = (void(*)(void))0x80000000;
int main()
{
printf("before\n");
scratchpad_func();
printf("after\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
显然,您必须使用实模式操作系统,或跳过您的OS /处理器组合所需的任何箍,以便直接访问该地址空间.
(在某些体系结构中,"行为类似于普通函数"如果不触及被调用者保存的寄存器,就像最后的"jump $ ra"一样简单.例如MIPS.)
要跳转到固定位置,您可以声明一个指向函数的指针类型,然后将地址转换为该类型并将其作为函数调用。
如果您希望该内存区域可供程序以外的进程访问,则必须使用操作系统 (Windows/Linux/OS X) 共享内存原语将一块内存映射到您选择的地址。
听起来像是一个疯狂的想法,但它可以实现。祝你好运!