Mixing C and Assembly

This is a discussion on Mixing C and Assembly within the C Programming forums, part of the General Programming Boards category; hi, i am having trouble understanding this problem. could anyone help me out? Write a C language program that compiles ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    2

    Mixing C and Assembly

    hi, i am having trouble understanding this problem.
    could anyone help me out?


    Write a C language program that compiles successfully as a borland project that reads char data from a streaming file and computes the odd parity of the 16 bit unsigned number and writes the 8 bit (7bit+parity bit) to an output file. EOF is NULL. Input file is Indata.dat. Output file is outdata.dat.

    "Odd parity" forces the number of "1s" to be odd including the parity bit. Some examples are:
    x1001110 -> 11001110
    x0011001 -> 00011001

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    I don't know why you'd need assembly to do this, but if you feel the need, you can include ASM code in C programs by "inlining" the asembly code. I believe that you can do this in Borland compilers by doing the following:
    Code:
    asm
    {
        ...
    }
    I would just read in the data from a file until the char I encountered was NULL (your EOF), than I'd figure out what the parity bit is, convert the data to the 8-bit number, and write that data to the file, all in one big loop.

    The general nature of your question suggests you're looking for someone to do this problem for you because you haven't done it yourself for whatever reasons. You won't find people very happy to help you if that is the case, so I suggest you show a little effort on your part. You can find file I/O tutorials at this site, and I believe you can also find an article that'll teach you how to work directly with binary data.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    XORing the bits of a value with a bit mask is one way to compute parity.
    Code:
       unsigned short mask;
       unsigned int result = 0;
       for ( mask = 0x40; mask; mask >>= 1 )
       {
          result ^= !!(mask & value);
       }
    The result of this will be 0 if there is an even number of bits in value, and 1 if there is an odd number of bits in value. But this is even parity; to get odd parity, invert the result.
    Code:
       if ( odd )
       {
          result = !result;
       }
    A way to combine this result with the initial value, in this situation, could be to OR the result at the MSB with the original value.
    Code:
       value |= result << 7;
    About the only thing I may have left out is a function declaration and return value.
    Code:
    unsigned short parity(unsigned short value, int odd)
    {
       /* ... */
       return value;
    }
    Or a test program that might produce these results:
    parity(0x4E,1) = 0xCE
    parity(0x19,1) = 0x19
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    2
    thank you.
    helped me alot.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21