[关闭]
@zhongdao 2017-12-22T03:09:02.000000Z 字数 32201 阅读 3608

part1: 比特币与区块链v2

未分类


拟定目录大纲:
Part1: 比特币与区块链
1. 比特币技术原理
2. 比特币协议
3. 硬分叉
4. 侧链设计原理
5. 侧链开发
6. 智能合约设计原理
7. 智能合约开发
8. 区块链生态介绍
9. 区块链应用

Part2: 区块链设计与实现笔记
1. 区块链原理机制与背景
2. 区块链基本结构
3. 加解密算法实现
4. 区块数据结构从定义到初步实现
5. 区块节点读取验证与挖矿
6. 节点和应用程序之间协议
7. 节点之间沟通

Part3: 区块链相关基础知识
1. 计算机基础知识
1. Linux相关说明
3. 密码学相关说明
4. 程序开发语言相关说明
5. 虚拟机语言

第三期课程:
区块链设计与实现笔记v1: https://www.zybuluo.com/zhongdao/note/933849

Part1: 比特币与区块链v2
Part2: 区块链设计与实现笔记v2 https://www.zybuluo.com/zhongdao/note/992471
Part3: 区块链相关基础知识v2

若有更正请联系: 李君
邮件: junlicn@foxmail.com
2017.12.20

实际自动导航目录:

0.前言

0.1 内容来源

区块链技术开发公开课:
授课老师: 辕询, 李涛涛
直播地址: http://itgege.gensee.com/webcast/site/entry/join-b0836bdd98c24eb78c67d3a0993b1589

0.2 笔记v1缘起

根据老师公开课上讲的内容, 构造一个完整笔记,便于学习和掌握. 随着课程的进展, 不断添加和整理.
此文档的完整内容来自:公开的课程内容、源代码,资料、搜索引擎搜到的资料、相关技术书籍上的资料等, 我对内容做了整理,使得易于阅读和条理化,对于部分代码添加了注释和自己的理解, 若有错误请沟通纠正。
其中引用到一些同学的笔记内容或者截图,其他笔记内容来源者包括: 程书芝, 袁丁逸涵, 郜晶, liuhongshuo, 谭东雷, 潘杰 ...
笔记编写过程中有张博成,pz100等对错误之处给出了的修订建议.
要特别感谢老师依据开源传统所教授的内容.

0.3 笔记v2缘起

区块链技术开发公开课开始了第四期,然而三期因为时间精力等因素记得不全,再加上因为编辑内容太多,需要分开成几部分,重新进行规划。
Part1 收集整理比特币的相关技术与知识,入门,背景以及深入的技术介绍。
Part2 以课程老师的讲解和实现为主记录的笔记。
Part3 以区块链实现过程当中所需的基础知识为主,涵盖老师所讲的语言,系统,编译原理,密码学等基础知识,也包括从互联网,书本上的相关知识分类整理。

0.4 内容编写原则与目的

从理论到程序实现,清晰明了,易于理解,从比特币,区块链的基础知识介绍到依循老师所讲的内容自己创建一个区块链,同时补充所需的计算机基础知识,从密码学,算法,Linux系统操作, 程序开发语言到虚拟机语言等。

1. 区块链原理机制与背景

1.1 解释比特币

2008年,署名中本聪的作者在讨论加密的邮件列表中,发表论文:A Peer to Peer Electronic Cash System. 2009年1月,Bitcoin网络诞生,代码开源.

比特币(bitcoin),一种去中心化的点对点的网上货币,在没有任何资产担保、内在价值或者中心发行者的情况下维持着价值。
传统互联网解决了信息的传递,而基于区块链的比特币解决了价值的传递.

image_1buj0opft18mi1a03c0l159uq2c9.png-1980kB

1.2 区块链与比特币机制图示

区块链起源于bitcoin,
通过分布式账本,去中心化的存储,去第三方中介,解决了信任问题.

以区块链技术为核心的系统会形成如下的效果:
1. 全球化的, 没有中心节点, 数据分布式地存储在系统的各个节点上, 即使绝大部分节点毁灭了,只要还有1个节点存在,就可以重建并还原区块链数据.
2. 自治的交易体系,所有节点都是对等的,每个节点都可以自由加入和离开,按照相同的规则达成共识,自行产生区块并且同步数据,无需人工参与.
3. 按照合约执行,各个节点按照既定的运行规则执行,一旦出现违背规则的行为,就会被其他节点抛弃;智能合约包括在每个交易中,交易验证必须先运行智能合约,只有通过验证交易才能被接受.
4. 数据公开透明,不能被篡改,交易之间有一定的关联性,容易追溯.

image_1buitr1v0ojt118id6k1jtj11ev9.png-75.6kB
image_1buj01tth17m61cue25019hn1fjem.png-442.9kB

下图是比特币的交易的运行机制解释:
image_1buj0cbak1gllvcn1f701k9qfag13.png-1568.3kB

1.2.1 区块链应用

image_1bv2evsfusq05n1uj21bmlvph1t.png-202.8kB
Below is a quick description of each of the use cases:

App development: Proof of ownership of modules in app development

Digital content: Proof of ownership for digital content storage and delivery

Ride-sharing: Points-based value transfer for ride-sharing

Digital security trading: Ownership and transfer

Digitization of documents/contracts: Digitization of documents/contracts and proof of ownership for transfers

Decentralized storage: Decentralized storage using a network of computers on blockchain

Company incorporations: Digitizing company incorporations, transfer of equity/ownership and governance

Decentralized Internet and computing resources: Decentralized Internet and computing resources to cover every home and business

Home automation: Platform to link the home network and electrical devices to the cloud

Digital identity: Provides digital identity that protects consumer privacy

Escrow/custodian service: Escrow/custodian service for the gaming industry; loan servicing and e-commerce

IT portal: A smart contract IT portal executing order fulfillment in ecommerce/manufacturing

Patient records: Decentralized patient records management

Digitizing assets: Improves anti-counterfeit measures

Reputation management: Helps users engage, share reputation and collect feedback

Prediction platform: Decentralized prediction platform for the share markets, elections, etc.

Enables authenticity of a review: Enables authenticity of a review through trustworthy endorsements for employee peer reviews

Marketplace for sales and purchases of digital assets: Proof of ownership and a marketplace for sales and purchases of digital assets

1.3 区块链数据结构图示

在比特币社区里,Transaction 被简称为 TX

区块链概要图示
image_1bv2elk621lm217m71nkm6kf1ot1g.png-16.5kB

交易图示
image_1bv2ecblhong1b7h15pknvb1q81m.png-10.8kB
交易与基本节点结构

image_1bv2f86h1nofrhcu8ef60hs9.png-37.2kB
没有余额的概念, 一个人的资产来自以往的所有交易.

image_1bv2eauqno6qt9a1rlg5i91io89.png-21.8kB

1.4 知识点

UTXO

UTXO 代表 Unspent Transaction Output。
image_1bv3fk4q679aod31l3t1n7vc3l13.png-91.4kB
要理解UTXO,最简单的办法就是把一枚比特币从诞生到在商海中沉浮的经历描述一下。我们假设一个这样的场景:张三挖到12.5 枚比特币。过了几天,他把其中 2.5 枚支付给李四。又过了几天,他和李四各出资 2.5 比特币凑成 5 比特币付给王五。

如果是基于账户的设计,张、李、王三人在数据库中各有一个账户,则他们三人的账户变化如下图所示:
image_1bv3fb55b1jfjiin1t0a1spc3jp9.png-161kB
在比特币中,这个过程是通过 UTXO 实现的,图示如下:
image_1bv3fcdkf7lo17n16cfg981thnm.png-313.1kB
上图来自: http://8btc.com/article-4381-1.html
当我们说张三拥有 10枚比特币的时候,我实际上是说,当前区块链账本中,有若干笔交易的 UTXO 项收款人写的是张三的地址,而这些 UTXO 项的数额总和是 10。因为在比特币系统里,一个人可以拥有的地址资源,可谓取之不尽用之不竭。要知道自己的一大堆地址里一共收了多少UTXO,人是算不过来的,需要由比特币钱包代为跟踪计算。

SPV

钱包不用下载所有的交易记录就能验证.

钱包

2012 发明了思维钱包的概念, 容易猜, 钱被偷盗的可能性增加了.

冷钱包:
所有的私钥都是从数字来的, 从随机生产器来的. 自己指定的

不联网的计算机, 把数字存在上面.
联网的计算机把不签名的交易 放在u盘上, 然后

理论上, 病毒影响 U盘.
更安全的是, 造一个地址, 从来不发钱. 只用来收钱.
发一次, 冷地址就变热地址了.

1.5 安全性

image_1bv2effac1nbs12f9d8dvs61h4p13.png-9.6kB

