这可能是一个有点奇特的问题,但我希望有人能 还是帮我一下 ;)。我想执行一个标准的 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);
程序计数器将经历以下阶段,假设 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);
有人知道如何做到这一点吗?我需要使用汇编程序跳转吗 说明还是有更优雅的东西。
非常感谢, 安迪
最佳答案
假设指令的行为与普通函数一样,您可以:
#include <stdio.h>
void (*scratchpad_func)(void) = (void(*)(void))0x80000000;
int main()
{
printf("before\n");
scratchpad_func();
printf("after\n");
return 0;
}
显然,您必须使用实模式操作系统,或者跳过您的操作系统/处理器组合需要的任何环节才能直接访问该地址空间。
(在某些架构上,如果您不触及被调用者保存的寄存器,“表现得像一个普通函数”就像最后一个“jump $ra”一样简单。例如 MIPS。)
关于c - 从 C 访问暂存器内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3246652/