如何将 "Hello World" 加载到 WebAssembly wast 中的 .data 部分

How to load "Hello World" into the `.data` section in WebAssembly wast

我正在查看 WebAssembly tests,看到 data:

(module
  (memory $m 1)
  (data (i32.const 0))
  (data (i32.const 1) "a" "" "bcd")
  (data (offset (i32.const 0)))
  (data (offset (i32.const 0)) "" "a" "bc" "")
  (data 0 (i32.const 0))
  (data 0x0 (i32.const 1) "a" "" "bcd")
  (data 0x000 (offset (i32.const 0)))
  (data 0 (offset (i32.const 0)) "" "a" "bc" "")
  (data $m (i32.const 0))
  (data $m (i32.const 1) "a" "" "bcd")
  (data $m (offset (i32.const 0)))
  (data $m (offset (i32.const 0)) "" "a" "bc" "")
)

想知道将 "Hello World" 放入 data 部分会是什么样子。我是这个低级功能的新手。类似于:

(data "Hello World")

在 x86 汇编中你可以这样做:

message:  db "Hello, World", 10

您可以找到 data segment syntax within the WebAssembly Text Format specification 的定义。真的很简单,这里是更简单的形式:

data $memidx $offset $data

其中 $memidx 是一个可选索引,指示数据段填充哪个模块内存。在当前版本的 WebAssembly 中,仅支持 single module memory,因此它将始终为零。

$offset是常量表达式,表示数据写入的起始位置。

最后$data就是数据本身了。它可以表示文本或二进制数据,使用简单的编码 detailed in the specification.

对于您的示例,要将 Hello World 添加到偏移量为零的模块内存,这将完成工作:

(data (i32.const 0) "Hello World")