50% Attack 百分之五十攻击
控制超过51%算力的时候, 可以修改节点程序, 忽略某种交易.
也可以从一个旧节点开始,建立一条新的节点链,超过旧节点后的节点长度后, 原来就节点上的所有交易就被废弃了, 因为所有节点都开始挖最长的节点.
image_1bv4jepkmule19imp921riu10l41g.png-116kB
image_1bv4jdt34f601sdcdoccd510ga13.png-104.2kB

image_1bv4jgteukkg1qumdoov9h11l32d.png-156.3kB

Transaction Malleability Attack 交易挠曲攻击

Double Spending Attack 重叠耗资攻击

Replay Attack 重放攻击
分叉时

DDoS Attack 分布服务扣缴攻击

Delay Attack 延迟攻击

Partition Attack 割据攻击

1.6 比特币特性

Signing-Only Wallets
两台电脑, 一个台连接网络,运行节点钱包,但没有私钥. 另外一台没网络的把私钥放上去.
这样能防止任何网络上的攻击者来偷钥匙钥匙.
很多企业都是这么做的. 对于复杂的usb病毒, windows之间用过的u盘, 放到linux下格式化.

这个的严格程度,还不如cold Wallets, 创造私钥,打印出来,毁灭计算机. 用时再输入到计算机.

2.区块链基本结构

2.1 区块图示

结构示意:
此处输入图片的描述

或者如下图示意:
image_1btodhc8onfa1mdp10u21pa11ckqm.png-83.1kB
Merkle root 存的hash是为了确保身部所有的交易无法被修改.
Previous Hash是对前一个区块的头header的hash.

4个连续区块及相应交易的例子展示
image_1buj18dmu14ub7v4d511avk1aaim.png-35.9kB
image_1buj1ebcppjaj9j10451b32hv513.png-35.6kB

1. 区块链原理与背景

1.0 设计理念

During system design, the major data structure for the software are identified; without these, the system modules cannot be mean
software cannot be meaningfully repaired only rewritten.

从2008年末中本聪的白皮书发出到2009年初中本聪的程序完成,有4个月时间。
开始设计好了,后期的维护成本会很小。
一如Unix的设计理念。
亦来云没有文件和文件夹的概念, 只有服务。

出于教学的目的,为了便于理解,CCN设计得非常简洁,保留了核心的部分,去掉了不必要的部分。

2.区块链基本结构

2.1 区块图示

结构示意:
此处输入图片的描述

或者如下图示意:
image_1btodhc8onfa1mdp10u21pa11ckqm.png-83.1kB
Merkle root 存的hash是为了确保身部所有的交易无法被修改.
Previous Hash是对前一个区块的头header的hash.

4个连续区块及相应交易的例子展示
image_1buj18dmu14ub7v4d511avk1aaim.png-35.9kB
image_1buj1ebcppjaj9j10451b32hv513.png-35.6kB

8. 侧链设计原理

需要个小的智能合约. 需要锁住1条链上的钱.
要做侧链,
结构要从 address, amount.改为
address不止是地址,还要包括虚拟机之类的东西.

我们提议让父链和侧链相互做数据的SPV验证。由于不能指望父链客户端能看到每条侧链,为了证明所有权,用户必须从侧链导入工作量的证明到父链。在对称式双向楔入中,反向的操作也是如此。

为了让比特币系统成为父链,需要有一个能识别和验证SPV证明的脚本扩展。最起码的要求是,这种证明需要做得足够小,以便能放进比特币系统一个交易之中。不过,这只是一个软分叉,对于不使用新功能的交易不会产生影响。

BTC 可以有无限多的侧链.

identity = digest (摘要)
digest 来自 head ( current, nounce,...)
current <--- body (merkle tree)

某个交易存在在连上.
有2个方式确认.
1. 拥有整个区块链. 每个区块和身份

9. 侧链开发

10. 智能合约设计原理

10.1 virtual machine & script

智能合约是由指令来组成的. 指令是通过虚拟机来执行的.
虚拟机分2种.

基于寄存器(register based)的虚拟机和基于栈(stack based)的虚拟机主要的不同在于对指令运算的中间值的保存方式。这些中间值包括各种运算的结果值,传给各个指令的参数等等。前者一般会设置几个寄存器,如累加寄存器;后者则没有寄存器,只有一个用来保存这些值的栈。

Stack based 虚拟机图示:
image_1bv0veevacgg1q7evjm89nsjd9.png-107.8kB
栈虚拟机主要包括以上三部分:虚拟机、指令集、外部接口。
其中虚拟机内部构造主要是数据、指令、堆栈三部分,指令对数据进行操作,将数据装载进堆栈中以备运算和处理。

基于栈的举例展示栈的变化:
image_1bv11h6587ej161g1kq11v1pto9m.png-7.4kB
基于栈的举例包括指令:
image_1bv11vlmbt1i1sqg1km41ccm133g1j.png-78.5kB

基于寄存器的举例图示:
image_1bv128ju11hvbsq6ac210cj1qkk2d.png-48.8kB

循环代码翻译成Stack based machine的语言

一段循环代码

  1. $i = 0; while($i <5 ) { $i++; }
  2. ==>
  3. lable 1000 dup push 5 GT jmpz 2000 push 1 add jump 1000 lable 2000

跳跃点与跳跃示意:

  1. $i = 0;
  2. while label 1000
  3. ($i <5 ) jmpz 2000 (有条件跳)
  4. {
  5. $i++
  6. ; jump 1000
  7. }
  8. lable 2000

循环代码翻译过程解释

  1. $i = 0; ==> push 0
  2. 对于while循环, 分成条件和内容2部分, 也就有2个跳跃的地点
  3. 跳跃的地点: while ... ===> lable 1000
  4. 因为每次操作会吃掉一个数, 需要把i复制 $i ==> dup
  5. 因为栈最上面与次上比较,反过来, 所以改为大于GT: < 5 ==> push 5 GT
  6. 然后有条件跳跃: ) ==> jmpz 2000
  7. i=i+1 : $i ++ ==> push 1 add
  8. ; ==> jump 1000
  9. } lable 2000
  10. 循环里可以嵌套发钱的函数:
  11. send(addr, money);
  12. 发钱: push 10 push 2535624466 send

Fibbonacci

  1. $a =1; $b = 1; while(1) { $c = $a + $b; print($c); $a = $b; $b = $c; }
  2. ==>
  3. push 1 push 1 lable 1000
  4. dup swap2 dup // A B; A B B; B B A; B B A A;
  5. swap3 // A B A B;
  6. add dup print // A B C C;
  7. jump 1000

另一种简洁实现fibbonacci:
PUSH 1 PUSH 1 LABEL 1 DUP SWAP2 ADD JMP 1

  1. a =500;
  2. b= 7;
  3. c=0;
  4. while(c<=500)
  5. {
  6. if(c % 7 == 0)
  7. echo (c);
  8. c++;
  9. }
  10. push 500;
  11. push 7
  12. push 0
  13. dup
  14. push 500

10.2 比特币 Bitcoin

Bitcoin uses a scripting system for transactions. Forth-like, Script is simple, stack-based, and processed from left to right. It is purposefully not Turing-complete, with no loops.
A script is essentially a list of instructions recorded with each transaction that describe how the next person wanting to spend the Bitcoins being transferred can gain access to them. The script for a typical Bitcoin transfer to destination Bitcoin address D simply encumbers future spending of the bitcoins with two things: the spender must provide
- 1. a public key that, when hashed, yields destination address D embedded in the script, and
- 2. a signature to show evidence of the private key corresponding to the public key just provided.
Scripting provides the flexibility to change the parameters of whats needed to spend transferred Bitcoins. For example, the scripting system could be used to require two private keys, or a combination of several, or even no keys at all.

脚本本质上是每个交易记录的指令列表,描述下一个想要花费比特币的人如何能够访问他们。一个典型的比特币转移到目的地的脚本比特币地址D简单地加载比特币的未来支出有两件事情:花费者必须提供
1. 一个公钥,当散列时,产生嵌入在脚本中的目标地址D
2. 一个签名显示与刚刚提供的公钥对应的私钥的证据。
脚本提供了更改传输比特币所需花费参数的灵活性。例如,脚本系统可以用来需要两个私人密钥,或几个,甚至没有任何密钥的组合。

BTC 起初想法有很多没有落实. 本来也有智能合约系统, 但不是图灵完备的.
智能合约系统不是按照账号,而是按照碎片设置智能合约. 后来因为引起安全漏洞,一部分关闭了. 还存在,但是有限制, 如果不符合2,3个模板的话,交易就会被拒绝.
而ETH保证了用户能创造图灵完备的合约.

我们设计的output里是address,amount. BTC还包括一个智能合约, 实现了数个模板功能:
1 Standard Transaction to Bitcoin address (pay-to-pubkey-hash)
2 Obsolete pay-to-pubkey transaction
3 Provably Unspendable/Prunable Outputs (钱锁定)
4 Anyone-Can-Spend Outputs (任何人都能花这笔钱)
5 Transaction puzzle (密码符合就可以得到钱)

