| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 266 人关注过本帖
标题:T.P版.完成了那个小模拟了.这两天在做个别的程序现在才上了.补发给你两老看 ...
只看楼主 加入收藏
clcqifeng
Rank: 2
等 级:论坛游民
帖 子:101
专家分:17
注 册:2012-2-16
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:1 
T.P版.完成了那个小模拟了.这两天在做个别的程序现在才上了.补发给你两老看看
程序代码:
/* r1 r2 r3

0001         复制操作
00011110     复制r1到r2
00010110     复制r2到r1
0010         加法
00100110     将r1与r2相加,结果存放在r1
00101101     将r1与r3相加,结果存放在r3
0011         减法
00110110     用r1减去r2,结果存放在r1
00111110     将r1减去r2,结果存放在r2
0100         移动到register
01000110     将r2中存放的地址的数据放到r1中
01001110     将r1中存放的地址的数据放到r2中

01000010 00001000  
             数字8放到r2中(00001000的值是8)
0101         移动到memory   

01010110     将r1中存放的数据放到r2所指的内存地址中去
01011110     将r2中存放的数据放到r1所指的内存地址中去
0110         为零跳转
01100101     如果r1的值为零,则跳转到r3所指的代码处
01101101     如果r3的值为零,则跳转到r1所指的代码处
0111         不为零跳转
01110101     如果r1的值不为零,则跳转到r3所指的代码处
01111101     如果r3的值不为零,则跳转到r1所指的代码处
1000         打印
10000100     打印r1所指向的内存地址的数字
10001001     打印r3所指向的内存地址的字符串,字符串以0结尾
11110000     程序结束
要求:
输入这段 01000001 00000011 01000010 00000010 00100011 01000100 00000000 01011110 10000100 11110000  把 3 + 2 的结果求出并打印出来
分析:
01000001 00000011  r3 = 3
01000010 00000010  r2 = 2
00100011           r2 = r3 + r2
01000100 00000000  r1 = 0
01011110           r2 -> memory(0)
10000100           print r1 -> memory(0)
11110000           end
要求2:
输入这段 01000001 00000001 10000001 11110000 00000000 01001000 01100101 01101100 01101100 01101111 00101100 00100000
         01110111 01101111 01110010 01101100 01100100 00100001 00000000 打印 Hello, world!
分析:
01000001 00000001  r3 = 1
10000001           print r3->memory(1)
11110000           end
00000000           not used
01001000 01100101 01101100 01101100 01101111 00101100 00100000 01110111 01101111 01110010 01101100 01100100 00100001 Hello, world
00000000           end of string   */







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


FILE *fp;
unsigned char *cp;
char *dp;
char r1, r2, r3;


char* getcommand(char* filename)
{
  unsigned char *ret = malloc(4000);
  unsigned char *c = ret;
  fread(c, 1, sizeof(unsigned char), fp);
  while((*c & 0xf0)!= 0xf0){
    c++;
    fread(c, 1, sizeof(unsigned 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(unsigned char 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(unsigned char 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(unsigned char 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(unsigned char 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: r3 = *(++cp); break;
  }
}

movemem(unsigned char command)
{
  switch(command & 0x0f){
    case 6: *(dp + r2) = r1; break;
    case 5: *(dp + r3) = r1; break;
    case 3: *(dp + r3) = r2; break;
    case 14: *(dp + r1) = r2; break;
    case 13: *(dp + r1) = r3; break;
    case 11: *(dp + r2) = r3; break;
  }
}

zero(unsigned char command)
{
  switch(command & 0x0f){
    case 6: if(r1 == 0) cp = cp + r2; break;
    case 5: if(r1 == 0) cp = cp + r3; break;
    case 3: if(r2 == 0) cp = cp + r3; break;
    case 14: if(r2 == 0) cp = cp + r1; break;
    case 13: if(r3 == 0) cp = cp + r1; break;
    case 11: if(r3 == 0) cp = cp + r2; break;
  }
}

nonzero(unsigned char command)
{
  switch(command & 0x0f){
    case 6: if(r1 != 0) cp = cp + r2; break;
    case 5: if(r1 != 0) cp = cp + r3; break;
    case 3: if(r2 != 0) cp = cp + r3; break;
    case 14: if(r2 != 0) cp = cp + r1; break;
    case 13: if(r3 != 0) cp = cp + r1; break;
    case 11: if(r3 != 0) cp = cp + r2; break;
  }
}


doprint(unsigned char command)
{
  switch(command & 0x0f){
    case 4: printf("%d",*(dp + r1)); break;
    case 2: printf("%d",*(dp + r2)); break;
    case 1: printf("%d",*(dp + r3)); break;
    case 12: printf("%s",(dp + r1)); break;
    case 10: printf("%s",(dp + r2)); break;
    case 9: printf("%s",(dp + r3)); break;
  }
}


execute(unsigned char 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: zero(command);    break;
    case 7: nonzero(command); break;
    case 8: doprint(command); break;
    default: break;
  }
}

int main(int argc, char* argv[])
{
  char filename[1024];
  if(argc == 2){
    strcpy(filename, argv[1]);
    fp = fopen(filename, "r");
    cp = getcommand(filename);
    dp = getdata(filename);
    while((*cp & 0xf0) != 0xf0){
      execute(*cp);
      cp++;
    }
    fclose(fp);
    free(cp);
    free(dp);
  }
  else printf("Usage:  a filename              ... Simulation of cpu.");
}


学1个多月了.每写一个进步一点.感谢两老的帮助.感谢两老这段时间的帮助


有哪里需要改进的,哪里不够完善的希望大家指导我更正.

[ 本帖最后由 clcqifeng 于 2012-3-15 16:33 编辑 ]
搜索更多相关主题的帖子: 数据 补发 
2012-03-15 16:28
C_戴忠意
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:2
帖 子:575
专家分:1349
注 册:2011-10-21
收藏
得分:14 

编程之路定要走完……
2012-03-15 16:59
快速回复:T.P版.完成了那个小模拟了.这两天在做个别的程序现在才上了.补发给你两 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038959 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved