我目前正在编写一个ijvm程序来递归地解决河内塔。我不确定如何为方法中调用的方法设置Object引用的数字。
这是我用来"翻译"的java代码。到ijvm代码:
public class Tower{
public static void towers(int n, int i, int j) {
int k;
if (n == 1) {
System.out.println("Move a disk from " + i + " to " + j);
} else {
k = 6 - i - j;
towers(n - 1, i, k);
towers(1, i, j);
towers(n - 1, k, j);
}
}
public static void main(String[] args){
towers(5,1,3);
}
}
这是ijvm代码:
;BIPUSH 0 is for the object reference
BIPUSH 0
BIPUSH 5
BIPUSH 1
BIPUSH 3
INVOKEVIRTUAL tower
HALT
;the recursive method
tower 4 1
ILOAD 1
BIPUSH 1
IF_ICMPEQ L1
BIPUSH 6
ILOAD 2
ISUB
ILOAD 3
ISUB
ISTORE 4
;this is for the method towers(n - 1, i, k);
;BIPUSH 1 is for the object reference
BIPUSH 1
ILOAD 1
BIPUSH 1
ISUB
ILOAD 2
ILOAD 4
INVOKEVIRTUAL tower
;this is for the method towers(1, i, j);
;BIPUSH 2 is for the object reference
BIPUSH 2
BIPUSH 1
ILOAD 2
ILOAD 3
INVOKEVIRTUAL tower
;this is for the method towers(n-1, k, j);
;BIPUSH 3 is for the object reference
BIPUSH 3
ILOAD 1
BIPUSH 1
ISUB
ILOAD 4
ILOAD 3
INVOKEVIRTUAL tower
L1: ILOAD 3
ILOAD 2
SPRINT "Move a disk from "
IPRINT
SPRINT " to "
IPRINT
SPRINT "\n"
答案 0 :(得分:0)
对象引用只是一个占位符,因为IJVM没有实现面向对象。你可以在那里放置任何价值。在INVOKEVIRTUAL例程中,它将被覆盖,但永远不会被读取。