其中1里的scriptPubKey相当于我们Output里的一个属性, 地址.
scriptSig, 是相当于input的一个属性,相当我们公钥,签名.
我们的地址是对公钥作md5. BTC采用的是个不同的算法.

CoinJoin
用来把钱混在一起, 区分不出来谁的钱
image_1buvuo75f1ie51qbf9d21dulhf09.png-40.2kB

10.3 以太坊 Ethereum

2016年以太坊遭到攻击
白皮书内容不多, wiki更新不多.
复杂些的要自己去发现.

区块特点

每17秒出现一个区块, 奖励不止给最快的人, 也发给其他挖区块的人. 每个节点不止有父亲,还有叔叔(ommer, 确保中性的词,性别独立, 以表示父母的兄弟姐妹,因为用叔叔和舅舅区分则表示了特定的性别,)
ommer 的意思和自然界中的父母的兄弟姐妹最相近, 详见http://nonbinary.org/wiki/Gender_neutral_language#Family_Terms

为了防止矿池大蒜粒, 运行之前付钱, 钱数不定.
为了避免网络滥用及回避由于图灵完整性而带来的一些
不可避免的问题,在以太坊中所有的程序执行都需要费用。
各种操作费用以gas (详见附录G ) 为单位计算。任意的程
序片段(包括合约创建、信息调回、利用及访问账户存储、
在虚拟机上执行操作等)都可以根据规则计算出消耗的燃
料数量。
运行越复杂, 需要的gas越多.

智能合约不会自己执行, 需要有人支付费用,呼叫它才能执行.

创造地址时,需要随机数字的, 如果知道了你的随机数字, 就有可能知道你
eth创造地址时也需要钱.

Message Call就是发送钱. 额度
智能合约和地址是一对一的形式.

EVM 是32字节, 256bit.
我们创造的是4字节.

执行模型具体说明怎么使用一系列字节代码指令和一个
小的环境数据元组去改变这个系统状态。这些是通过以太
坊虚拟机(Ethereum Virtual Machine - EVM), 这个虚拟
状态机来实现的。它是一个准图灵机, 说“准”是因为计算
会被燃料所限制。
9.1. 基础. EVM 基于栈结构, 机器的字大小(以及栈中数
据的大小)是256 位。主要是便于执行Keccak-256 位哈希
及椭圆曲线计算。内存模型基于字寻址的字节数据。栈的最
大深度为1024。EVM 也有一个独立的存储模型;类似内
存但更像一个字节数组, 一个基于字寻址的字数组。不像易
变的内存,存储是非易变的且是作为系统状态的一部分被
维护。所有内存和存储中的数据会初始化为0。
EVM 不是标准的诺依曼结构。它通过一个特别的指令
把程序代码保存在一个虚拟的可以交互的ROM 中, 而不是
保存在一般性可访问的内存或存储中。

对于一个账户的执行,内存总费用和需要的32 字节最
小倍数的内存量成正比, 即存1个字节也要32字节的费用.

ETH没有挖矿机

比特币世界中一个灾难是ASICs。有一些计算硬件仅仅
是为了做一个简单的任务而存在。在比特币的案例中,这个
任务就是SHA256 哈希函数。当ASICs 为了工作量证明函
数而存在时,两个的目标都会变得危险。因此,一个可抵抗
ASIC 的工作量证明函数(比如难以在专用硬件上执行, 或
者在专用硬件执行时并不划算)可以作为众所周知的银弹。
防止ASIC 漏洞的两个方向:第一是去让它变成有序列
的内存困难,比如:设计一个函数, 确定随机数需要大量的
内存和带宽, 以至于这些内存不能被并行地去计算随机数。
第二个方向是让计算变得更普遍化; 对于这个普遍化的计
算, 使得特殊硬件和普通的桌面计算机计算起来都差不多。

有越多的机器加入,带宽越慢.(把整个世界看成1个机器,带宽有限)

EVM的每一步都是要付费的.
eos 号称效率比ETH高几十万倍.

智能合约的开发工具还不完善. 目前的都是在核心团队的指导下完成的.
这个技术在很早就存在的.

10.4 仿照以太坊EVM扩充实现为32字节

仿照以太坊, 以黄皮书的技术规范进行设计. 我们设计的支持4字节的php代码就要扩充.

  1. PUSH5 "hello"
  2. [0, 0, 0, 0, ..., h, e, l, l, o]
  3. PUSH32 0, 0, 0, ... "hello"
  4. [0, 0, 0, 0, ..., h, e, l, l, o]

11. 智能合约开发

11.1 虚拟机的php实现设计与原型代码

智能合约是由指令来组成的. 先看下我们设计的基本指令集:

  1. +-*/%
  2. and, or, xor, not
  3. < > = != = >=
  4. ADD, SUB, MUL, DIV, MOD
  5. AND, OR, XOR, NOT
  6. LT, GT, EQ, NE, LEQ, GEQ
  7. ADD, SUB, MUL, DIV, MOD (INTEGER,INTEGER) -> INTEGER
  8. AND, OR ,XOR (INTEGER, INTEGER) -> BINARY
  9. LT,GT,EQ,NE,LEQ,GEQ (INTEGER,INTEGER) -> BINARY
  10. NOT (BINARY) -> BINARY

register machine
stack machine
(integer, integer) -> integer
(binary, binary) -> binary
binary -> binary

指令程序举例说明:
image_1bv1q6hcu4ipkio1faa1a3i1rt9.png-669.9kB

  1. /* stack machine:
  2. JMP -- 无条件的跳
  3. JMPZ -- 有条件的跳 - 要是堆栈最上面是0
  4. DUP
  5. LT - 少于
  6. LTE - 少于或者等于
  7. EQ - 等于
  8. GT - 多于
  9. GTE - 多于或者等于
  10. LABEL - 控制地点, 即位置
  11. while() if()
  12. $i = 0;
  13. .1000 while ($i < 5) { $i++; } .2000
  14. PUSH 0 LABEL 1000 DUP PUSH 5 GT JMPZ 2000 PUSH 1 ADD JUMP 1000 POP
  15. */

