【问题标题】:Translating String Constants to Virtual Machine Code将字符串常量转换为虚拟机代码
【发布时间】:2021-10-11 10:00:41
【问题描述】:

我有一个项目供学校将 Jack 程序转换为 vm 代码。我们得到了一些模糊的指示,让我们自己去做。
我在将字符串常量转换为 vm 代码时遇到问题。
假设程序中有如下命令:
do Output.printString("Hello world!")
翻译应该是这样的:

push SOMETHING (I don't know what to put here) 
call Output.printString 1

如何将字符串翻译成 vm?

谢谢!

【问题讨论】:

    标签: string compilation constants


    【解决方案1】:

    这是我想出的:
    有两个函数用于创建字符串。 String.newString.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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多