Recursion 在IJVM中编写递归函数

Recursion 在IJVM中编写递归函数,recursion,Recursion,我目前正在编写一个ijvm程序,用于递归求解河内塔。我不确定应该如何为方法中调用的方法设置对象引用的编号 这是我用来“翻译”成ijvm代码的java代码: 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); } e

我目前正在编写一个ijvm程序,用于递归求解河内塔。我不确定应该如何为方法中调用的方法设置对象引用的编号

这是我用来“翻译”成ijvm代码的java代码:

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);
  }
}
 ;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"
这是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);
  }
}
 ;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"

对象引用只是一个占位符,因为IJVM不实现对象定向。您可以在那里放置任何值。它将在INVOKEVIRTUAL例程中被覆盖,但不会被读取