Skip to content

Commit 6c25413

Browse files
name66888Const123456nanaitStevewJinzorro215
authored
任务48 区块链供应链金融应收款保理融资 (#122)
* Task任务38:时间锁 (#94) * Create a * Update a * 任务30:链上治理 (#99) * MultiSign * Ballot Co-authored-by: Steve Jin <steve.w.jin@gmail.com> * Task 28 国密算法支持 提供国密算法使用相关的工具和实例合约 (#98) Co-authored-by: tangxianjie <xianjie.tang@vonechain.com> * Task任务37:自定义任务:许可证合约和最小代理合约 (#102) * fix redundant return values * one (#96) * commit Co-authored-by: luswar <luswar@163.com> Co-authored-by: zye-xdu <92678671+zye-xdu@users.noreply.github.com> Co-authored-by: Lu-zihang <3303147337@qq.com> * 第3季任务26:实现通过块高来查询值的历史状态的功能 (#103) * Create VerifySignature.sol * 数字签名验签合约更新2 补充库合约 * Task35 历史状态快照合约 (#133) * feat(HistorySnapshot):新增HistorySnapshotV2合约 * refactor(history_snapshot):修改注释 * refactor(history_snapshot):更新注释 * feat(history_snapshot):新增历史状态快照合约测试demo * feat(history_snapshot):新增HistorySnapshotV2文档 * TASK40 多方授权执行交易合约 (#132) * TASK39 编写合约源码+注释+合约功能解析(时间锁合约) * Revert "TASK39 编写合约源码+注释+合约功能解析(时间锁合约)" This reverts commit f810a3d. * feat(multiparty_authorization):添加多方授权执行交易合约 * feat(multiparty_authorization):添加多方授权执行交易合约说明文档 * feat(multiparty_authorization):添加多方授权执行交易合约测试文档 * Task任务 自定义解决方案 企业碳排放解决方案合约案例 助力双碳#48 (#110) * #48 自定义解决方案 企业碳排放解决方案合约案例 助力双碳 * Update 企业碳排放解决方案合约案例 助力双碳.md * Task 41 智能合约时间触发器 * Update Solidity合约和时间触发器实现待办事项.md * Delete Solidity合约和时间触发器实现待办事项.md * Task41 Solidity合约时间触发器实现 * Rename Solidity合约和时间触发器实现待办事项.md to Solidity智能合约时间触发器实现待办事项.md * Update 企业碳排放解决方案合约案例 助力双碳.md * 企业碳排放方案智能合约 * TASK 45 慈善公益智能合约案例解决方案 * Delete CharityFund.zip * Delete 慈善公益解决方案.md * TASK 45 慈善公益智能合约解决方案 * Delete CharityFund.zip * Delete 慈善公益解决方案.md * TASK 45 慈善公益智能合约解决方案 Markdown + 慈善公益合约压缩包 * 知识产权管理合约 (#114) * 自定义任务40-基于区块链的分级诊疗数据共享案例分析 (#83) * DevSecretBallot (#86) * longjj2016 (#78) 1.修改之前问题,改为以下方法 10进制ascii转字符串 16进制ascii转字符串 2.文档描述更新 * 添加剪刀石头布的合约玩法 (#79) * add solidity file * add tutorial * add basic solidity tutorials * add assets * divergence * add divergence game * 增加func_call_contract.sol 及doc (#80) * Base64编解码库 (#81) * Counter: SimpleCounter; MultiSigCounter (#82) * 32 医疗卫生合约通用模版 (#84) * #32 医疗卫生合约模版 * 完善#32 医疗合约模版 * Solidity开发教程进阶版 (#85) * add solidity file * add tutorial * add basic solidity tutorials * add assets * divergence * add divergence game * advance solidity tutorial init * solidity tutorial advance Co-authored-by: longJJ2016 <1017303686@qq.com> Co-authored-by: 高野 <yekai1003@163.com> Co-authored-by: itherunder <61953384+itherunder@users.noreply.github.com> Co-authored-by: Kesling <43846775+Kesling515@users.noreply.github.com> Co-authored-by: kimroniny <1301862177@qq.com> Co-authored-by: Ferwoo <wufeiphil@163.com> * fix redundant return values * one (#96) * 知识产权管理合约 --------- Co-authored-by: wzl521 <63525530+wzl521@users.noreply.github.com> Co-authored-by: JqLiu123 <85717832+JqLiu123@users.noreply.github.com> Co-authored-by: longJJ2016 <1017303686@qq.com> Co-authored-by: 高野 <yekai1003@163.com> Co-authored-by: itherunder <61953384+itherunder@users.noreply.github.com> Co-authored-by: Kesling <43846775+Kesling515@users.noreply.github.com> Co-authored-by: kimroniny <1301862177@qq.com> Co-authored-by: Ferwoo <wufeiphil@163.com> Co-authored-by: luswar <luswar@163.com> Co-authored-by: dalaocu <njumjy06@126.com> Co-authored-by: zye-xdu <92678671+zye-xdu@users.noreply.github.com> Co-authored-by: linxuanming <2594771947@qq.com> * TASK任务 48 金融供应链解决方案合约案例 (#115) * TASK任务 48 金融供应链解决方案合约案例 * Update 金融供应链解决方案 合约案例.md * Task39 类型转换工具合约 (#131) * feat(base_type):新增TypeUtils合约 * improvement(base_type):修改合约类型(contract->library) * refactor(base_type):修正函数命名 * feat(base_type):新增TypeUtilsDemo * feat(docs):新增TypeUtils文档 * TASK39 编写合约源码+注释+合约功能解析(时间锁合约) (#125) * TASK39 编写合约源码+注释+合约功能解析(时间锁合约) * Revert "TASK39 编写合约源码+注释+合约功能解析(时间锁合约)" This reverts commit f810a3d. * feat:管理员合约 * feat(timelock):数学计算合约,方便时间戳上的计算 * feat(timelock):时间锁合约源码+注释 * feat(timelock):时间锁测试合约 * feat(timelock):时间锁合约说明文档以及测试案例的结果 * fix(timelock):说明文档中的测试结果图片添加到图床 * TASK48 自定义任务 Solidity Assembly内联汇编 (#120) --------- Co-authored-by: Const123456 <103564714+Const123456@users.noreply.github.com> Co-authored-by: nanait <664135787@qq.com> Co-authored-by: Steve Jin <steve.w.jin@gmail.com> Co-authored-by: zorro215 <280514870@qq.com> Co-authored-by: tangxianjie <xianjie.tang@vonechain.com> Co-authored-by: 0xEvmLuna <116624440+0xEvmLuna@users.noreply.github.com> Co-authored-by: luswar <luswar@163.com> Co-authored-by: zye-xdu <92678671+zye-xdu@users.noreply.github.com> Co-authored-by: Lu-zihang <3303147337@qq.com> Co-authored-by: defaultthree <107139725+defaultthree@users.noreply.github.com> Co-authored-by: Yu Cui <87604354+cuiyuchain@users.noreply.github.com> Co-authored-by: dalaocu <njumjy06@126.com> Co-authored-by: xun chen <ashinnotfound@qq.com> Co-authored-by: qilong li <97083300+L7L9@users.noreply.github.com> Co-authored-by: 张宇豪 <84267606+CN-ZHANGYH@users.noreply.github.com> Co-authored-by: power <244875489@qq.com> Co-authored-by: wzl521 <63525530+wzl521@users.noreply.github.com> Co-authored-by: JqLiu123 <85717832+JqLiu123@users.noreply.github.com> Co-authored-by: longJJ2016 <1017303686@qq.com> Co-authored-by: 高野 <yekai1003@163.com> Co-authored-by: itherunder <61953384+itherunder@users.noreply.github.com> Co-authored-by: Kesling <43846775+Kesling515@users.noreply.github.com> Co-authored-by: kimroniny <1301862177@qq.com> Co-authored-by: Ferwoo <wufeiphil@163.com> Co-authored-by: linxuanming <2594771947@qq.com> Co-authored-by: jxnuwjy <100462197+jxnuwjy@users.noreply.github.com> Co-authored-by: hior-y <126435017+hior-y@users.noreply.github.com>
1 parent 4817b8a commit 6c25413

22 files changed

+4254
-0
lines changed

Solidity Assembly内联汇编.md

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
# Solidity Assembly 内联汇编
2+
3+
> 学校:深职院
4+
>
5+
> 作者:钟胜宇
6+
7+
## 1. 什么是内联汇编?
8+
9+
内联汇编是一种在底层访问以太坊虚拟机的语言,由于编译器无法对汇编语句进行检查,所以 Solidity 提供的很多重要安全特性都没办法作用于汇编。写汇编代码相对比较困难,很多时候只有在处理一些相对复杂的问题时才需要使用它,并且开发者需要明确知道自己要做什么。
10+
11+
在合约的内部使用汇编,是在合约内部包含 `assembly` 关键字进行编写的,在 Solidity `inline assembly`(内联汇编) 中的语言被称为 Yul。Yul除了在 Solidity 之中作为 inline assembly 的一部分,也能当作独立的直译语言能够被编译成 bytecode 给不同的后端。
12+
13+
## 2. Yul语言的介绍
14+
15+
官网地址: https://docs.soliditylang.org/zh/latest/yul.html
16+
17+
上面说到内联汇编中的语言被称为Yul,可以简单的看一下官网的Yul语言的案例和语法就可以。
18+
19+
![image-20230528224900766](https://blog-1304715799.cos.ap-nanjing.myqcloud.com/imgs/202305282249947.webp)
20+
21+
22+
23+
## 3. 合约中内联汇编支持的指令
24+
25+
1. `add/sub/mul/div/mod`: 加、减、乘、除和取模运算。
26+
2. `sdiv/smod`: 有符号整数除法和取模运算。
27+
3. `exp`: 指数运算,计算 e 的 x 次方。
28+
4. `and/or/xor/not`: 位运算 and、or、xor和not操作。
29+
5. `shl/shr/sar`: 位移运算,左移、右移和算术右移操作。
30+
6. `lt/gt/slt/sgt/eq`: 比较运算,小于、大于、有符号小于、有符号大于和等于操作。
31+
7. `address/balance/selfdestruct`: 地址相关操作,获取当前合约地址、查询某个地址持有的以太币数量和销毁当前合约自身。
32+
8. `extcodesize/extcodecopy`: 获取外部合约的代码大小并复制其代码到指定内存位置。
33+
9. `mload/mstore/mstore8`: 内存读写操作,读取指定地址处的内存、将数据存入指定地址处的内存和将一个字节存入指定地址处的内存。
34+
10. `gas`: 查询剩余的gas数量。
35+
36+
37+
38+
## 4. 内联汇编的基本格式
39+
40+
通过 `assembly {}` 包裹代码。并且内部每一行语句不需要使用`;`显示的标注结束。Assembly 也支持注释,可以使用 `//``/* */` 来进行注释。
41+
42+
let 指令执行如下任务:
43+
44+
- 创建一个新的堆栈槽位
45+
- 为变量保留该槽位
46+
- 当到达代码块结束时自动销毁该槽位
47+
48+
let 指令在汇编代码块中定义的变量,在代码块外部是无法访问的,内部代码块可以访问外部的代码块的内容。
49+
50+
```solidity
51+
pragma solidity 0.4.25;
52+
53+
contract Test {
54+
function test() public view returns (uint256) {
55+
assembly {
56+
let x := 1
57+
}
58+
assembly {
59+
// 当前的x是无法访问的
60+
let y := x
61+
}
62+
}
63+
}
64+
```
65+
66+
67+
68+
## 5. 内联汇编合约案例基本使用
69+
70+
定义了一个名为 `Test` 的合约,其中包含了一个名为 `addAssembly` 的公共函数。这个函数接受两个 uint256 类型的参数 `_x``_y`,并返回一个元组 `(uint256,uint256)`
71+
72+
- 这个函数使用了内联汇编 `assembly` 来计算 `_x``_y` 的和,并获取当前可用的 gas 数量。
73+
74+
- 定义了两个 `uint256` 类型的变量,分别为 `result``remainingGas`。然后,在 `assembly` 块中,调用了内置的 `add` 函数将 `_x``_y` 相加的结果赋值给 `result` 变量。
75+
76+
- 使用 `gas()` 操作获取当前可用的 gas 数量,并将其赋值给变量 `remainingGas`
77+
78+
- 返回 `(result, remainingGas)` 元组来输出计算结果和剩余的 gas 数量。
79+
80+
```solidity
81+
pragma solidity 0.4.25;
82+
83+
contract Test {
84+
85+
function addAssembly(uint256 _x, uint256 _y) public returns (uint256,uint256) {
86+
uint256 result;
87+
uint256 remainingGas;
88+
assembly {
89+
// add(_x, _y) 是计算 x + y 的结果
90+
// := 是将 x + y 的结果赋值给变量 result
91+
result := add(_x, _y)
92+
93+
// 查询剩余的gas
94+
remainingGas := gas()
95+
}
96+
return (result,remainingGas);
97+
}
98+
}
99+
```
100+
101+
102+
103+
### 5.1 第一个内联汇编案例
104+
105+
> #### 使用WeBASE-Front部署合约
106+
107+
- 将合约部署到WeBASE-Front中
108+
- 调用addAssembly函数
109+
110+
> 但是有一些地址的余额这些就无法调用,需要更换成以太坊上部署solidity
111+
112+
![image-20230528224324862](https://blog-1304715799.cos.ap-nanjing.myqcloud.com/imgs/202305282243092.webp)
113+
114+
![image-20230528224431430](https://blog-1304715799.cos.ap-nanjing.myqcloud.com/imgs/202305282244520.webp)
115+
116+
查看交易回执:
117+
118+
- 30是内联汇编调用add指令相加后得到的结果
119+
- 299978109是当前剩余的gas
120+
121+
![image-20230528224504398](https://blog-1304715799.cos.ap-nanjing.myqcloud.com/imgs/202305282245512.webp)
122+
123+
124+
125+
126+
127+
128+
129+
### 5.2 位运算案例
130+
131+
`and/or/xor/not`: 位运算 and、or、xor和not操作。
132+
133+
```solidity
134+
contract BitwiseExample {
135+
function and(uint256 a, uint256 b) public pure returns (uint256) {
136+
uint256 result;
137+
assembly {
138+
result := and(a, b) // 位与操作,将 a 和 b 的二进制位分别进行与操作并返回结果
139+
}
140+
return result;
141+
}
142+
143+
function or(uint256 a, uint256 b) public pure returns (uint256) {
144+
uint256 result;
145+
assembly {
146+
result := or(a, b) // 位或操作,将 a 和 b 的二进制位分别进行或操作并返回结果
147+
}
148+
return result;
149+
}
150+
151+
function xor(uint256 a, uint256 b) public pure returns (uint256) {
152+
uint256 result;
153+
assembly {
154+
result := xor(a, b) // 位异或操作,将 a 和 b 的二进制位分别进行异或操作并返回结果
155+
}
156+
return result;
157+
}
158+
159+
function not(uint256 a) public pure returns (uint256) {
160+
uint256 result;
161+
assembly {
162+
result := not(a) // 位非操作,将 a 的二进制位取反并返回结果
163+
}
164+
return result;
165+
}
166+
}
167+
```
168+
169+
在WeBASE-Front上部署BitwiseExample合约调用and函数测试。
170+
171+
![image-20230528231524988](https://blog-1304715799.cos.ap-nanjing.myqcloud.com/imgs/202305282315092.webp)
172+
173+
![image-20230528231557848](https://blog-1304715799.cos.ap-nanjing.myqcloud.com/imgs/202305282315958.webp)
174+
175+
176+
177+
### 5.3 比较运算案例
178+
179+
`lt/gt/slt/sgt/eq`: 比较运算,小于、大于、有符号小于、有符号大于和等于操作。
180+
181+
```solidity
182+
pragma solidity ^0.4.25;
183+
184+
contract ComparisonExample {
185+
function lt(uint256 a, uint256 b) public pure returns (bool) {
186+
bool result;
187+
assembly {
188+
result := lt(a, b) // 比较 a 是否小于 b,若成立则返回 true,否则返回 false
189+
}
190+
return result;
191+
}
192+
193+
function gt(uint256 a, uint256 b) public pure returns (bool) {
194+
bool result;
195+
assembly {
196+
result := gt(a, b) // 比较 a 是否大于 b,若成立则返回 true,否则返回 false
197+
}
198+
return result;
199+
}
200+
201+
function eq(uint256 a, uint256 b) public pure returns (bool) {
202+
bool result;
203+
assembly {
204+
result := eq(a, b) // 比较 a 是否等于 b,若成立则返回 true,否则返回 false
205+
}
206+
return result;
207+
}
208+
}
209+
```
210+
211+
在WeBASE-Front上部署ComparisonExample合约调用eq函数测试。
212+
213+
![image-20230528232431517](https://blog-1304715799.cos.ap-nanjing.myqcloud.com/imgs/202305282324617.webp)
214+
215+
如下是交易回执:
216+
217+
![image-20230528232442366](https://blog-1304715799.cos.ap-nanjing.myqcloud.com/imgs/202305282324475.webp)
218+
219+
如上其他的内联编码的指令也是一样使用。

0 commit comments

Comments
 (0)