Bitfield Example 1

The following typedef example defines the bit fields in an I-Format instruction for a MIPS processor.

 typedef union {
         struct {
                 signed int immed:16;
                 unsigned int rt:5;
                 unsigned int rs:5;
                 unsigned int opcode:6;
         };
         unsigned int w;
 } I_format_t;

Examination of the disassembly listing from the MPLAB IDE shows that the ext (extract bitfield) instruction is used to reference individual fields in the instruction.

20:	printf("rs: %d rt: %d\n", ia.rs, ia.rt);
   lw          v0,16(s8)     vo <= ia.w
   ext         v1,v0,21,5    v1 <= extract_bitfield(v0,21,5) note 1
   lw          v0,16(s8)     vo <= ia.w  note 2
   ext         a2,v0,16,5    a2 <= extract_bitfield(v0,16,5)
   lui         v0,0x9d07     a0 <= format_string
   addiu       a0,v0,16184
   addu        a1,v1,zero    a1 <= v1
   jal         0x9d000554    call printf
   nop         

The ext (extract bitfield) instruction is used to obtain the bit fields.

The arguments for printf are stored in a0, a1, and a2, respectively.

notes

  1. Why not use a1 as the destination register? You need the result there later.
  2. ia.w is already loaded, so this instruction is not necessary.

Contents

C Source
Results
Disassembly Listing

C Source

Download C source from bitfields.zip.


01: #include <stdio.h>
02: 
03: typedef union {
04:         struct {
05:                 signed int immed:16;
06:                 unsigned int rt:5;
07:                 unsigned int rs:5;
08:                 unsigned int opcode:6;
09:         };
10:         unsigned int w;
11: } I_format_t;
12: 
13: 
14: int main()
15: {
16:         I_format_t ia;
17:         ia.w = 0xAFBE0010;
18:         printf("\ninstruction: %X\n",ia.w);
19:         printf("opcode: %X\n",ia.opcode);
20:         printf("rs: %d rt: %d\n", ia.rs, ia.rt);
21:         printf("immed: %d\n",ia.immed);
22:         return 0;
23: }


Results

The program was compiled with Microsoft C and then run, redirecting the output to a file:

>cl bitfield1.c
>bitfield1 > bitfield1.txt

The resulting output file is included below

instruction: AFBE0010
opcode: 2B
rs: 29 rt: 30
immed: 16

This instruction was decoded to sw s8,16(sp). See From C to Machine Instructions I.

Disassembly Listing

The above code was modified slightly, as shown below, to run on the PIC32 Starter Kit. The output was the same.

---  C:\pic32\test\bitfield1.c  ------------------------------------------------------------------
1:                   #include 
2:                   #include "db_utils.h"
3:                   
4:                   typedef union {
5:                   	struct {
6:                   		signed int immed:16;
7:                   		unsigned int rt:5;
8:                   		unsigned int rs:5;
9:                   		unsigned int opcode:6;
10:                  	};
11:                  	unsigned int w;
12:                  } I_format_t;
13:                  
14:                  int main()
15:                  {
9D000018  27BDFFE0   addiu       sp,sp,-32
9D00001C  AFBF001C   sw          ra,28(sp)
9D000020  AFBE0018   sw          s8,24(sp)
9D000024  03A0F021   addu        s8,sp,zero
16:                  	I_format_t ia;
17:                  	ia.w = 0xAFBE0010;
9D000028  3C02AFBE   lui         v0,0xafbe
9D00002C  34420010   ori         v0,v0,0x10
9D000030  AFC20010   sw          v0,16(s8)
18:                  	printf("\ninstruction: %X\n",ia.w);
9D000034  3C029D07   lui         v0,0x9d07
9D000038  24443F18   addiu       a0,v0,16152
9D00003C  8FC50010   lw          a1,16(s8)
9D000040  0F400155   jal         0x9d000554
9D000044  00000000   nop         
19:                  	printf("opcode: %X\n",ia.opcode);
9D000048  8FC20010   lw          v0,16(s8)
9D00004C  7C432E80   ext         v1,v0,26,6
9D000050  3C029D07   lui         v0,0x9d07
9D000054  24443F2C   addiu       a0,v0,16172
9D000058  00602821   addu        a1,v1,zero
9D00005C  0F400155   jal         0x9d000554
9D000060  00000000   nop         
20:                  	printf("rs: %d rt: %d\n", ia.rs, ia.rt);
9D000064  8FC20010   lw          v0,16(s8)
9D000068  7C432540   ext         v1,v0,21,5
9D00006C  8FC20010   lw          v0,16(s8)
9D000070  7C462400   ext         a2,v0,16,5
9D000074  3C029D07   lui         v0,0x9d07
9D000078  24443F38   addiu       a0,v0,16184
9D00007C  00602821   addu        a1,v1,zero
9D000080  0F400155   jal         0x9d000554
9D000084  00000000   nop         
21:                  	printf("immed: %d\n",ia.immed);
9D000088  87C30010   lh          v1,16(s8)
9D00008C  3C029D07   lui         v0,0x9d07
9D000090  24443F48   addiu       a0,v0,16200
9D000094  00602821   addu        a1,v1,zero
9D000098  0F400155   jal         0x9d000554
9D00009C  00000000   nop         
22:                  	DBPUTS("Program terminated. Click HALT and then RESET to stop the microcontroller. \n");
9D0000A0  3C029D07   lui         v0,0x9d07
9D0000A4  24443F54   addiu       a0,v0,16212
9D0000A8  2405004C   addiu       a1,zero,76
9D0000AC  0F400034   jal         0x9d0000d0
9D0000B0  00000000   nop         
23:                  	return 0;
9D0000B4  00001021   addu        v0,zero,zero
24:                  }
9D0000B8  03C0E821   addu        sp,s8,zero
9D0000BC  8FBF001C   lw          ra,28(sp)
9D0000C0  8FBE0018   lw          s8,24(sp)
9D0000C4  27BD0020   addiu       sp,sp,32
9D0000C8  03E00008   jr          ra
9D0000CC  00000000   nop         


Maintained by John Loomis, updated Thu Aug 07 21:09:45 2008