Sway编程语言-更新中(The Sway Programming Language-Updatin
  • Sway编程语言(The Sway Programming Language)
  • 1. 导言(Introduction)
    • 1.1 安装(Installation)
    • 1.2 Sway快速入门 (Sway Quickstart)
    • 1.3 Fuel工具链 (The Fuel Toolchain)
    • 1.4 一个Forc项目 (A Forc Project)
    • 1.5 标准库 (Standard Library)
  • 2. 示例(Example)
    • 2.1计数器(Counter)
    • 2.2子货币(Subcurrency)
    • 2.3 FizzBuzz
    • 2.4 钱包智能合约(Wallet Smart Contract)
  • 3.Sway编程类型(Sway Program Types)
    • 3.1 合约(Contracts)
    • 3.2 库 (Libraries)
    • 3.3 脚本(Scripts)
    • 3.4 谓词 (Predicates)
  • 4. Sway语言基础 (Sway Language basics)
    • 4.1 变量 (Variables)
    • 4.2 内置类型(Built-in Types)
    • 4.3 常用库类型(Commonly Used Library Types)
    • 4.4 区块链类 (Blockchain Types)
    • 4.5 函数 (Functions)
    • 4.6 结构、元祖和穷举 (Structs, Tuples, and Enums)
    • 4.7 方法和关联函数 (Methods and Associated Functions)
    • 4.8 注释和日志 (Comments and Logging)
    • 4.9 控制流 (Control Flow)
  • 5. 用Sway部署区块链 (Blockchain Development with Sway)
    • 5.1 哈希和加密学 (Hashing and Cryptography)
    • 5.2 合约存储(Contract Storage)
    • 5.3 函数纯度 (Function Purity)
    • 5.4 标识符(Identifiers)
    • 5.5 原生资产(Native Assets)
    • 5.6 访问控制 (Access Control)
    • 5.7 调用合约(Calling Contracts)
  • 6. 高级概念 (Advanced Concepts)
    • 6.1 高级类型 (Advanced Types)
    • 6.2 通用类型 (Generic Types)
    • 6.3 特征 (Traits)
    • 6.4 集 (Assembly)
  • 7. 一般集聚 (Common Collections)
    • 7.1 堆上的向量(Vectors on the Heap)
    • 7.2 存储向量 (Storage Vectors)
    • 7.3 存储映射 (Storage Maps)
  • 8.测试(Testing)
    • 8.1 单元测试(Unit Testing)
    • 8.2 用Rust来测试 (Testing with Rust)
  • 9.应用前端开发 (Application Frontend Development)
    • 9.1 TypeScript SDK
  • 10.Sway应用(Sway Reference)
    • 10.1 编译器内部函数(Compiler Intrinsics)
    • 10.2 属性(Attributes)
    • 10.3 风格向导(Style Guide)
    • 10.4 已知各类问题(Known Issues and Workarounds)
    • 10.5 与Solidity的不同之处 (Differences From Solidity)
    • 10.6 与Rust的不同之处 (Differences From Rust)
    • 10.7 向Sway贡献 (Contributing To Sway)
  • 11. Forc引用 (Forc Reference)
    • 11.1清单参考 (Manifest Reference)
    • 11.2 工作区(Workspaces)
    • 11.3 依赖(Dependencies)
    • 11.4 命令(Commands)
      • 11.4.1 forc-addr2line
      • 11.4.2 forc-build
      • 11.4.3 forc-check
Powered by GitBook
On this page
  • ASM块 (ASM Block)
  • 有用的网址 (Helpful Links)
  1. 6. 高级概念 (Advanced Concepts)

6.4 集 (Assembly)

While many users will never have to touch assembly language while writing sway code, it is a powerful tool that enables many advanced use-cases (ie: optimizations, building libraries, etc).

虽然许多用户在编写 sway 代码时永远不必接触汇编语言,但它是一个支持许多高级用例(即:优化、构建库等)的强大工具。

ASM块 (ASM Block)

In Sway, the way we use assembly inline is to declare an asm block like this:

在 Sway 中,我们使用内联汇编的方式是像这样声明一个 asm 块:

asm() {...}

Declaring an asm block is similar to declaring a function. We can specify register names to operate on as arguments, we can perform operations within the block, and we can return a value. Here's an example showing what this might look like:

声明一个 asm 块类似于声明一个函数。我们可以指定要操作的注册器名称作为参数,我们可以在块内执行操作,我们可以返回一个值。这是一个示例,显示了其外观:

pub fn add_1(num: u32) -> u32 {
    asm(r1: num, r2) {
        add r2 r1 one;
        r2: u32
    }
}

An asm block can only return a single register. If you really need to return more than one value, you can modify a tuple. Here's an example showing how can implement this (u64, u64):

一个 asm 块只能返回一个注册器。如果你真的需要返回多个值,你可以修改一个元组。下面是一个示例,展示了如何实现这个(u64, u64):

script;

fn adder(a: u64, b: u64, c: u64) -> (u64, u64) {
    let empty_tuple = (0u64, 0u64);
    asm(output: empty_tuple, r1: a, r2: b, r3: c, r4, r5) {
        add  r4 r1 r2; // add a & b and put the result in r4
        add  r5 r2 r3; // add b & c and put the result in r5
        sw   output r4 i0; // store the word in r4 in output + 0 words
        sw   output r5 i1; // store the word in r5 in output + 1 word
        output: (u64, u64) // return both values
    }
}

fn main() -> bool {
    let (first, second) = adder(1, 2, 3);
    assert(first == 3);
    assert(second == 5);
    true
}

Note that this is contrived example meant to demonstrate the syntax; there's absolutely no need to use assembly to add integers!

请注意,这是人为设计的示例,旨在演示语法;绝对不需要使用汇编来添加整数!

Note that in the above example:

请注意,在上面的示例中:

  • we initialized the register r1 with the value of num. 我们用 num 的值初始化了注册器 r1

  • we declared a second register r2 (you may choose any register names you want). 我们声明了第二个注册器 r2(您可以选择任何您想要的注册器名称)

  • we use the add opcode to add one to the value of r1 and store it in r2. 我们使用 add 操作码将 one 添加到 r1 的值并将其存储在 r2 中

  • one is an example of a "reserved register", of which there are 16 in total. Further reading on this is linked below under "Semantics". one 是“保留注册器”的示例,总共有 16 个。关于此的进一步阅读链接在下面的“语义”下

  • we return r2 & specify the return type as being u32 (the return type is u64 by default). 我们返回 r2 并将返回类型指定为 u32(返回类型默认为 u64)。

An important note is that the ji and jnei opcodes are not available within an asm block. For those looking to introduce control flow to asm blocks, it is recommended to surround smaller chunks of asm with control flow (if, else, and while).

需要注意的是,ji 和 jnei 操作码在 asm 块中不可用。对于那些希望将控制流引入 asm 块的人,建议用控制流(if、else 和 while)包围较小的 asm 块。

有用的网址 (Helpful Links)

Previous6.3 特征 (Traits)Next7. 一般集聚 (Common Collections)

Last updated 1 year ago

For examples of assembly in action, check out the . 有关运行中的程序集示例,请查看 。

For a complete list of all instructions supported in the FuelVM: . 如需 FuelVM 支持的所有指令的完整列表:。

And to learn more about the FuelVM semantics: . 并了解有关 FuelVM 语义的更多信息:。

Sway standard library
Sway 标准库
Instructions
说明
Semantics
语义