2013-01-25 29 views
0

約二進制信息假定的指令aw是由一個32位的結構定義的代碼010,如下所示:獲取從十進制

bits 31-25 unused (all 0s) 
bits 24-22: code 
bits 21-19: argument 1 
bits 18-16: argument 2 
bits 15-0: offset (a 16-bit, 2's complement number with a range of -32768 to 32767) 

鑑於號8454151,我怎樣才能確定該代碼是aw

我試着移動數字22位,就像8454151 >> 22,但我一直得到0.關於如何獲得代碼的位信息(檢查它是否是aw或其他)的任何想法?

回答

0

有點變化應該工作。一定要檢查數據類型。你也可以用「0x01C000」和數字進行比較。

+0

我寧願編碼這個具有常量的幻數或定義它的構建可以理解的部分(比如'7 << 22'或'2 << 22'),以使其更加真實爲其他人服務。 – junix

+0

確保您在換班時使用4字節整數。 – xpda

2

如果你只需要驗證,如果一個指令是有一定的操作,代碼需要最少的週期將是如下:

const uint32_t mask = 7 << 22; // Shift 3 set bits by 22 in order to build 
            // a mask where bits 22-24 are set. 
const uint32_t inst_aw = 2 << 22; // Shift the opcode by 22 to build a comparable value 

uint32_t instruction = ...;  // Your instruction word 

if ((instruction & mask) == inst_aw) { 
    // Do your thing 
} 

無論如何,如果你要建立類似的「指令解碼器」或「解釋器」,我建議使用帶有指令代碼索引的指令名稱(或函數指針)的查找表:

/*! \brief Instruction names lookup table. Make sure it has always 8 entries 
      in order to avoid access beyond the array limits */ 
const char *instruction_names[] = { 
    /* 000 */ "Unknown instruction 000", 
    /* 001 */ "Unknown instruction 001", 
    /* 010 */ "AW", 
    ... 
    /* 111 */ "Unknown instruction 111" 
}; 


uint32_t instruction = ...; 
unsigned int opcode = (instruction >> 22) & 0x07; // Retrieving the opcode by shifting right 22 
                // times and mask out all bit except the last 3 

printf("Instruction is: %s", instruction_names[opcode]);