从 C 访问暂存器存储器

发布于 2024-09-10 11:40:26 字数 801 浏览 2 评论 0原文

这可能是一个有点奇怪的问题,但我希望有人可以 仍然帮助我一点;)。我想执行一个标准的 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);

有人知道如何做到这一点吗?我需要使用汇编跳转吗 说明或者有更优雅的东西。

非常感谢, 安迪

That might be a little bit an exotic problem, but I hope somebody can
still help me out a bit ;). I would like to execute a standard C program,
however, at some point during program execution I would like that a certain
number of instructions, which are stored in a local scratch pad RAM, are
executed. The scratchpad memory is accessable for all processes. Lets assume
this local memory starts at address 0x80000000 and I would integrate this
in the following C code fragement

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);

}

The program counter would go through the following stages, assuming
main is loaded at 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);

Anyone an idea how to do this? Do I need to use assembler jump
instructions or is there something more elegant.

Many thanks,
Andi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

樱桃奶球 2024-09-17 11:40:27

假设这些指令的行为类似于普通函数,您可以这样做:

#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。)

Assuming that the instructions behave like a normal function, you can do:

#include <stdio.h>

void (*scratchpad_func)(void) = (void(*)(void))0x80000000;

int main()
{
    printf("before\n");
    scratchpad_func();
    printf("after\n");
    return 0;
}

Obviously, you'll have to be using a real mode operating system, or jump through whatever hoops your OS/processor combination requires to have direct access to that address space.

(On some architectures "behave like a normal function" is as simple as a "jump $ra" at the end if you don't touch callee-saved registers. E.g. MIPS.)

千纸鹤 2024-09-17 11:40:27

将地址转换为函数指针或内联汇编器都不是标准化的,因此这些都不是可移植的;-)

使用 POSIX 可以像 Carl 提到的那样映射固定地址,但要再次执行它,没有简单的方法。

之前的帖子都没有回答,是如何在正好三个指令之后跳回来......

我能想到的唯一方法就是将这三个指令复制到某处,并在其后立即放置一个无条件跳转。然后使用内联汇编指令跳转到该位置。

Neither casting the address to a function pointer nor inline assembler is normalized, so nothing of that would be portable ;-)

Mapping the fixed address as Carl mentions is possible with POSIX, but then again to get it executed, there is no simple way.

What none of the previous post answers either, is how to jump back after exactly three instructions...

The only way I can think of would really be to copy just the three instructions somewhere and place an unconditional jump immediately after it. Then use an inline assembler instruction to jump to that location.

红玫瑰 2024-09-17 11:40:27

要跳转到固定位置,您可以声明一个指向函数的指针类型,然后将地址转换为该类型并将其作为函数调用。

如果您希望该内存区域可供程序以外的进程访问,则必须使用操作系统 (Windows/Linux/OS X) 共享内存原语将一块内存映射到您选择的地址。

听起来像是一个疯狂的想法,但它可以实现。祝你好运!

To jump to a fixed location, you can declare a type that's a pointer to a function, then cast your address to that type and call it as a function.

If you want this memory area to be accessible to processes other than your program, you'll have to use your operating system's (Windows/Linux/OS X) shared memory primitives to map a chunk of memory to your chosen address.

Sounds like a crazy idea, but it could be made to work. Good luck!

白龙吟 2024-09-17 11:40:27

嗯,有一些提示。

  1. 您需要确保“便笺本”中的内存是可寻址且可执行的。
  2. 确保内存位置中的指令序列以 ret 指令
  3. call 代码结束,而不是跳转到该代码。

Well a couple of hints.

  1. You need to ensure that the memory in the "scratch pad" is addressable and executable.
  2. Make sure that the sequence of instructions in the memory location ends with a ret instruction
  3. call the code rather than jumping to it.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文