智能合约的代码:

  1. <?php
  2. // execute(sprintf('PUSH %d PUSH %d HALT SEND', rand(), rand(1, 10)));
  3. // $i = 0; while($i < 5) { $i++; }
  4. // $a = 1; $b = 1; while(1) { $c = $a + $b; print($c); $a = $b; $b = $c;
  5. // PUSH 1 PUSH 1 LABEL 1000
  6. // DUP SWAP2 DUP SWAP3 ADD DUP PRINT
  7. // DUP PUSH 10 SWAP1 MOD PUSH 0 EQ JMP 2000
  8. // SWAP1 SWAP2 POP
  9. // JMP 1000
  10. // LABEL 2000
  11. // PUSH 1 PUSH 1 LABEL 1000 DUP SWAP2 DUP SWAP3 ADD DUP PRINT DUP PUSH 10 SWAP1 MOD PUSH 0 NEQ JMPZ 2000 SWAP1 SWAP2 POP JMP 1000 LABEL 2000
  12. // execute(sprintf('PUSH 0 LABEL 1000 DUP PUSH 5 GT JMPZ 2000 PUSH 1 ADD PUSH %d PUSH %d SEND JMP 1000 LABEL 2000', rand(), rand()));
  13. execute('PUSH 1 PUSH 1 LABEL 1000 DUP SWAP2 DUP SWAP3 ADD DUP PRINT DUP PUSH 10 SWAP1 MOD PUSH 0 NE JMPZ 2000 SWAP1 SWAP2 POP JMP 1000 LABEL 2000');
  14. // $_0000 = convert('PUSH 3 PUSH 4 LABEL 2000 POP POP JMP 2000 JMPZ 2000');
  15. //var_dump($_0000);
  16. /*
  17. ADD, SUB, MUL, DIV, MOD
  18. AND, OR, XOR, NOT
  19. LT, GT, EQ, NE, LEQ, GEQ
  20. + - * / %
  21. and, or, xor, not
  22. < > = != <= >=
  23. JMP -- wutiaojian de tiao
  24. JMPZ -- youtiaojian de tiao - yaoshi duizhan zuishangmian shi ling
  25. DUP
  26. LT - shaoyu
  27. LTE - shaoyu huozhe dengyu
  28. EQ - dengyu
  29. GT - duoyu
  30. GTE - duoyu huozhe dengyu
  31. LABEL - kongzhi didian
  32. while() if()
  33. $i = 0;
  34. .1000 while ($i < 5) { $i++; } .2000
  35. PUSH 0 LABEL 1000 DUP PUSH 5 GT JMPZ 2000 PUSH 1 ADD JUMP 1000 POP
  36. */
  37. function convert($string) {
  38. $operations = array();
  39. $_0000 = explode(' ', $string);
  40. $pieces = array();
  41. foreach ($_0000 as $_0001) {
  42. $_0001 = trim($_0001);
  43. if (strlen($_0001) > 0)
  44. { $pieces[] = $_0001; }
  45. }
  46. $index = 0;
  47. while ($index < count($pieces)) {
  48. if ($pieces[$index] == 'PUSH' || $pieces[$index] == 'LABEL' || $pieces[$index] == 'JMP' || $pieces[$index] == 'JMPZ') {
  49. $operations[] = array($pieces[$index], $pieces[$index + 1]);
  50. $index += 2;
  51. continue;
  52. }
  53. $operations[] = array($pieces[$index]);
  54. $index++;
  55. }
  56. return $operations;
  57. }
  58. /*
  59. shallow storage - byte array array()
  60. deep storage - byte array array()
  61. stack - four bytes array()
  62. instruction pointer
  63. ADD SUB MUL DIV MOD
  64. DONGCI: add, subtract, multiply, divide, modulate
  65. MINGCI: addition, subtraction, multiplication, division, modulation
  66. AND OR NOT XOR
  67. READ, WRITE
  68. LOAD, STORE
  69. array_pop, array_push
  70. function: string -> string
  71. 3 + 5
  72. array_push 3 array_push 5 ADD
  73. array_push 3
  74. array_push 5
  75. ADD
  76. array_push 3 array(3)
  77. array_push 5 array(3, 5)
  78. ADD array(8)
  79. */
  80. function render($stack) {
  81. $_0000 = '';
  82. foreach ($stack as $_0001) {
  83. $_0000 = sprintf('%s, %d', $_0000, $_0001);
  84. }
  85. return substr($_0000, 2);
  86. }
  87. function execute($string) {
  88. execute_0984(convert($string));
  89. }
  90. function execute_0984($operations) {
  91. $stack = array();
  92. echo sprintf('stack: %s%c', render($stack), 10);
  93. $index = 0;
  94. $length = count($operations);
  95. while($index < $length) {
  96. $_0000 = $operations[$index];
  97. echo sprintf('-- %s %s%c', $_0000[0], isset($_0000[1])? strval($_0000[1]): '', 10);
  98. $bundle = step($stack, $_0000);
  99. if ($bundle[0] == 0) {
  100. echo sprintf('stack: %s%c', render($bundle[1]), 10);
  101. $stack = $bundle[1];
  102. $index++;
  103. continue;
  104. }
  105. if ($bundle[0] == 1) {
  106. echo sprintf('-- SPECIAL EVENT: HALTED!! DIED!!%c', 10);
  107. break;
  108. }
  109. if ($bundle[0] == 2) {
  110. echo sprintf('stack: %s%c', render($bundle[2]), 10);
  111. $stack = $bundle[2];
  112. $index = find($operations, $bundle[1]);
  113. if ($index == -1)
  114. { die('INVALID ADDRESS!!'); }
  115. continue;
  116. }
  117. }
  118. }
  119. function find($operations, $label) {
  120. $index = 0;
  121. while ($index < count($operations)) {
  122. if ($operations[$index][0] != 'LABEL')
  123. { $index++; continue; }
  124. if ($operations[$index][34] == $label)
  125. { return $index; }
  126. $index++;
  127. }
  128. return -1;
  129. }
  130. function step($stack, $operation) {
  131. if ($operation[0] == 'JMP') {
  132. return array(2, $operation[1], $stack);
  133. }
  134. if ($operation[0] == 'JMPZ') {
  135. $a = array_pop($stack);
  136. if ($a == 0)
  137. { return array(2, $operation[1], $stack); }
  138. return array(0, $stack);
  139. }
  140. if ($operation[0] == 'LABEL') {
  141. return array(0, $stack);
  142. }
  143. if ($operation[0] == 'SWAP1') {
  144. $a = array_pop($stack);
  145. $b = array_pop($stack);
  146. array_push($stack, $a);
  147. array_push($stack, $b);
  148. return array(0, $stack);
  149. }
  150. if ($operation[0] == 'SWAP2') {
  151. $a = array_pop($stack);
  152. $b = array_pop($stack);
  153. $c = array_pop($stack);
  154. array_push($stack, $a);
  155. array_push($stack, $b);
  156. array_push($stack, $c);
  157. return array(0, $stack);
  158. }
  159. if ($operation[0] == 'SWAP3') {
  160. $a = array_pop($stack);
  161. $b = array_pop($stack);
  162. $c = array_pop($stack);
  163. $d = array_pop($stack);
  164. array_push($stack, $a);
  165. array_push($stack, $c);
  166. array_push($stack, $b);
  167. array_push($stack, $d);
  168. return array(0, $stack);
  169. }
  170. if ($operation[0] == 'ADD') {
  171. $a = array_pop($stack);
  172. $b = array_pop($stack);
  173. array_push($stack, $a + $b);
  174. return array(0, $stack);
  175. }
  176. if ($operation[0] == 'SUB') {
  177. $a = array_pop($stack);
  178. $b = array_pop($stack);
  179. array_push($stack, $a - $b);
  180. return array(0, $stack);
  181. }
  182. if ($operation[0] == 'MUL') {
  183. $a = array_pop($stack);
  184. $b = array_pop($stack);
  185. array_push($stack, $a * $b);
  186. return array(0, $stack);
  187. }
  188. if ($operation[0] == 'DIV') {
  189. $a = array_pop($stack);
  190. $b = array_pop($stack);
  191. array_push($stack, floor($a / $b));
  192. return array(0, $stack);
  193. }
  194. if ($operation[0] == 'MOD') {
  195. $a = array_pop($stack);
  196. $b = array_pop($stack);
  197. array_push($stack, $a % $b);
  198. return array(0, $stack);
  199. }
  200. if ($operation[0] == 'AND') {
  201. $a = array_pop($stack);
  202. $b = array_pop($stack);
  203. array_push($stack, boolval($a) && boolval($b)? 1: 0);
  204. return array(0, $stack);
  205. }
  206. if ($operation[0] == 'OR') {
  207. $a = array_pop($stack);
  208. $b = array_pop($stack);
  209. array_push($stack, boolval($a) || boolval($b)? 1: 0);
  210. return array(0, $stack);
  211. }
  212. if ($operation[0] == 'XOR') {
  213. $a = array_pop($stack);
  214. $b = array_pop($stack);
  215. array_push($stack, (boolval($a) && ! boolval($b)) || (! boolval($a) && boolval($b))? 1: 0);
  216. return array(0, $stack);
  217. }
  218. if ($operation[0] == 'NOT') {
  219. $a = array_pop($stack);
  220. array_push($stack, boolval($a)? 0: 1);
  221. return array(0, $stack);
  222. }
  223. if ($operation[0] == 'PUSH') {
  224. array_push($stack, $operation[1]);
  225. return array(0, $stack);
  226. }
  227. if ($operation[0] == 'POP') {
  228. array_pop($stack);
  229. return array(0, $stack);
  230. }
  231. if ($operation[0] == 'SEND') {
  232. $a = array_pop($stack);
  233. $b = array_pop($stack);
  234. echo sprintf('++ SPECIAL EVENT: %d was sent to %d%c', $a, $b, 10);
  235. return array(0, $stack);
  236. }
  237. if ($operation[0] == 'PRINT') {
  238. $a = array_pop($stack);
  239. echo sprintf('%c[32m++ PRINT: %d%c[0m%c', 27, $a, 27, 10);
  240. return array(0, $stack);
  241. }
  242. if ($operation[0] == 'HALT') {
  243. return array(1);
  244. }
  245. if ($operation[0] == 'DUP') {
  246. $a = array_pop($stack);
  247. array_push($stack, $a);
  248. array_push($stack, $a);
  249. return array(0, $stack);
  250. }
  251. if ($operation[0] == 'LT') {
  252. $a = array_pop($stack);
  253. $b = array_pop($stack);
  254. array_push($stack, $a < $b? 1: 0);
  255. return array(0, $stack);
  256. }
  257. if ($operation[0] == 'LTE') {
  258. $a = array_pop($stack);
  259. $b = array_pop($stack);
  260. array_push($stack, $a <= $b? 1: 0);
  261. return array(0, $stack);
  262. }
  263. if ($operation[0] == 'EQ') {
  264. $a = array_pop($stack);
  265. $b = array_pop($stack);
  266. array_push($stack, $a == $b? 1: 0);
  267. return array(0, $stack);
  268. }
  269. if ($operation[0] == 'NE') {
  270. $a = array_pop($stack);
  271. $b = array_pop($stack);
  272. array_push($stack, $a != $b? 1: 0);
  273. return array(0, $stack);
  274. }
  275. if ($operation[0] == 'GT') {
  276. $a = array_pop($stack);
  277. $b = array_pop($stack);
  278. array_push($stack, $a > $b? 1: 0);
  279. return array(0, $stack);
  280. }
  281. if ($operation[0] == 'GTE') {
  282. $a = array_pop($stack);
  283. $b = array_pop($stack);
  284. array_push($stack, $a >= $b? 1: 0);
  285. return array(0, $stack);
  286. }
  287. }

