1

hi i built a vm in c and want to read a bin file. This bin file has a structure, I can read them and compile this. but i dont know how i can save the value as unsigned int in a array :/

The binary file has this structure:

    4 Bytes -> Verify the format identifier
    4 Bytes -> version number
    4 Bytes -> number of instructions
    4 Bytes -> number of variables

n*4 bytes instructions

Here is my code

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

unsigned  int buffer[300];

int main(int argc, char *argv[]) {

    FILE *file = fopen("Programm1.bin","r+b");

    printf("VM start...\n\n");

    if(file != NULL){

       if(1 == fread(&buffer, sizeof(unsigned int),1,file)){

            //printf("%c ", buffer[0]);
            //printf("%c ", buffer[1]);
            //printf("%c ", buffer[2]);
            //printf("%c ", buffer[3]);

            //printf("%x",buffer[4]);
        }
        else
        {
            printf("Der Header der Datei ist ungültig...");
            return(99);
        }
    }
    else
    {
        printf("Datei konnte nicht geöffnet werden...");
        return(99);
    }

    fclose(file);

return 0;
}

every instruction is 32 Bit => 4 Byte. The first Byte is the number of the instruction and the rest is the value

For Example:

0000 0001 0000 0000 0000 0000 0000 0011 => PUSH 3 (because PUSH is 1)

I want to save every instruction as unsigned int in a array.

I have the bin file and if I use hexdump -C i get this:

(For Information: The output is on head that mean it is from right to left)

enter image description here

The first 16 Byte are the header.

The rest are the instructions.

If I compile my Code in Clion IDE Then it doesn#t work but if I compile the code in gcc on the Terminal then I get this:

enter image description here

My Problems are that i dont know how i can save the instructions as unsigned int in my array and why Clion IDE it downst compile... -.-

(Maybe I can configured th Clion IDE to gcc compiler :/ )

but importent are the instructions to the array

thanks for help :)

Seki
  • 11,135
  • 7
  • 46
  • 70
Tarasov
  • 3,625
  • 19
  • 68
  • 128
  • I don't think that your buffer is big enough... You say that each line requires 32 bits, but your buffer is of size 30. Additionally it isn't account for the spaces separating the 4 parts of the line. – kyle Nov 02 '15 at 22:00
  • 2
    What you have in `buffer` is already the binary data. You've just chosen to print it out as a character. That is, the underlying value is the the same. How you choose to interpret it can be different. So for example, if you printed out `buffer[0]` as a hex int with `%x` you would get `4E`. If you want to read the entire integer then just change the buffer to be an array of `uint32_t` instead of `char`. – kaylum Nov 02 '15 at 22:02
  • but it dont work with unsigned int buffer[200] for example and i dont get a error or a warning in gcc :( – Tarasov Nov 02 '15 at 22:07
  • 1
    "it don't work" is not a description of the problem. Explain the input, ouput and expected behavior. – hexasoft Nov 02 '15 at 22:09
  • the input is the same how in the picture...i dont get a output...by this line it stop -> if(1 == fread(&buffer, sizeof(buffer),10,file)) – Tarasov Nov 02 '15 at 22:11
  • 1
    What do you mean "it stops"? It crashes? The program exits? The condition is never true? And perhaps you mean `1` and not `10` in the `fread`. – kaylum Nov 02 '15 at 22:14
  • how i can see why it stops ....the gcc have by comile the file no errors or warnings. I get only this of my code --> printf("Der Header der Datei ist ungültig..."); – Tarasov Nov 02 '15 at 22:18
  • i change the 10 to 1 – Tarasov Nov 02 '15 at 22:19
  • You need `fread(buffer, sizeof(uint32_t), 1, file) == 1` if you want to read one instruction. Not `sizeof(buffer)`. Anyway, the question is diverging from the original and it's no longer clear what the state of your code is. Please update your question with what your current code looks like and what it currently does. – kaylum Nov 02 '15 at 22:24
  • i make a update in my code – Tarasov Nov 02 '15 at 22:38

0 Answers0