5

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

任何人都知道如何做到这一点?我需要使用汇编程序跳转指令还是有更优雅的东西。

非常感谢,安迪

4

4 回答 4

6

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

于 2010-07-14T13:57:42.447 回答
1

将地址转换为函数指针和内联汇编程序都没有被规范化,所以这些都不是可移植的;-)

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

上一篇文章也没有回答,如何在恰好三个指令后跳回来......

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

于 2010-07-14T14:04:23.167 回答
0

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!

于 2010-07-14T13:53:22.600 回答
0

好吧,有几个提示。

  1. 您需要确保“便笺”中的内存是可寻址和可执行的。
  2. 确保内存位置中的指令序列以指令ret结束
  3. call代码而不是跳转到它。
于 2010-07-14T14:03:02.293 回答