2014-10-07 21 views
0

我已經嘗試了幾分鐘來糾正在添加extractOperand方法後出現在代碼中的錯誤。我找不到任何語法或邏輯錯誤。另外,我注意到extractOpcode不會產生錯誤,而方法實際上是相同的。我現在的猜測是錯誤與函數的放置有關。理解和糾正錯誤消息的難度

import java.util.*; 
import java.lang.*; 
import java.io.*; 


public class Micro86 { 
    static int[] Memory = new int[20]; 
    static int accumulator = 0, 
       instruction_pointer = 0, 
       flags = 0, 
       instruction_register = 0; 

    public static void main(String[] args) 
    { 
     String m86File = args[0]; 

     bootUp(); 
     loader(m86File); 
     memoryDump(); 
    } 

    public static void bootUp() 
    { 
     for(int i : Memory) 
      i = 0; 
    } 

    public static void memoryDump() 
    { 
     for(int i: Memory) 
      System.out.println(left_pad_zeros(Integer.toHexString(i))); 
    } 

    public static String registerDump() 
    { 
     return "Registers acc: " + accumulator 
       + " ip: " + instruction_pointer 
       + " flags: " + flags 
       + "ir: " + instruction_register + "\n"; 
    } 

    public static void loader(String file) 
    { 

     try{ 
      Scanner sc = new Scanner(new File(file)); 
      for(int i = 0; sc.hasNextInt() ; ++i){ 
       Memory[i] = sc.nextInt(16); 
      } 

     }catch(Exception e) { 
      System.out.println("Cannot open file"); 
      System.exit(0); 
     } 
    } 

    public static int extractOpCode(int instruction) 
    { 
     return instruction >>> 16; 
    } 

    public static String left_pad_zeros(String str) 
    { 
     while(str.length() < 8) 
      str = "0" + str; 
     return str; 
    } 

    public static int extractOperand(int instruction) 
    { 
     return instruction <<< 16; 
    } 

} 

ERROR:

Micro86.java:71: illegal start of type 
    return instruction <<< 16; 
         ^
Micro86.java:71: illegal start of expression 
     return instruction <<< 16; 
           ^
    Micro86.java:71: ';' expected 
      return instruction <<< 16; 
           ^
    Micro86.java:74: reached end of file while parsing 
    } 
    ^
4 errors 
+0

@jpw這很有趣,那我爲什麼沒有得到extractOpcode方法同樣的錯誤?我在那時做了>>> – 2014-10-07 22:11:05

+1

很簡單,沒有這樣的操作符。 – 2014-10-07 22:12:11

回答

5

在此行中的extractOperand方法:

return instruction <<< 16; 

即使有一個無符號的向右移位運算>>>,沒有無符號左移位運算<<< ,因爲左移時沒有符號擴展。您可以使用正常的左移位運算符,<<

return instruction << 16;