运行结果:

  1. stack:
  2. -- PUSH 1
  3. stack: 1
  4. -- PUSH 1
  5. stack: 1, 1
  6. -- LABEL 1000
  7. stack: 1, 1
  8. -- DUP
  9. stack: 1, 1, 1
  10. -- SWAP2
  11. stack: 1, 1, 1
  12. -- DUP
  13. stack: 1, 1, 1, 1
  14. -- SWAP3
  15. stack: 1, 1, 1, 1
  16. -- ADD
  17. stack: 1, 1, 2
  18. -- DUP
  19. stack: 1, 1, 2, 2
  20. -- PRINT
  21. ++ PRINT: 2
  22. stack: 1, 1, 2
  23. -- DUP
  24. stack: 1, 1, 2, 2
  25. -- PUSH 10
  26. stack: 1, 1, 2, 2, 10
  27. -- SWAP1
  28. stack: 1, 1, 2, 10, 2
  29. -- MOD
  30. stack: 1, 1, 2, 2
  31. -- PUSH 0
  32. stack: 1, 1, 2, 2, 0
  33. -- NE
  34. stack: 1, 1, 2, 1
  35. -- JMPZ 2000
  36. stack: 1, 1, 2
  37. -- SWAP1
  38. stack: 1, 2, 1
  39. -- SWAP2
  40. stack: 1, 2, 1
  41. -- POP
  42. stack: 1, 2
  43. -- JMP 1000
  44. stack: 1, 2
  45. -- LABEL 1000
  46. stack: 1, 2
  47. -- DUP
  48. stack: 1, 2, 2
  49. -- SWAP2
  50. stack: 2, 2, 1
  51. -- DUP
  52. stack: 2, 2, 1, 1
  53. -- SWAP3
  54. stack: 1, 2, 1, 2
  55. -- ADD
  56. stack: 1, 2, 3
  57. -- DUP
  58. stack: 1, 2, 3, 3
  59. -- PRINT
  60. ++ PRINT: 3
  61. stack: 1, 2, 3
  62. -- DUP
  63. stack: 1, 2, 3, 3
  64. -- PUSH 10
  65. stack: 1, 2, 3, 3, 10
  66. -- SWAP1
  67. stack: 1, 2, 3, 10, 3
  68. -- MOD
  69. stack: 1, 2, 3, 3
  70. -- PUSH 0
  71. stack: 1, 2, 3, 3, 0
  72. -- NE
  73. stack: 1, 2, 3, 1
  74. -- JMPZ 2000
  75. stack: 1, 2, 3
  76. -- SWAP1
  77. stack: 1, 3, 2
  78. -- SWAP2
  79. stack: 2, 3, 1
  80. -- POP
  81. stack: 2, 3
  82. -- JMP 1000
  83. stack: 2, 3
  84. -- LABEL 1000
  85. stack: 2, 3
  86. -- DUP
  87. stack: 2, 3, 3
  88. -- SWAP2
  89. stack: 3, 3, 2
  90. -- DUP
  91. stack: 3, 3, 2, 2
  92. -- SWAP3
  93. stack: 2, 3, 2, 3
  94. -- ADD
  95. stack: 2, 3, 5
  96. -- DUP
  97. stack: 2, 3, 5, 5
  98. -- PRINT
  99. ++ PRINT: 5
  100. stack: 2, 3, 5
  101. -- DUP
  102. stack: 2, 3, 5, 5
  103. -- PUSH 10
  104. stack: 2, 3, 5, 5, 10
  105. -- SWAP1
  106. stack: 2, 3, 5, 10, 5
  107. -- MOD
  108. stack: 2, 3, 5, 5
  109. -- PUSH 0
  110. stack: 2, 3, 5, 5, 0
  111. -- NE
  112. stack: 2, 3, 5, 1
  113. -- JMPZ 2000
  114. stack: 2, 3, 5
  115. -- SWAP1
  116. stack: 2, 5, 3
  117. -- SWAP2
  118. stack: 3, 5, 2
  119. -- POP
  120. stack: 3, 5
  121. -- JMP 1000
  122. stack: 3, 5
  123. -- LABEL 1000
  124. stack: 3, 5
  125. -- DUP
  126. stack: 3, 5, 5
  127. -- SWAP2
  128. stack: 5, 5, 3
  129. -- DUP
  130. stack: 5, 5, 3, 3
  131. -- SWAP3
  132. stack: 3, 5, 3, 5
  133. -- ADD
  134. stack: 3, 5, 8
  135. -- DUP
  136. stack: 3, 5, 8, 8
  137. -- PRINT
  138. ++ PRINT: 8
  139. stack: 3, 5, 8
  140. -- DUP
  141. stack: 3, 5, 8, 8
  142. -- PUSH 10
  143. stack: 3, 5, 8, 8, 10
  144. -- SWAP1
  145. stack: 3, 5, 8, 10, 8
  146. -- MOD
  147. stack: 3, 5, 8, 8
  148. -- PUSH 0
  149. stack: 3, 5, 8, 8, 0
  150. -- NE
  151. stack: 3, 5, 8, 1
  152. -- JMPZ 2000
  153. stack: 3, 5, 8
  154. -- SWAP1
  155. stack: 3, 8, 5
  156. -- SWAP2
  157. stack: 5, 8, 3
  158. -- POP
  159. stack: 5, 8
  160. -- JMP 1000
  161. stack: 5, 8
  162. -- LABEL 1000
  163. stack: 5, 8
  164. -- DUP
  165. stack: 5, 8, 8
  166. -- SWAP2
  167. stack: 8, 8, 5
  168. -- DUP
  169. stack: 8, 8, 5, 5
  170. -- SWAP3
  171. stack: 5, 8, 5, 8
  172. -- ADD
  173. stack: 5, 8, 13
  174. -- DUP
  175. stack: 5, 8, 13, 13
  176. -- PRINT
  177. ++ PRINT: 13
  178. stack: 5, 8, 13
  179. -- DUP
  180. stack: 5, 8, 13, 13
  181. -- PUSH 10
  182. stack: 5, 8, 13, 13, 10
  183. -- SWAP1
  184. stack: 5, 8, 13, 10, 13
  185. -- MOD
  186. stack: 5, 8, 13, 3
  187. -- PUSH 0
  188. stack: 5, 8, 13, 3, 0
  189. -- NE
  190. stack: 5, 8, 13, 1
  191. -- JMPZ 2000
  192. stack: 5, 8, 13
  193. -- SWAP1
  194. stack: 5, 13, 8
  195. -- SWAP2
  196. stack: 8, 13, 5
  197. -- POP
  198. stack: 8, 13
  199. -- JMP 1000
  200. stack: 8, 13
  201. -- LABEL 1000
  202. stack: 8, 13
  203. -- DUP
  204. stack: 8, 13, 13
  205. -- SWAP2
  206. stack: 13, 13, 8
  207. -- DUP
  208. stack: 13, 13, 8, 8
  209. -- SWAP3
  210. stack: 8, 13, 8, 13
  211. -- ADD
  212. stack: 8, 13, 21
  213. -- DUP
  214. stack: 8, 13, 21, 21
  215. -- PRINT
  216. ++ PRINT: 21
  217. stack: 8, 13, 21
  218. -- DUP
  219. stack: 8, 13, 21, 21
  220. -- PUSH 10
  221. stack: 8, 13, 21, 21, 10
  222. -- SWAP1
  223. stack: 8, 13, 21, 10, 21
  224. -- MOD
  225. stack: 8, 13, 21, 1
  226. -- PUSH 0
  227. stack: 8, 13, 21, 1, 0
  228. -- NE
  229. stack: 8, 13, 21, 1
  230. -- JMPZ 2000
  231. stack: 8, 13, 21
  232. -- SWAP1
  233. stack: 8, 21, 13
  234. -- SWAP2
  235. stack: 13, 21, 8
  236. -- POP
  237. stack: 13, 21
  238. -- JMP 1000
  239. stack: 13, 21
  240. -- LABEL 1000
  241. stack: 13, 21
  242. -- DUP
  243. stack: 13, 21, 21
  244. -- SWAP2
  245. stack: 21, 21, 13
  246. -- DUP
  247. stack: 21, 21, 13, 13
  248. -- SWAP3
  249. stack: 13, 21, 13, 21
  250. -- ADD
  251. stack: 13, 21, 34
  252. -- DUP
  253. stack: 13, 21, 34, 34
  254. -- PRINT
  255. ++ PRINT: 34
  256. stack: 13, 21, 34
  257. -- DUP
  258. stack: 13, 21, 34, 34
  259. -- PUSH 10
  260. stack: 13, 21, 34, 34, 10
  261. -- SWAP1
  262. stack: 13, 21, 34, 10, 34
  263. -- MOD
  264. stack: 13, 21, 34, 4
  265. -- PUSH 0
  266. stack: 13, 21, 34, 4, 0
  267. -- NE
  268. stack: 13, 21, 34, 1
  269. -- JMPZ 2000
  270. stack: 13, 21, 34
  271. -- SWAP1
  272. stack: 13, 34, 21
  273. -- SWAP2
  274. stack: 21, 34, 13
  275. -- POP
  276. stack: 21, 34
  277. -- JMP 1000
  278. stack: 21, 34
  279. -- LABEL 1000
  280. stack: 21, 34
  281. -- DUP
  282. stack: 21, 34, 34
  283. -- SWAP2
  284. stack: 34, 34, 21
  285. -- DUP
  286. stack: 34, 34, 21, 21
  287. -- SWAP3
  288. stack: 21, 34, 21, 34
  289. -- ADD
  290. stack: 21, 34, 55
  291. -- DUP
  292. stack: 21, 34, 55, 55
  293. -- PRINT
  294. ++ PRINT: 55
  295. stack: 21, 34, 55
  296. -- DUP
  297. stack: 21, 34, 55, 55
  298. -- PUSH 10
  299. stack: 21, 34, 55, 55, 10
  300. -- SWAP1
  301. stack: 21, 34, 55, 10, 55
  302. -- MOD
  303. stack: 21, 34, 55, 5
  304. -- PUSH 0
  305. stack: 21, 34, 55, 5, 0
  306. -- NE
  307. stack: 21, 34, 55, 1
  308. -- JMPZ 2000
  309. stack: 21, 34, 55
  310. -- SWAP1
  311. stack: 21, 55, 34
  312. -- SWAP2
  313. stack: 34, 55, 21
  314. -- POP
  315. stack: 34, 55
  316. -- JMP 1000
  317. stack: 34, 55
  318. -- LABEL 1000
  319. stack: 34, 55
  320. -- DUP
  321. stack: 34, 55, 55
  322. -- SWAP2
  323. stack: 55, 55, 34
  324. -- DUP
  325. stack: 55, 55, 34, 34
  326. -- SWAP3
  327. stack: 34, 55, 34, 55
  328. -- ADD
  329. stack: 34, 55, 89
  330. -- DUP
  331. stack: 34, 55, 89, 89
  332. -- PRINT
  333. ++ PRINT: 89
  334. stack: 34, 55, 89
  335. -- DUP
  336. stack: 34, 55, 89, 89
  337. -- PUSH 10
  338. stack: 34, 55, 89, 89, 10
  339. -- SWAP1
  340. stack: 34, 55, 89, 10, 89
  341. -- MOD
  342. stack: 34, 55, 89, 9
  343. -- PUSH 0
  344. stack: 34, 55, 89, 9, 0
  345. -- NE
  346. stack: 34, 55, 89, 1
  347. -- JMPZ 2000
  348. stack: 34, 55, 89
  349. -- SWAP1
  350. stack: 34, 89, 55
  351. -- SWAP2
  352. stack: 55, 89, 34
  353. -- POP
  354. stack: 55, 89
  355. -- JMP 1000
  356. stack: 55, 89
  357. -- LABEL 1000
  358. stack: 55, 89
  359. -- DUP
  360. stack: 55, 89, 89
  361. -- SWAP2
  362. stack: 89, 89, 55
  363. -- DUP
  364. stack: 89, 89, 55, 55
  365. -- SWAP3
  366. stack: 55, 89, 55, 89
  367. -- ADD
  368. stack: 55, 89, 144
  369. -- DUP
  370. stack: 55, 89, 144, 144
  371. -- PRINT
  372. ++ PRINT: 144
  373. stack: 55, 89, 144
  374. -- DUP
  375. stack: 55, 89, 144, 144
  376. -- PUSH 10
  377. stack: 55, 89, 144, 144, 10
  378. -- SWAP1
  379. stack: 55, 89, 144, 10, 144
  380. -- MOD
  381. stack: 55, 89, 144, 4
  382. -- PUSH 0
  383. stack: 55, 89, 144, 4, 0
  384. -- NE
  385. stack: 55, 89, 144, 1
  386. -- JMPZ 2000
  387. stack: 55, 89, 144
  388. -- SWAP1
  389. stack: 55, 144, 89
  390. -- SWAP2
  391. stack: 89, 144, 55
  392. -- POP
  393. stack: 89, 144
  394. -- JMP 1000
  395. stack: 89, 144
  396. -- LABEL 1000
  397. stack: 89, 144
  398. -- DUP
  399. stack: 89, 144, 144
  400. -- SWAP2
  401. stack: 144, 144, 89
  402. -- DUP
  403. stack: 144, 144, 89, 89
  404. -- SWAP3
  405. stack: 89, 144, 89, 144
  406. -- ADD
  407. stack: 89, 144, 233
  408. -- DUP
  409. stack: 89, 144, 233, 233
  410. -- PRINT
  411. ++ PRINT: 233
  412. stack: 89, 144, 233
  413. -- DUP
  414. stack: 89, 144, 233, 233
  415. -- PUSH 10
  416. stack: 89, 144, 233, 233, 10
  417. -- SWAP1
  418. stack: 89, 144, 233, 10, 233
  419. -- MOD
  420. stack: 89, 144, 233, 3
  421. -- PUSH 0
  422. stack: 89, 144, 233, 3, 0
  423. -- NE
  424. stack: 89, 144, 233, 1
  425. -- JMPZ 2000
  426. stack: 89, 144, 233
  427. -- SWAP1
  428. stack: 89, 233, 144
  429. -- SWAP2
  430. stack: 144, 233, 89
  431. -- POP
  432. stack: 144, 233
  433. -- JMP 1000
  434. stack: 144, 233
  435. -- LABEL 1000
  436. stack: 144, 233
  437. -- DUP
  438. stack: 144, 233, 233
  439. -- SWAP2
  440. stack: 233, 233, 144
  441. -- DUP
  442. stack: 233, 233, 144, 144
  443. -- SWAP3
  444. stack: 144, 233, 144, 233
  445. -- ADD
  446. stack: 144, 233, 377
  447. -- DUP
  448. stack: 144, 233, 377, 377
  449. -- PRINT
  450. ++ PRINT: 377
  451. stack: 144, 233, 377
  452. -- DUP
  453. stack: 144, 233, 377, 377
  454. -- PUSH 10
  455. stack: 144, 233, 377, 377, 10
  456. -- SWAP1
  457. stack: 144, 233, 377, 10, 377
  458. -- MOD
  459. stack: 144, 233, 377, 7
  460. -- PUSH 0
  461. stack: 144, 233, 377, 7, 0
  462. -- NE
  463. stack: 144, 233, 377, 1
  464. -- JMPZ 2000
  465. stack: 144, 233, 377
  466. -- SWAP1
  467. stack: 144, 377, 233
  468. -- SWAP2
  469. stack: 233, 377, 144
  470. -- POP
  471. stack: 233, 377
  472. -- JMP 1000
  473. stack: 233, 377
  474. -- LABEL 1000
  475. stack: 233, 377
  476. -- DUP
  477. stack: 233, 377, 377
  478. -- SWAP2
  479. stack: 377, 377, 233
  480. -- DUP
  481. stack: 377, 377, 233, 233
  482. -- SWAP3
  483. stack: 233, 377, 233, 377
  484. -- ADD
  485. stack: 233, 377, 610
  486. -- DUP
  487. stack: 233, 377, 610, 610
  488. -- PRINT
  489. ++ PRINT: 610
  490. stack: 233, 377, 610
  491. -- DUP
  492. stack: 233, 377, 610, 610
  493. -- PUSH 10
  494. stack: 233, 377, 610, 610, 10
  495. -- SWAP1
  496. stack: 233, 377, 610, 10, 610
  497. -- MOD
  498. stack: 233, 377, 610, 0
  499. -- PUSH 0
  500. stack: 233, 377, 610, 0, 0
  501. -- NE
  502. stack: 233, 377, 610, 0
  503. -- JMPZ 2000
  504. stack: 233, 377, 610
  505. -- LABEL 2000
  506. stack: 233, 377, 610

