Mic*_*nes 2 c assembly c-preprocessor
我正在编写一个需要常量的程序,但常量的值将在运行时确定.我有一个操作码数组,我想从中随机选择一个并将其发送到程序代码中.这是一个例子:
unsigned char opcodes[] = {
0x60, // pushad
0x61, // popad
0x90 // nop
}
int random_byte = rand() % sizeof(opcodes);
__asm _emit opcodes[random_byte]; // optimal goal, but invalid
Run Code Online (Sandbox Code Playgroud)
但是,似乎_emit只能取一个恒定的值.例如,这是有效的:
switch(random_byte) {
case 2:
__asm _emit 0x90
break;
}
Run Code Online (Sandbox Code Playgroud)
但是,如果操作码阵列增长到任何相当长的时间,这就变得难以处理,并且基本上也消除了阵列的价值,因为它必须以不太吸引人的方式表达.
有没有办法整齐地编码,以促进操作码阵列的增长?我尝试过其他方法:
#define OP_0 0x60
#define OP_1 0x61
#define OP_2 0x90
#define DO_EMIT(n) __asm _emit OP_##n
// ...
unsigned char abyte = opcodes[random_byte];
DO_EMIT(abyte)
Run Code Online (Sandbox Code Playgroud)
在这种情况下,转换为OP_abyte,因此需要调用DO_EMIT(2),这会强制我回到switch语句并枚举数组中的每个元素.
我也很可能在这里采用完全无效的方法.有用的反馈表示赞赏.
我不确定你使用的是什么编译器/汇编器,但是你可以使用标签做你在GCC中所做的事情.在asm网站上,你会把它写成:
asm (
"target_opcode: \n"
".byte 0x90\n" ); /* Placeholder byte */
Run Code Online (Sandbox Code Playgroud)
...并且在您要修改该代码的地方,您将使用:
extern volatile unsigned char target_opcode[];
int random_byte = rand() % sizeof(opcodes);
target_opcode[0] = random_byte;
Run Code Online (Sandbox Code Playgroud)
也许你可以把它翻译成编译器的方言asm.
请注意,所有关于自修改代码的常见警告都适用:代码段可能不可写,您可能必须在执行修改后的代码之前刷新I-cache.
| 归档时间: |
|
| 查看次数: |
479 次 |
| 最近记录: |