This example extends the program in example 1 to decode both I-format and R-format instructions. A union is used to give dual assignments for the least-signficant 16 bits of the instruction. See From C to Machine Instructions I from which the test instructions were taken.
C source files: bitfields.zip
01: #include <stdio.h> 02: 03: typedef union { 04: struct { 05: unsigned int fn:6; 06: unsigned int sh:5; 07: unsigned int rd:5; 08: }; 09: struct { 10: signed int immed:16; 11: unsigned int rt:5; 12: unsigned int rs:5; 13: unsigned int opcode:6; 14: }; 15: unsigned int w; 16: } mips_format_t; 17: 18: 19: int main() 20: { 21: int i; 22: mips_format_t ia; 23: unsigned int codes[] = {0x27bdffe8, 0xAFBE0010, 0x03A0F021, 0x2402000E}; 24: int n = sizeof(codes)/sizeof(int); 25: for (i=0; i<n; i++) { 26: ia.w = codes[i]; 27: printf("\ninstruction: %X\n",ia.w); 28: printf("opcode: %X\n",ia.opcode); 29: printf("rs: %d rt: %d\n", ia.rs, ia.rt); 30: if (ia.opcode==0) { 31: printf("rd: %d sh: %d fn: %X\n",ia.rd,ia.sh,ia.fn); 32: } 33: else { 34: printf("immed: %d\n",ia.immed); 35: } 36: } 37: return 0; 38: }
Line 24, shown below, determines the length of the codes array
int n = sizeof(codes)/sizeof(int);sizeof(codes) returns the number of bytes in the array codes.
instruction: 27BDFFE8 opcode: 9 rs: 29 rt: 29 immed: -24 instruction: AFBE0010 opcode: 2B rs: 29 rt: 30 immed: 16 instruction: 3A0F021 opcode: 0 rs: 29 rt: 0 rd: 30 sh: 0 fn: 21 instruction: 2402000E opcode: 9 rs: 0 rt: 2 immed: 14
Maintained by John Loomis, updated Thu Aug 07 21:09:52 2008