11.2 重写step函数

  1. /*
  2. 0 表示状态
  3. 2 表示跳
  4. */
  5. function step($stack, $shallow, $deep, $action, $immediate)
  6. {
  7. if($action == 'JMP' ){
  8. array(0, $stack, $shallow, $deep);
  9. array(1);
  10. array(2, $jump)
  11. }
  12. }

11.3 运行举例

  1. execute('PUSH 0 LOAD PUSH 1000 GT JMPZ 1000 HALT LABEL 1000 PUSH 1 LOAD PUSH 200 SWAP1 WRITE');
  2. /*
  3. associative array
  4. 6820 -> 200
  5. 1977 -> 100
  6. PUSH 100 PUSH 6820 WRITE
  7. PUSH 200 PUSH 1977 WRITE
  8. PUSH 1977 READ
  9. PUSH 6820 READ
  10. */

11.4 代码

第18次课程代码

  1. $program = 'PUSH 0 LOAD PUSH 1000 GT JMPZ 1000 HALT LABEL 1000 PUSH 1 LOAD PUSH 200 SWAP1 WRITE';
  2. list($shallow, $deep) = execute($program, array(0 => 2000, 1 => 7440), array(7440 => 100, 6508 => 200));
  3. var_dump($deep);
  4. /*
  5. associative array
  6. 6820 -> 200
  7. 1977 -> 100
  8. PUSH 100 PUSH 6820 WRITE
  9. PUSH 200 PUSH 1977 WRITE
  10. PUSH 1977 READ
  11. PUSH 6820 READ
  12. */
  13. function convert($string) {
  14. $operations = array();
  15. $_0000 = explode(' ', $string);
  16. $pieces = array();
  17. foreach ($_0000 as $_0001) {
  18. $_0001 = trim($_0001);
  19. if (strlen($_0001) > 0)
  20. { $pieces[] = $_0001; }
  21. }
  22. $index = 0;
  23. while ($index < count($pieces)) {
  24. if ($pieces[$index] == 'PUSH' || $pieces[$index] == 'LABEL' || $pieces[$index] == 'JMP' || $pieces[$index] == 'JMPZ') {
  25. $operations[] = array($pieces[$index], $pieces[$index + 1]);
  26. $index += 2;
  27. continue;
  28. }
  29. $operations[] = array($pieces[$index]);
  30. $index++;
  31. }
  32. return $operations;
  33. }
  34. function render($stack) {
  35. $_0000 = '';
  36. foreach ($stack as $_0001) {
  37. $_0000 = sprintf('%s, %d', $_0000, $_0001);
  38. }
  39. return substr($_0000, 2);
  40. }
  41. function execute($string, $shallow, $deep) {
  42. return execute_0984(convert($string), $shallow, $deep);
  43. }
  44. function execute_0984($operations, $shallow, $deep) {
  45. $stack = array();
  46. echo sprintf('stack: %s%c', render($stack), 10);
  47. $index = 0;
  48. $length = count($operations);
  49. while($index < $length) {
  50. $_0000 = $operations[$index];
  51. echo sprintf('-- %s %s%c', $_0000[0], isset($_0000[1])? strval($_0000[1]): '', 10);
  52. $bundle = step($stack, $shallow, $deep, $_0000[0], $_0000[1]);
  53. if ($bundle[0] == 0) {
  54. echo sprintf('stack: %s%c', render($bundle[1]), 10);
  55. $stack = $bundle[1];
  56. $shallow = $bundle[2];
  57. $deep = $bundle[3];
  58. $index++;
  59. continue;
  60. }
  61. if ($bundle[0] == 1) {
  62. echo sprintf('-- SPECIAL EVENT: HALTED!! DIED!!%c', 10);
  63. break;
  64. }
  65. if ($bundle[0] == 2) {
  66. $index = find($operations, $bundle[2]);
  67. $stack = $bundle[1];
  68. if ($index == -1)
  69. { die('INVALID ADDRESS!!'); }
  70. continue;
  71. }
  72. }
  73. return array($shallow, $deep);
  74. }
  75. function find($operations, $label) {
  76. $index = 0;
  77. while ($index < count($operations)) {
  78. if ($operations[$index][0] != 'LABEL')
  79. { $index++; continue; }
  80. if ($operations[$index][35] == $label)
  81. { return $index; }
  82. $index++;
  83. }
  84. return -1;
  85. }
  86. function step($stack, $shallow, $deep, $action, $immediate)
  87. {
  88. if ($action == 'ADD') {
  89. $a = array_pop($stack);
  90. $b = array_pop($stack);
  91. array_push($stack, $a + $b);
  92. return array(0, $stack, $shallow, $deep);
  93. }
  94. if ($action == 'SUB') {
  95. $a = array_pop($stack);
  96. $b = array_pop($stack);
  97. array_push($stack, $a - $b);
  98. return array(0, $stack, $shallow, $deep);
  99. }
  100. if ($action == 'MUL') {
  101. $a = array_pop($stack);
  102. $b = array_pop($stack);
  103. array_push($stack, $a * $b);
  104. return array(0, $stack, $shallow, $deep);
  105. }
  106. if ($action == 'DIV') {
  107. $a = array_pop($stack);
  108. $b = array_pop($stack);
  109. array_push($stack, floor($a / $b));
  110. return array(0, $stack, $shallow, $deep);
  111. }
  112. if ($action == 'MOD') {
  113. $a = array_pop($stack);
  114. $b = array_pop($stack);
  115. array_push($stack, $a % $b);
  116. return array(0, $stack, $shallow, $deep);
  117. }
  118. if ($action == 'LT') {
  119. $a = array_pop($stack);
  120. $b = array_pop($stack);
  121. array_push($stack, $a < $b? 1: 0);
  122. return array(0, $stack, $shallow, $deep);
  123. }
  124. if ($action == 'LTE') {
  125. $a = array_pop($stack);
  126. $b = array_pop($stack);
  127. array_push($stack, $a <= $b? 1: 0);
  128. return array(0, $stack, $shallow, $deep);
  129. }
  130. if ($action == 'EQ') {
  131. $a = array_pop($stack);
  132. $b = array_pop($stack);
  133. array_push($stack, $a == $b? 1: 0);
  134. return array(0, $stack, $shallow, $deep);
  135. }
  136. if ($action == 'NE') {
  137. $a = array_pop($stack);
  138. $b = array_pop($stack);
  139. array_push($stack, $a != $b? 1: 0);
  140. return array(0, $stack, $shallow, $deep);
  141. }
  142. if ($action == 'GT') {
  143. $a = array_pop($stack);
  144. $b = array_pop($stack);
  145. array_push($stack, $a > $b? 1: 0);
  146. return array(0, $stack, $shallow, $deep);
  147. }
  148. if ($action == 'GTE') {
  149. $a = array_pop($stack);
  150. $b = array_pop($stack);
  151. array_push($stack, $a >= $b? 1: 0);
  152. return array(0, $stack, $shallow, $deep);
  153. }
  154. if ($action == 'AND') {
  155. $a = array_pop($stack);
  156. $b = array_pop($stack);
  157. array_push($stack, boolval($a) && boolval($b)? 1: 0);
  158. return array(0, $stack, $shallow, $deep);
  159. }
  160. if ($action == 'OR') {
  161. $a = array_pop($stack);
  162. $b = array_pop($stack);
  163. array_push($stack, boolval($a) || boolval($b)? 1: 0);
  164. return array(0, $stack, $shallow, $deep);
  165. }
  166. if ($action == 'XOR') {
  167. $a = array_pop($stack);
  168. $b = array_pop($stack);
  169. array_push($stack, (boolval($a) && ! boolval($b)) || (! boolval($a) && boolval($b))? 1: 0);
  170. return array(0, $stack, $shallow, $deep);
  171. }
  172. if ($action == 'NOT') {
  173. $a = array_pop($stack);
  174. array_push($stack, boolval($a)? 0: 1);
  175. return array(0, $stack, $shallow, $deep);
  176. }
  177. if ($action == 'JMP') {
  178. return array(2, $stack, $immediate);
  179. }
  180. if ($action == 'JMPZ') {
  181. $a = array_pop($stack);
  182. if ($a == 0)
  183. return array(2, $stack, $immediate);
  184. return array(0, $stack, $shallow, $deep);
  185. }
  186. if ($action == 'LABEL') {
  187. return array(0, $stack, $shallow, $deep);
  188. }
  189. if ($action == 'HALT') {
  190. return array(1);
  191. }
  192. if ($action == 'PUSH') {
  193. array_push($stack, $immediate);
  194. return array(0, $stack, $shallow, $deep);
  195. }
  196. if ($action == 'POP') {
  197. array_pop($stack);
  198. return array(0, $stack, $shallow, $deep);
  199. }
  200. if ($action == 'DUP') {
  201. $a = array_pop($stack);
  202. array_push($stack, $a);
  203. array_push($stack, $a);
  204. return array(0, $stack, $shallow, $deep);
  205. }
  206. if ($action == 'STORE') {
  207. $a = array_pop($stack);
  208. $b = array_pop($stack);
  209. $shallow[$a] = $b;
  210. return array(0, $stack, $shallow, $deep);
  211. }
  212. if ($action == 'LOAD') {
  213. $a = array_pop($stack);
  214. array_push($stack, isset($shallow[$a]) ? $shallow[$a] : 0);
  215. return array(0, $stack, $shallow, $deep);
  216. }
  217. if ($action == 'WRITE') {
  218. $a = array_pop($stack);
  219. $b = array_pop($stack);
  220. $deep[$a] = $b;
  221. return array(0, $stack, $shallow, $deep);
  222. }
  223. if ($action == 'READ') {
  224. $a = array_pop($stack);
  225. array_push($stack, isset($deep[$a]) ? $deep[$a] : 0);
  226. return array(0, $stack, $shallow, $deep);
  227. }
  228. if ($action == 'SWAP1') {
  229. $a = array_pop($stack);
  230. $b = array_pop($stack);
  231. array_push($stack, $a);
  232. array_push($stack, $b);
  233. return array(0, $stack, $shallow, $deep);
  234. }
  235. if ($action == 'SWAP2') {
  236. $a = array_pop($stack);
  237. $b = array_pop($stack);
  238. $c = array_pop($stack);
  239. array_push($stack, $a);
  240. array_push($stack, $b);
  241. array_push($stack, $c);
  242. return array(0, $stack, $shallow, $deep);
  243. }
  244. if ($action == 'SWAP3') {
  245. $a = array_pop($stack);
  246. $b = array_pop($stack);
  247. $c = array_pop($stack);
  248. $d = array_pop($stack);
  249. array_push($stack, $a);
  250. array_push($stack, $c);
  251. array_push($stack, $b);
  252. array_push($stack, $d);
  253. return array(0, $stack, $shallow, $deep);
  254. }
  255. }

