This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

c dode for 24bit*8bit multiplication,result of 32bit should be divided by 32 bit

i want to multiply 24bit value which i get from spi ,i stored it in an array of 3byte & i want to multiply with a constant of 8bit,the result i get which is a 32bit has to be divided with a constant of 32bit
i want a c code for this procedure so pls help me

Parents
  • You really need to leave all that as a callable assembly routine or make an earnest effort to translate it to C. Translating it shouldn't be too hard. Below is an incomplete start. Just look at what a small chunk of the code is doing, translate it to C, and iterate that (removing goto's, etc.) until you've got legitimate C.

    unsigned long s, e, w, k;
    
    s  = 0x000F4240;
    k  = 0;
    e  = read_val1;
    e  = (e << 8) | read_val2;
    e  = (e << 8) | read_val3;
    e *= 0x10;
    
    do {
        w  = e - s;
        if (w < 0)
            goto endit;
        s >>= 1;
        :
        :
    } while ((s & 0x80000000) == 0);
    :
    :
    endit:
    
    
    
    
    

Reply
  • You really need to leave all that as a callable assembly routine or make an earnest effort to translate it to C. Translating it shouldn't be too hard. Below is an incomplete start. Just look at what a small chunk of the code is doing, translate it to C, and iterate that (removing goto's, etc.) until you've got legitimate C.

    unsigned long s, e, w, k;
    
    s  = 0x000F4240;
    k  = 0;
    e  = read_val1;
    e  = (e << 8) | read_val2;
    e  = (e << 8) | read_val3;
    e *= 0x10;
    
    do {
        w  = e - s;
        if (w < 0)
            goto endit;
        s >>= 1;
        :
        :
    } while ((s & 0x80000000) == 0);
    :
    :
    endit:
    
    
    
    
    

Children
  • thanks a lot dan henry for your code but i don't know exact algorithm for division of 32bit/32bit no,can i put k=e/s for that or should i do it in another way if yes tell me the procedure

  • "can i put k=e/s for that ..."

    Yes. The result of the '/' operator is the quotient from the division of the first operand by the second; the result of the '%' operator is the remainder.

  • mr.dan henry i am interested in knowing what the following code do

    do {

    w = e - s;

    if (w < 0)

    goto endit;

    s >>= 1; : :
    } while ((s & 0x80000000) == 0);
    : :
    endit:
    i didn't understand the procedure clearly how to divide 32bit/32bit pls send me algorithm

  • "i am interested in knowing what the following code do"

    It was only intended to illustrate my interpretation of what part of your assembly code was doing and how one might go about translating assembly to C. The ':' characters indicate that more follows but is left as an exercise for the reader (you) to complete.

    "how to divide 32bit/32bit "

    unsigned long dividend, divisor, quotient, remainder;
    
    quotient  = dividend / divisor;
    remainder = dividend % divisor;
    

    This is basic C stuff. You should try to get your hands on a C tutorial and/or reference book.