string - 将字符串常量转换为虚拟机代码
问题描述
我有一个学校项目,可以将千斤顶程序翻译成 vm 代码。我们得到了一些模糊的指示,让我们自己去做。
我在将字符串常量转换为 vm 代码时遇到问题。
假设程序中有一个命令如下:
do Output.printString("Hello world!")
翻译将是这样的:
push SOMETHING (I don't know what to put here)
call Output.printString 1
如何将字符串翻译成 vm?
谢谢!
解决方案
这是我想出的:
有两个函数用于创建字符串。String.new
和String.appendChar
。
要创建一个字符串,首先调用String.new
字符串的长度作为参数。然后,调用String.appendChar
每个字符,将当前字符的 ascii 值作为函数的参数。
因此,例如,要创建字符串"Hello World"
:
push constant 11 //this is the length of the word
call String.new 1
push constant 72 //ascii of h
call String.appendChar 2
//continue this for each character in the word
...
call Output.printString 1 //prints the string that we created
推荐阅读
- javascript - 参考对象的可观察链
- c++ - GDB 在调试时多次显示行
- c# - 当模型是具体类型并且现在是接口时如何进行 mongoDB 迁移
- loops - 将对象列表与地图 Dart 中的信息相结合
- angular - 如何在 [(ngModel)] 中使用 Map()?
- javascript - JavaScript 购买 SDK - Shopify
- javascript - 沙盒以外的不同属性可在您的网站上显示网页
- node.js - 从 node express 中的 html 输入返回或查找对象
- android-studio - 使用proguard时执行失败
- redis - Kubernetes 中的 redis-ha 无法故障转移回主服务器