Becavalier / Book-DISO-WebAssembly

A book related repository which name is 《深入浅出 WebAssembly》
MIT License
72 stars 17 forks source link

对于data段的描述存在疑惑 #14

Open r3turn00 opened 5 years ago

r3turn00 commented 5 years ago

书中第181页,举例说“i32.const 16 指出该内存段需要填充的数据偏移量为16个32位整数的长度,即64字节。“

想请问一下,这里是指偏移为64个字节,还是初始化的数据占64个字节,也就是offset和length的区别。

个人觉得对于data段讲得不是很清楚,从data段的功能而言——初始化内存,是既需要offset也需要length的,并且数据的存储是否遵循小端序等等也没有提及,希望作者能给一个例子,描述得更清楚一些~

非常感谢!

Becavalier commented 4 years ago

Hi, sorry for the delay. For example the following instructions stores an i32 “123” at byte range [4..7]:

i32.const 4    // byte-sized address
i32.const 123  // value to store
i32.store 2 0  // 2 = 32-bit/4-byte alignment, 0 = offset

The first immediate of the instruction "i32.store" is the alignment hint and the second immediate indicates the offset based on the base address you provide by operands (i32.const 4). So, the final address should be the base_addr + offset_addr.