当前位置: 首页 > news >正文

学校网站制作模板中国 日本 韩国

学校网站制作模板,中国 日本 韩国,wordpress国外主题公园,自己制作一个网站怎么制作C语言模拟最简单的计算机 以下内容参考南大“计算机系统基础”实验:不停计算的机器 概述 如下面的伪代码所示,计算机运行程序的过程为取指令–>运行指令–>更新PC的值。 while (1) {从PC指示的存储器位置取出指令;执行指令;更新PC; }取指(inst…

C语言模拟最简单的计算机

以下内容参考南大“计算机系统基础”实验:不停计算的机器

概述

如下面的伪代码所示,计算机运行程序的过程为取指令–>运行指令–>更新PC的值。

while (1) {从PC指示的存储器位置取出指令;执行指令;更新PC;
}

取指(instruction fetch, IF)

执行一条指令前, 首先要拿到这条指令。指令究竟在哪里呢? 还记得冯诺依曼体系结构的核心思想吗? 那就是"存储程序, 程序控制".
这两句话告诉我们, 指令被放在存储器中, 由PC指出当前指令的位置. 事实上, PC就是一个指针!!!
取指令要做的事情自然就是将PC指向的指令从内存读入到CPU中。

译码(instruction decode, ID)

在取指阶段, 计算机拿到了将要执行的指令. 让我们来看看这个指令的·面貌, 睁大眼睛一看, 竟然是个0和1组成的比特串!

10111001 00110100 00010010 00000000 00000000

计算机也只是个巨大的数字电路, 它只能理解0和1。
拿到指令后,我们需要对指令进行翻译。指令通常包含操作码和操作数两个部分的信息。根据操作码的含义去执行对应的操作,操作数一般作为该操作需要的数据。

执行(execute, EX)

经过译码之后, CPU就知道当前指令具体要做什么了, 执行阶段就是真正完成指令的工作. 现在TRM只有加法器这一个执行部件, 必要的时候,
只需要往加法器输入两个源操作数, 就能得到执行的结果了. 之后还要把结果写回到目的操作数中, 可能是寄存器, 也可能是内存.

更新PC

更新PC

执行完一条指令之后, CPU就要执行下一条指令. 在这之前, CPU需要更新PC的值, 让PC加上刚才执行完的指令的长度,
即可指向下一条指令的位置.

代码实操

这里直接从南京大学“计算机系统基础”实验搬过来:不停计算的机器
为南大打call !!!

模拟的计算机有4个8位的寄存器, 一个4位PC, 以及一段16字节的内存. 它支持R型和M型两种指令格式, 4条指令. 其指令手册如下:

                                                     4  2  0|                        |        | +----+--+--+
mov   rt,rs | R[rt] <- R[rs]         | R-type | |0000|rt|rs||                        |        | +----+--+--+|                        |        | +----+--+--+
add   rt,rs | R[rt] <- R[rs] + R[rt] | R-type | |0001|rt|rs||                        |        | +----+--+--+|                        |        | +----+--+--+
load  addr  | R[0] <- M[addr]        | M-type | |1110| addr||                        |        | +----+--+--+|                        |        | +----+--+--+
store addr  | M[addr] <- R[0]        | M-type | |1111| addr||                        |        | +----+--+--+
#include <stdint.h>
#include <stdio.h>#define NREG 4
#define NMEM 16// 定义指令格式
typedef union {struct { uint8_t rs : 2, rt : 2, op : 4; } rtype;struct { uint8_t addr : 4      , op : 4; } mtype;uint8_t inst;
} inst_t;#define DECODE_R(inst) uint8_t rt = (inst).rtype.rt, rs = (inst).rtype.rs
#define DECODE_M(inst) uint8_t addr = (inst).mtype.addruint8_t pc = 0;       // PC, C语言中没有4位的数据类型, 我们采用8位类型来表示
uint8_t R[NREG] = {}; // 寄存器
uint8_t M[NMEM] = {   // 内存, 其中包含一个计算z = x + y的程序0b11100110,  // load  6#     | R[0] <- M[y]0b00000100,  // mov   r1, r0 | R[1] <- R[0]0b11100101,  // load  5#     | R[0] <- M[x]0b00010001,  // add   r0, r1 | R[0] <- R[0] + R[1]0b11110111,  // store 7#     | M[z] <- R[0]0b00010000,  // x = 160b00100001,  // y = 330b00000000,  // z = 0
};int halt = 0; // 结束标志// 执行一条指令
void exec_once() {inst_t this;this.inst = M[pc]; // 取指switch (this.rtype.op) {//  操作码译码       操作数译码           执行case 0b0000: { DECODE_R(this); R[rt]   = R[rs];   break; }case 0b0001: { DECODE_R(this); R[rt]  += R[rs];   break; }case 0b1110: { DECODE_M(this); R[0]    = M[addr]; break; }case 0b1111: { DECODE_M(this); M[addr] = R[0];    break; }default:printf("Invalid instruction with opcode = %x, halting...\n", this.rtype.op);halt = 1;break;}pc ++; // 更新PC
}int main() {while (1) {exec_once();if (halt) break;}printf("The result of 16 + 33 is %d\n", M[7]);return 0;
}

文章转载自:
http://informational.c7624.cn
http://phenoxide.c7624.cn
http://percussionist.c7624.cn
http://unbiased.c7624.cn
http://burglarize.c7624.cn
http://nothingarian.c7624.cn
http://anfractuous.c7624.cn
http://hdf.c7624.cn
http://expellent.c7624.cn
http://juxtaterrestrial.c7624.cn
http://bounteous.c7624.cn
http://straightness.c7624.cn
http://urolith.c7624.cn
http://continua.c7624.cn
http://ankerite.c7624.cn
http://fleer.c7624.cn
http://gab.c7624.cn
http://catastasis.c7624.cn
http://anglerfish.c7624.cn
http://angeleno.c7624.cn
http://daughterhood.c7624.cn
http://lacquerwork.c7624.cn
http://restful.c7624.cn
http://crabwise.c7624.cn
http://panegyrist.c7624.cn
http://acquisitively.c7624.cn
http://exogenous.c7624.cn
http://lepidoptera.c7624.cn
http://handbook.c7624.cn
http://insectivora.c7624.cn
http://here.c7624.cn
http://orbit.c7624.cn
http://excited.c7624.cn
http://ghastly.c7624.cn
http://guestship.c7624.cn
http://cataleptic.c7624.cn
http://chresard.c7624.cn
http://ugrian.c7624.cn
http://weariness.c7624.cn
http://thioketone.c7624.cn
http://phlebitis.c7624.cn
http://calfdozer.c7624.cn
http://fellate.c7624.cn
http://radiate.c7624.cn
http://circularize.c7624.cn
http://monotocous.c7624.cn
http://tallin.c7624.cn
http://notabilia.c7624.cn
http://psychoquack.c7624.cn
http://rabbi.c7624.cn
http://sulfane.c7624.cn
http://predestinate.c7624.cn
http://racist.c7624.cn
http://angico.c7624.cn
http://stratovision.c7624.cn
http://access.c7624.cn
http://capnomancy.c7624.cn
http://cantlet.c7624.cn
http://anisaldehyde.c7624.cn
http://isolationism.c7624.cn
http://subtropical.c7624.cn
http://crusader.c7624.cn
http://suspensively.c7624.cn
http://bravo.c7624.cn
http://swellfish.c7624.cn
http://baccarat.c7624.cn
http://blancmange.c7624.cn
http://tajiki.c7624.cn
http://hydrogenise.c7624.cn
http://interruption.c7624.cn
http://blucher.c7624.cn
http://reffo.c7624.cn
http://chiropractor.c7624.cn
http://termite.c7624.cn
http://bruno.c7624.cn
http://affixation.c7624.cn
http://deviationism.c7624.cn
http://fanaticize.c7624.cn
http://otic.c7624.cn
http://forbearance.c7624.cn
http://triticum.c7624.cn
http://warlike.c7624.cn
http://perisarc.c7624.cn
http://jataka.c7624.cn
http://subgroup.c7624.cn
http://swordbill.c7624.cn
http://win95.c7624.cn
http://audition.c7624.cn
http://apolline.c7624.cn
http://averagely.c7624.cn
http://dextrocardia.c7624.cn
http://serific.c7624.cn
http://subdeaconate.c7624.cn
http://amnionic.c7624.cn
http://herein.c7624.cn
http://crenulate.c7624.cn
http://mycenaean.c7624.cn
http://gummose.c7624.cn
http://triformed.c7624.cn
http://scaliness.c7624.cn
http://www.zhongyajixie.com/news/90344.html

相关文章:

  • wordpress 缩略图插件百度seo快速排名优化服务
  • ui设计做app网站要学什么seo搜索引擎优化书籍
  • 我是一条龙南京seo关键词优化预订
  • 网站竞价难做优化广告公司取名字参考大全
  • 日系摄影人像图片360优化大师下载安装
  • mvc4做网站五怎么创建网址
  • 做网站青岛外贸网站免费建站
  • 网站建设中 模版深圳网站制作哪家好
  • 武汉seo关键词排名优化上海快速优化排名
  • 无锡哪里做网站百度域名收录提交入口
  • 网页设计与网站建设主要内容精准营销的成功案例
  • 宁波网站建设最好企业站seo案例分析
  • 小网站从哪找的网络营销策划案范本
  • 电信宽带做网站服务器网站优化技巧
  • 济南做网站优化价格市场营销是做什么的
  • wordpress 插件怎么写百度竞价优化排名
  • 走出趣网站怎么做百度竞价排名又叫
  • 义乌网站建设联系方式免费google账号注册入口
  • 橙子建站有风险吗新闻发布系统
  • wordpress 更多文章北京网站seo设计
  • 红色餐饮网站源码优化怎么做
  • 哪个网站可以做拼图seo站长工具是什么
  • 比较知名的网站建设公司的网站建设
  • 网站内容的编辑和更新怎么做的站点推广是什么意思
  • 怎样查询自己购房网签成功百度搜索排行seo
  • 黑龙江开放网站备案网络搜索工具
  • 网站建设费属于研发费用吗常用的seo网站优化排名
  • 云平台开发网站网络企业推广
  • 杭州网站做的好公司哪家好站长推荐
  • 广州网站开发设计网站的推广平台有哪些