Thread: MIPS instructions

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    7

    MIPS instructions

    Code:
    #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.

    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?

    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

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There's a FAQ on why it's not a good idea to use feof to control loops. With that out of the way:
    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
    Try using fseek to seek to that point in the file and then begin your reading from there. Also, using things like ftell and fseek in combination will allow you to tell if your data is a multiple of 4 bytes or not. Then you simply do something like fread to read into your unsigned long (assuming that's actually 4 bytes on your machine).

    Oh, and if you're reading into a structure, you need to read either the size of the structure into the structure instance's name, or the size of that data member into the member of the function. Also, you may want to test the return value of your fread to make sure it hasn't failed.


    Quzah.
    Last edited by quzah; 08-24-2005 at 12:03 AM.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > ive noted tt the MIPS text segment begins at 0x00400000.
    Ask lots of questions about how the bin file was saved in the first place. It seems unlikely to me that the first byte in the file was from address 0 in the memory.

    Perhaps the first byte in the file came from 0x00400000 and all you need do is add that constant to your address printing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange 4 assembly instructions for a function
    By George2 in forum Windows Programming
    Replies: 1
    Last Post: 07-21-2008, 02:59 AM
  2. Need help with Mips
    By NoobieGecko in forum C Programming
    Replies: 11
    Last Post: 02-25-2008, 02:57 PM
  3. Mixing user mode and OS instructions
    By Canadian0469 in forum C Programming
    Replies: 2
    Last Post: 11-05-2007, 03:49 AM
  4. Emulating x86 Instructions
    By SMurf in forum C Programming
    Replies: 1
    Last Post: 12-18-2005, 12:16 PM
  5. reading instructions
    By 8ball in forum C Programming
    Replies: 3
    Last Post: 06-04-2004, 09:44 AM