发布于 2022-10-08 23:04:29
好吧,想清楚了。以下是如何:
函数将如何工作的基本示例(使用硬编码字符串,重点是显示应该如何准备内存)--我不是显示周围的代码,而是显示关键功能:
function getString() {
mstore(0x00, 0x20) # you need to say where in *the return data* (btw. not relative to your own memory) the string starts (aka where it's length is stored - here it's 0x20)
mstore(0x20, 0xe) # then the length of the string, let's say it's 14 bytes
mstore(0x40, 0x737461636B6F766572666C6F7721000000000000000000000000000000000000) # the string to return in hex
return(0, 0x60)
}注意:对于字符串:重要的是将它与0's放在右边,否则字节将全部位于32字节单词的右侧。如果字符串大于32个字节,则需要保持mstore()'ing,直到整个字符串位于内存中(当然,在长度部分仍然以字节为单位反映全文长度)。
如果省略了最初的32字节‘S (mstore(0x00, 0x20)),它就不能正常工作。
https://ethereum.stackexchange.com/questions/137112
复制相似问题