嘿,我现在必须上课做一些练习,但是我被困住了。我是C语言中的血腥初学者,必须编写一个vm。
我正在尝试读取.bin文件,以获取诸如格式,版本,指令数量之类的信息...
那是我的代码
FILE *inputFile;
char *charBuffer;
int *intBuffer;
long filelen;
char *inputPath = argv[i + 1];
inputFile = fopen(inputPath,"r");
fseek(inputFile,SEEK_END);
filelen = ftell(inputFile);
rewind(inputFile);
charBuffer = (char *)malloc((filelen+1)*sizeof(char));
intBuffer = (int *)malloc((filelen+1)*sizeof(int));
fread(charBuffer,sizeof(char),4,inputFile);
char *format = charBuffer;
printf("format: %s\n",format);
fread(intBuffer,sizeof(int),1,inputFile);
int *version = intBuffer;
printf("version: %ls\n",version);
fread(intBuffer,inputFile);
int *instructionCount = intBuffer;
printf("instruction Count: %ls\n",instructionCount);
fread(intBuffer,inputFile);
int *variables = intBuffer;
printf("variable Count: %ls\n",variables);
这就是输出:
NJBF是正确的,但是我期望版本为2。为什么这样一个带有数字的怪异立方体?为什么指令和变量为空?
不幸的是,这堂课每周只有一次,我不能问老师。
这就是test1.bin
打开的hexdump
中的内容
00000000 4e 4a 42 46 02 00 00 00 0b 00 00 00 00 00 00 00 |NJBF............|
00000010 03 00 00 01 04 00 00 01 00 00 00 02 0a 00 00 01 |................|
00000020 06 00 00 01 00 00 00 03 00 00 00 04 00 00 00 08 |................|
00000030 0a 00 00 01 00 00 00 0a 00 00 00 00 |............|
0000003c
这些是规则:
4 bytes 'N','J','B','F' (identifies the format)
4 bytes version number (must match the VM's version number)
4 bytes number of instructions contained in the file
4 bytes number of variables in the static data area
n * 4 bytes instructions (the program to be executed)