自己做着做着有点懂你这个了。但->这东西还没弄懂。.刚做完第四步
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE *fp;
char *cp, *dp;
char r1, r2, r3;
char* getcommand(char* filename)
{
char *ret = malloc(4000);
char *c = ret;
fread(c, 1, sizeof(char), fp);
while((*c & 0xf0)!= 0xf0){
c++;
fread(c, 1, sizeof(char), fp);
}
return ret;
}
char* getdata(char* filename)
{
int i = 0;
char *ret = malloc(4000);
char *d = ret;
fread(d, 1, sizeof(char), fp);
while(!feof(fp)){
d++;
fread(d, 1, sizeof(char), fp);
}
return ret;
}
docopy(command)
{
switch(command & 0x0f){
case 6: r1 = r2; break;
case 5: r1 = r3; break;
case 3: r2 = r3; break;
case 14: r2 = r1; break;
case 13: r3 = r1; break;
case 11: r3 = r2; break;
}
}
doadd(command)
{
switch(command & 0x0f){
case 6: r1 = r1+ r2; break;
case 5: r1 = r1+ r3; break;
case 3: r2 = r2+ r3; break;
case 14: r2 = r1+ r2; break;
case 13: r3 = r1+ r3; break;
case 11: r3 = r2+ r3; break;
}
}
dosub(command)
{
switch(command & 0x0f){
case 6: r1 = r1- r2; break;
case 5: r1 = r1- r3; break;
case 3: r2 = r2- r3; break;
case 14: r2 = r1- r2; break;
case 13: r3 = r1- r3; break;
case 11: r3 = r2- r3; break;
}
}
movereg(command)
{
switch(command & 0x0f){
case 6: r1 = *(dp + r2); break;
case 5: r1 = *(dp + r3); break;
case 3: r2 = *(dp + r3); break;
case 14: r2 = *(dp + r1); break;
case 13: r3 = *(dp + r2); break;
case 11: r3 = *(dp + r1); break;
case 4: r1 = *(++cp); break;
case 2: r2 = *(++cp); break;
case 1: r1 = *(++cp); break;
}
}
movemem(command)
{
switch(command & 0x0f){
case 6:
}
}
execute(command)
{
switch(command >> 4){
case 1:
docopy(command);
break;
case 2:
doadd(command);
break;
case 3:
dosub(command);
break;
case 4:
movereg(command);
break;
case 5:
movemem(command);
break;
case 6:
break;
case 7:
break;
case 8:
break;
}
}
int main(int argc, char* argv[])
{
int c;
char filename[1024];
strcpy(filename, argv[1]);
fp = fopen(filename, "r");
cp = getcommand(filename);
printf("%2x\n",*cp);
dp = getdata(filename);
printf("%2x\n",*(dp+1));
while((*cp & 0xf0) != 0xf0){
execute(*cp);
cp++;
}
fclose(fp);
free(cp);
free(dp);
}