Back

blockchain - solidity - huff - 2.安装和初见

发布时间: 2024-04-02 09:01:00

refer to:https://docs.huff.sh/get-started/installing/#installing-huff

安装:(奇了怪了,我一直无法安装),后来在tokyo服务器上安装的

curl -L get.huff.sh | bash

声明一个ABI

声明一个macro

 

#define function addTwo(uint256, uint256) view returns (uint256)

#define macro MAIN() = takes(0) returns(0) {  // 从stack 0 的位置取, 返回的结果也是一样
  0x00 calldataload    // 0x00:  0 
  0x20 calldataload    // 0x20:  就是32,  32 bytes = 256 bit 
  add

  0x00 mstore
  0x20 0x00 return
}

编译:

huffc addTwo.huff --bytecode

各种解释:

600c8060093d393df35f35602035015f5260205ff3 (我的机器上)

600f8060093d393df36000356020350160005260206000f3 (官方文档上)

可以看到, 600f8060093d393df3  前面这些都是废代码(框架代码)

下面是对后续的bytes的分析:

BYTECODE          MNEMONIC         STACK                 ACTION
 60 00          // PUSH1 0x00       // [0x00]
 35             // CALLDATALOAD     // [number1]          Store the first 32 bytes on the stack
 60 20          // PUSH1 0x20       // [0x20, number1]
 35             // CALLDATALOAD     // [number2, number1] Store the second 32 bytes on the stack
 01             // ADD              // [number2+number1]  Take two stack inputs and add the result
 60 00          // PUSH1 0x00       // [0x0, (n2+n1)]
 52             // MSTORE           // []                 Store (n2+n1) in the first 32 bytes of memory
 60 20          // PUSH1 0x20       // [0x20]
 60 00          // PUSH1 0x00       // [0x00, 0x20]
 f3             // RETURN           // []                 Return the first 32 bytes of memory

Back