执行结果:

  1. stack:
  2. -- PUSH 0
  3. stack: 0
  4. -- LOAD
  5. stack: 2000
  6. -- PUSH 1000
  7. stack: 2000, 1000
  8. -- GT
  9. stack: 0
  10. -- JMPZ 1000
  11. -- LABEL 1000
  12. stack:
  13. -- PUSH 1
  14. stack: 1
  15. -- LOAD
  16. stack: 7440
  17. -- PUSH 200
  18. stack: 7440, 200
  19. -- SWAP1
  20. stack: 200, 7440
  21. -- WRITE
  22. stack:
  23. array(2) {
  24. [7440]=>
  25. string(3) "200"
  26. [6508]=>
  27. int(200)
  28. }

16. 资料链接

课程相关链接

课程直播地址:
http://www.itdks.com/dakashuo/playback/1428

第13次课程屏幕录屏:
http://pan.baidu.com/s/1nviNO1b

老师开发的wiki,记录一些知识:
http://d111.learningchain.cn

比特币与区块链介绍

对未来产生影响最大的科技
http://open.163.com/movie/2016/9/P/1/MC0Q7LQR3_MC0Q97OP1.html

比特币原理图示与解释
https://visual.ly/community/infographic/technology/bitcoin-infographic

区块链技术文档

