将单个装配件转换为机器代码

时间:2016-03-02 20:41:46

标签: c assembly

我正在尝试将程序集转换为机器代码。我有一个MINGW编译器,如果我键入./convert.exe mov%a,那么它应该输出0x01 0xc0。我正在考虑使用一个结构列出每个汇编代码及其相应的机器值。目前,我不断收到像#34;请求成员操作码的错误,而不是结构"。任何帮助将不胜感激。

#include <stdio.h>
#include <string.h>

struct _Instruction {
char mnemonic[10];
unsigned char opcode;} typedef Instruction;

Instruction instruction_list[] = {
{"mov", 0x01},
{"add", 0x04},
{"sub", 0x05},
{"mul",0x06},
{"div", 0x07},
{"and",0x08},
{"or",0x09},
{"xor",0x0a},
{"cmp",0x0b},
{"",-1},
};

Instruction get_inst(char mnemonic[]);

int main2(int argc, char *argv[])
{

char* instruction = argv[1];
Instruction get_inst = get_Instruction(instruction);

printf("%s  ;  %s",instruction_list.mnemonic,instruction_list.opcode);
return 0;
}

Instruction get_inst(char mnemonic[])
{
int i;

for(i=0; instruction_list[i].opcode != -1; i++)
{
  if(!strcmp(instruction_list[i].mnemonic, mnemonic))
  {
    return instruction_list[i];
  }
}
return instruction_list[i]; 
}

1 个答案:

答案 0 :(得分:1)

首先,您的结构声明不正确。您应该将其格式化为:

typedef struct _Instruction {
    ....
} Instruction;

我不确定为什么没有触发语法错误,但它肯定没有帮助。

此外,您还拥有一个名为get_inst的变量和一个函数。您调用名为get_Instruction()的不存在的函数。你可能想要命名你的函数get_Instruction()

此外,您的结构的.opcode成员是一个char。您的printf语句使用&#34; %s&#34;格式说明符来打印它。这需要一个字符串,这将导致printf继续读取.opcode成员的末尾,显示不可预测的垃圾并访问它不应该触摸的内存。

相关问题