Hi guys,
do any of u know how to extract a certain amount of bits from a parameter(instruction) and return the bits as a integer in a range .

for example i would like to extract the opcode field i would extract from bits 31 to 26.
but if i wan to extract from bits 25 to 21 for R type registers, how can i go about it? Some help pls?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void displayBits( unsigned value );

int main()
{
	unsigned long x; /*user input variable*/
	printf("Enter an long integer: ");
	scanf ("%ld", &x );

	displayBits( x );

	return 0; /*indicates sucessful termination*/

} /*end main*/

/* display bits of an unsigned integer value*/
void displayBits( unsigned  value )
{
	unsigned c; /* counter */

	/* define displayMask and left shift 31 bits */
	unsigned displayMask = 1 << 31;

	printf("%10ld = ", value);

	/*loop through bits */
	for ( c=1; c<=8; c++){
		putchar( value & displayMask ? '1' :'0');
		value <<= 1; /*shift value left by 1*/

		if( c% 8 ==0){ /*output space after 8 bits*/
			putchar( ' ' );
		}/*end if*/
	}/*end for*/

	putchar(' \n ');
}/* end function displayBits */

do any of u know how to extract a certain amount of bits from a parameter(instruction) and return the bits as a integer in a range .

[thread=17834]This thread[/thread] may be of interest.

#include <stdio.h>
#include <stdlib.h>

    union uVariant {

        // Long
        unsigned long num;

        // Bits
        struct {
            unsigned char bit31 : 6;

            unsigned char bit25 : 5;

            unsigned char bit20 : 1;
            unsigned char bit19 : 1;
            unsigned char bit18 : 1;
            unsigned char bit17 : 1;
            unsigned char bit16 : 1;
            unsigned char bit15 : 1;
            unsigned char bit14 : 1;
            unsigned char bit13 : 1;
            unsigned char bit12 : 1;
            unsigned char bit11 : 1;
            unsigned char bit10 : 1;
            unsigned char bit09 : 1;
            unsigned char bit08 : 1;
            unsigned char bit07 : 1;
            unsigned char bit06 : 1;
            unsigned char bit05 : 1;
            unsigned char bit04 : 1;
            unsigned char bit03 : 1;
            unsigned char bit02 : 1;
            unsigned char bit01 : 1;
            unsigned char bit00 : 1;

        };
           };



int main()
{
    long    nTestLong = 0x3e5AF3;

	union uVariant flags;
   	flags.num = nTestLong;

    printf( "%d\n", (int)flags.bit25);
}

i managed to figure out how to extract different sections of the bits in the instruction, but supposedly if i like to convert the above code into a function . how can i do so ?
in the main will be where the input instruction will be. but how do i put the struct into a function and print from there?
some help pls?

I have another problem.
supposedly i have to read a .bin file.
i have to make sure tt the length is in a multiple of 4 bytes. and store each four byte into a unsigned long variable and print the variable in hexa.

i would use the fopen to read the file in binary .
however im having problems trying to read the file in four 'bytes'...
how am i suppose to break the file into bytes?


some help pls?
thanks.

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

struct VarData{
	unsigned long var;
};


int main()
{
  FILE * mFile;

  struct VarData data;


  mFile = fopen ("b3-long-sample.bin" , "rb");
  if (mFile == NULL) { /* If there was a problem, show the error */
    perror ("Error opening file");
  }
  else {

	  while( !feof( mFile ) ) {

		  fread( &data, sizeof( struct VarData ), 1, mFile);

		  	printf("The address: %08lx\n", &data);
    		printf("The word: %08lx\n", data.var);
		}

   }
return 0;
}

this is the attempt i made so far.
im wondering if this is right.
also the address is the same for each word ...
but i have to print the address before each instruction is loaded into.
ive noted tt the MIPS text segment begins at 0x00400000.
but i have can't seems to acheive that.
can some1 tell me what the problem is ?
thanks

This prints the address of the variable data.

printf("The address: %08lx\n", &data);

Have you posted a sample of the data file?