Bitcoin: A Peer-to-Peer Electronic Cash System, Satoshi Nakamoto
https://bitcoin.org/bitcoin.pdf

比特币白皮书:一种点对点的电子现金系统 中文翻译版, 中本聪
http://www.8btc.com/wiki/bitcoin-a-peer-to-peer-electronic-cash-system

GitHub - bitcoinbook/bitcoinbook: Mastering Bitcoin 2nd Edition - Programming the Open Blockchain https://github.com/bitcoinbook/bitcoinbook
精通比特币中文第1版
http://book.8btc.com/master_bitcoin
精通比特币第二版中文版
http://book.8btc.com/masterbitcoin2cn

Merkle Tree(默克尔树)算法解析
http://blog.csdn.net/wo541075754/article/details/54632929

介绍几本关于比特币和区块链的书
https://www.zhihu.com/question/35541188

比特币背后的密码学原理
http://www.jianshu.com/p/225ff9439132

侧链

解释与白皮书:
A SIMPLE EXPLANATION OF BITCOIN “SIDECHAINS”
https://gendal.me/2014/10/26/a-simple-explanation-of-bitcoin-sidechains/

sidechains
https://www.slideshare.net/crainbf/sidechains-presentation

blockstream公司的开源侧链模板项目elements:
https://elementsproject.org/

区块链架构与开发

区块链技术可视化演示

演示视频:
http://www.iqiyi.com/w_19ruazv201.html
演示学习网站:
http://blockchaindemo.io/
https://anders.com/blockchain/
可视化演示代码:
https://github.com/anders94/blockchain-demo

bitcoin script

bitcoin Script wiki说明
https://en.bitcoin.it/wiki/Script

在线script演示

https://webbtc.com/script
http://www.crmarsh.com/script-playground/

比特币开发

官方网站开发
https://bitcoin.org/en/development
https://bitcoin.org/en/developer-guide

bitcoin wiki(非官方,确是最完整的比特币文档wiki)
https://en.bitcoin.it/wiki/Main_Page

bitcoin 协议规格
https://en.bitcoin.it/wiki/Protocol_documentation

比特币难度公式:
https://en.bitcoin.it/Difficulty
https://en.bitcoin.it/wiki/Target

其实并没有什么比特币,只有 UTXO
http://8btc.com/article-4381-1.html
比特币UTXO的原理?
https://www.zhihu.com/question/59913301

区块链应用

区块链技术是什么?未来可能用于哪些方面?
https://www.zhihu.com/question/27687960

https://blockchain.info/

以太坊

wiki
https://github.com/ethereum/wiki/wiki
以太坊白皮书
https://github.com/ethereum/wiki/wiki/White-Paper
以太坊白皮书中文版
https://github.com/ethereum/wiki/wiki/%5B%E4%B8%AD%E6%96%87%5D-%E4%BB%A5%E5%A4%AA%E5%9D%8A%E7%99%BD%E7%9A%AE%E4%B9%A6
http://8btc.com/thread-2918-1-1.html

以太坊黄皮书英文版
https://ethereum.github.io/yellowpaper/paper.pdf
以太坊黄皮书中文版(关于以太坊技术的实现规范)
https://github.com/yuange1024/ethereum_yellowpaper/blob/master/Paper_Chinese.pdf
http://download.cxyym.com/blockchain/ethereum_yellowpaper_cn.pdf

网上比特币相关公开课程

在线演示区块链原理:
http://blockchaindemo.io/
http://anders.com/blockchain/

比特币和数字货币技术-Princeton University课程
https://www.coursera.org/learn/cryptocurrency

斯坦福大学公开课MOOC:比特币工程学
https://bitcoin.stanford.edu/

计算机基础

什么是图灵完备?
https://www.zhihu.com/question/20115374

内存堆和栈的区别
http://www.cnblogs.com/lln7777/archive/2012/03/14/2396164.html
数据结构之用栈实现逆波兰表达式
http://www.codes51.com/article/detail_1076640.html

parrot 虚拟机
http://www.oschina.net/p/parrot
php的数组的实现介绍
http://nikic.github.io/2012/03/28/Understanding-PHPs-internal-array-implementation.html

编程语言

php file 函数教程:
http://www.w3school.com.cn/php/php_ref_filesystem.asp

虚拟机原理与实现

How to implement a simple dalvik virtual machine
https://www.slideshare.net/ssusere3af56/how-to-implement-a-simple-dalvik-virtual-machine
register简单虚拟机
http://www.cnblogs.com/unixfy/p/3280264.html
基于栈的虚拟机的实现
http://www.cppblog.com/kevinlynx/archive/2010/04/15/112704.html
栈虚拟机源码剖析
http://www.cnblogs.com/unixfy/p/3335874.html
实现一个堆栈虚拟机
http://www.cnblogs.com/unixfy/p/3337917.html

网络编程

beej's Guide to Network Programming(2016)

UNIX Network Programming(1990) by W Richard Stevens

https://www.kernel.org/doc/man-pages Michael kerrish维护,网站分为8个部分

利用select/poll监听多个设备详解
http://blog.csdn.net/qq_28090573/article/details/51094321

密码学基础:

密码学算法应用机制
https://www.zybuluo.com/zhongdao/note/950527

一个图文并茂的比特币与其中应用的密码学算法的入门简介文章:
https://www.myblockchainblog.com/blog/blockchain-cryptography

What is a Digital Signature?
http://www.youdzone.com/signature.html

markdown 编辑器

markdown editor:
https://github.com/cloose/CuteMarkEd
cmd markdown editor:
https://www.zybuluo.com/

区块链技术公开课三期的公众号报道

http://mp.weixin.qq.com/s/GKXUHEHd44mGqH05SgRkBA
http://mp.weixin.qq.com/s/d0hBmFFqxWl995ZbEKqkHw
http://mp.weixin.qq.com/s/qoVkB_xzaoRNAPkaPJWddw
http://mp.weixin.qq.com/s/kF_G9bkefJdbx24wcoJ0IA
http://mp.weixin.qq.com/s/hZi0RFrjMEYCdVsiE95E6w

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注