[edit]And now I'm thoroughly confused by trying to follow all of the cross posting (cprogramming, devshed, gidforums, cppworld, and here). Rather than continue on with your presumed solutions and various grabs from different sources, how about summarizing for us what you have for input and what you expect for output?

This prints the address of the variable data.

printf("The address: %08lx\n", &data);

Have you posted a sample of the data file?

[edit]And now I'm thoroughly confused by trying to follow all of the cross posting (cprogramming, devshed, gidforums, cppworld, and here). Rather than continue on with your presumed solutions and various grabs from different sources, how about summarizing for us what you have for input and what you expect for output?

i have my b3-long-sample.bin file for input
it is a file containing machine excutable MIPS programs.
i need to determine whether it is in lengths of 4 bytes and read the contents into an unsigned long and print out in hexa.

Also, i like to print the address before each instruction is loaded into.

I didn't know that my posting in various forums confuses you... But they are actually the same posts in every forums....so if you just look at one forum, it will suffice.

I still like to thank you, Dave for your help so far.

i have my b3-long-sample.bin file for input
it is a file containing machine excutable MIPS programs.

Right, I still don't have it, so I'll have to make up my own "pretend" version.

i need to determine whether it is in lengths of 4 bytes and read the contents into an unsigned long and print out in hexa.

Bytes are bytes. Read until you run out.

Also, i like to print the address before each instruction is loaded into.

I'm skipping that part for the moment.

I didn't know that my posting in various forums confuses you... But they are actually the same posts in every forums....so if you just look at one forum, it will suffice.

The sequence in which I encounter them has generally been: answer, answer, leadup, scroll back, okay... question. Multiply that times five. Then jumble the sequence.

Anyways, pieced together somewhat with the other question that I assume is still part of this -- bit masking -- here's some kind of example that I'm sure is not what you want. But I'm hoping you can focus your question.

#include <stdio.h>

/* http://www.daniweb.com/code/snippet149.html */
char *bits_ulong(char *dest, unsigned long value)
{
   char *start = dest;
   unsigned long bit;
   for ( bit = (-1UL >> 1) + 1; bit > 0; bit >>= 1 )
   {
      *dest++ = value & bit ? '1' : '0';
   }
   *dest = 0;
   return start;
}

int main(void)
{
   static const char filename[] = __FILE__;
   FILE *file = fopen(filename, "rb");
   if ( file )
   {
      unsigned long value;
      char buffer[33];
      while ( fread(&value, sizeof value, 1, file) == 1 )
      {
         unsigned long r = (value & 0xFC000000) >> 26;
         unsigned long i = (value & 0x03E00000) >> 21;
         unsigned long j =  value & 0x001FFFFF;
         printf("%08lX : %s : %02lX %02lX %06lX\n", 
                value, bits_ulong(buffer, value), r, i, j);
      }
      fclose(file);
   }
   else
   {
      perror(filename);
   }
   return 0;
}

Feeding its own source code to the the program produces this (truncated).

636E6923 : 01100011011011100110100100100011 : 18 1B 0E6923
6564756C : 01100101011001000111010101101100 : 19 0B 04756C
74733C20 : 01110100011100110011110000100000 : 1D 03 133C20
2E6F6964 : 00101110011011110110100101100100 : 0B 13 0F6964
0A0D3E68 : 00001010000011010011111001101000 : 02 10 0D3E68
68630A0D : 01101000011000110000101000001101 : 1A 03 030A0D
2A207261 : 00101010001000000111001001100001 : 0A 11 007261
73746962 : 01110011011101000110100101100010 : 1C 1B 146962
6F6C755F : 01101111011011000111010101011111 : 1B 1B 0C755F
6328676E : 01100011001010000110011101101110 : 18 19 08676E
20726168 : 00100000011100100110000101101000 : 08 03 126168
7365642A : 01110011011001010110010000101010 : 1C 1B 05642A

[edit]It also seems to me that you may want to visit http://www.wotsit.org/ and look up the PE and EXE formats.

This article has been dead for over six months. Start a new discussion instead.