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
  • unsigned char read_val1; unsigned char read_val2; unsigned char read_val3; void send_uart(void);

    void send_uart()
    { #pragma asm

    S1 EQU 08H
    S2 EQU 09H
    S3 EQU 0AH
    S4 EQU 0BH

    E1 EQU 0CH
    E2 EQU 0DH
    E3 EQU 0EH
    E4 EQU 0FH

    W1 EQU 10H
    W2 EQU 11H
    W3 EQU 12H
    W4 EQU 13H

    K1 EQU 14H
    K2 EQU 15H
    K3 EQU 16H
    K4 EQU 17H

    CNT EQU 18H

    MOV S1,#00H
    MOV S2,#0fH
    MOV S3,#42H
    MOV S4,#40H

    MOV K1,#00H
    MOV K2,#00H
    MOV K3,#00H
    MOV K4,#00H
    mov r0, read_val1 ;h.b of n1
    mov r1,read_val2 ;m.b of n1
    mov r2,read_val3 ;l.b of n1
    mov r7,#00h
    mov r6,#00h
    mov r5,#00h
    mov r4,#00h
    mov r3,#10h ;muliplier

    mov a,r3
    mov b,r2
    mul ab ;product of r2r3
    mov r4,a ;r3 in r4
    mov r5,b ;r2 in r5
    mov a,r3
    mov b,r1
    mul ab
    addc a,r5
    mov r5,a
    mov r6,b
    jnc x1
    mov a,r6
    add a,#01
    mov r6,a
    x1:
    mov a,r0
    mov b,r3
    mul ab
    add a,r6
    mov r6,a
    mov r7,b
    jnc x2
    mov a,r7
    add a,#01
    mov r7,a
    x2:

    MOV SP,#30H

    MOV E1,r7
    MOV E2,r6
    MOV E3,r5
    MOV E4,r4

    MOV CNT,#00H CALL SUB3224 JC ENDIT

    DOAGAIN: INC CNT CALL SHIFTER24BITL CALL SUB3224

    JNC KHALI1 CALL SHIFTER24BITR JMP LOOPBACK DEC CNT
    KHALI1:

    MOV A,S1 JNB Acc.7,DOAGAIN

    LOOPBACK: CALL SUB3224 JC BELOW MOV E1,W1 MOV E2,W2 MOV E3,W3 MOV E4,W4
    ;PUT ONE SETB C MOV A,K1 RLC A MOV K1,A

    MOV A,K2 RLC A MOV K2,A

    MOV A,K3 RLC A MOV K3,A

    MOV A,K4 RLC A MOV K4,A

    CLR C
    ;-------------
    JMP OOOOO BELOW:
    ;PUT ZERO CLR C

    MOV A,K1 ADD A,K1 MOV K1,A

    MOV A,K2 RLC A MOV K2,A

    MOV A,K3 RLC A MOV K3,A

    MOV A,K4 RLC A MOV K4,A

    OOOOO: CLR C
    ;------------- CALL SHIFTER24BITR DJNZ CNT,LOOPBACK
    RETT: JMP RETT

    ENDIT: MOV K1,#00H MOV K2,#00H MOV K3,#00H MOV K4,#01H
    RET
    ;----------------- 24-bit Shifter -----------------------------
    SHIFTER24BITR:
    ;S1:S2:S3:S4 CLR C MOV A,S1 RRC A MOV S1,A

    MOV A,S2 RRC A MOV S2,A

    MOV A,S3 RRC A MOV S3,A

    MOV A,S4 RRC A MOV S4,A

    CLR C
    RET

    SHIFTER24BITL: MOV A,S4 ADD A,S4 ;RLC A MOV S4,A

    MOV A,S3 RLC A MOV S3,A

    MOV A,S2 RLC A MOV S2,A

    MOV A,S1 RLC A MOV S1,A

    CLR C

    RET
    ;----------------- 32-BIT-24-BIT SUBSTARCTOR ---------------------
    SUB3224:
    ;E1:E2:E3:E4 - S1:S2:S3:S4 = W1:W2:W3:W4 CLR C MOV A,E4 SUBB A,S4 MOV W4,A

    MOV A,E3 SUBB A,S3 MOV W3,A

    MOV A,E2 SUBB A,S2 MOV W2,A

    MOV A,E1 SUBB A,S1 MOV W1,A
    RET #pragma endasm return; }
    IS my procedure of calling inline function is correct,ifn't how to do it .also suggest me to receive values store in k1,k2,k3,k4 in c function

Reply
  • unsigned char read_val1; unsigned char read_val2; unsigned char read_val3; void send_uart(void);

    void send_uart()
    { #pragma asm

    S1 EQU 08H
    S2 EQU 09H
    S3 EQU 0AH
    S4 EQU 0BH

    E1 EQU 0CH
    E2 EQU 0DH
    E3 EQU 0EH
    E4 EQU 0FH

    W1 EQU 10H
    W2 EQU 11H
    W3 EQU 12H
    W4 EQU 13H

    K1 EQU 14H
    K2 EQU 15H
    K3 EQU 16H
    K4 EQU 17H

    CNT EQU 18H

    MOV S1,#00H
    MOV S2,#0fH
    MOV S3,#42H
    MOV S4,#40H

    MOV K1,#00H
    MOV K2,#00H
    MOV K3,#00H
    MOV K4,#00H
    mov r0, read_val1 ;h.b of n1
    mov r1,read_val2 ;m.b of n1
    mov r2,read_val3 ;l.b of n1
    mov r7,#00h
    mov r6,#00h
    mov r5,#00h
    mov r4,#00h
    mov r3,#10h ;muliplier

    mov a,r3
    mov b,r2
    mul ab ;product of r2r3
    mov r4,a ;r3 in r4
    mov r5,b ;r2 in r5
    mov a,r3
    mov b,r1
    mul ab
    addc a,r5
    mov r5,a
    mov r6,b
    jnc x1
    mov a,r6
    add a,#01
    mov r6,a
    x1:
    mov a,r0
    mov b,r3
    mul ab
    add a,r6
    mov r6,a
    mov r7,b
    jnc x2
    mov a,r7
    add a,#01
    mov r7,a
    x2:

    MOV SP,#30H

    MOV E1,r7
    MOV E2,r6
    MOV E3,r5
    MOV E4,r4

    MOV CNT,#00H CALL SUB3224 JC ENDIT

    DOAGAIN: INC CNT CALL SHIFTER24BITL CALL SUB3224

    JNC KHALI1 CALL SHIFTER24BITR JMP LOOPBACK DEC CNT
    KHALI1:

    MOV A,S1 JNB Acc.7,DOAGAIN

    LOOPBACK: CALL SUB3224 JC BELOW MOV E1,W1 MOV E2,W2 MOV E3,W3 MOV E4,W4
    ;PUT ONE SETB C MOV A,K1 RLC A MOV K1,A

    MOV A,K2 RLC A MOV K2,A

    MOV A,K3 RLC A MOV K3,A

    MOV A,K4 RLC A MOV K4,A

    CLR C
    ;-------------
    JMP OOOOO BELOW:
    ;PUT ZERO CLR C

    MOV A,K1 ADD A,K1 MOV K1,A

    MOV A,K2 RLC A MOV K2,A

    MOV A,K3 RLC A MOV K3,A

    MOV A,K4 RLC A MOV K4,A

    OOOOO: CLR C
    ;------------- CALL SHIFTER24BITR DJNZ CNT,LOOPBACK
    RETT: JMP RETT

    ENDIT: MOV K1,#00H MOV K2,#00H MOV K3,#00H MOV K4,#01H
    RET
    ;----------------- 24-bit Shifter -----------------------------
    SHIFTER24BITR:
    ;S1:S2:S3:S4 CLR C MOV A,S1 RRC A MOV S1,A

    MOV A,S2 RRC A MOV S2,A

    MOV A,S3 RRC A MOV S3,A

    MOV A,S4 RRC A MOV S4,A

    CLR C
    RET

    SHIFTER24BITL: MOV A,S4 ADD A,S4 ;RLC A MOV S4,A

    MOV A,S3 RLC A MOV S3,A

    MOV A,S2 RLC A MOV S2,A

    MOV A,S1 RLC A MOV S1,A

    CLR C

    RET
    ;----------------- 32-BIT-24-BIT SUBSTARCTOR ---------------------
    SUB3224:
    ;E1:E2:E3:E4 - S1:S2:S3:S4 = W1:W2:W3:W4 CLR C MOV A,E4 SUBB A,S4 MOV W4,A

    MOV A,E3 SUBB A,S3 MOV W3,A

    MOV A,E2 SUBB A,S2 MOV W2,A

    MOV A,E1 SUBB A,S1 MOV W1,A
    RET #pragma endasm return; }
    IS my procedure of calling inline function is correct,ifn't how to do it .also suggest me to receive values store in k1,k2,k3,k4 in c function

Children
  • "IS my procedure of calling inline function is correct"

    NO!

    You aren't calling anything!

    The entire body of the function is pure assembler - what on earth is the point in that?!

    It is an assembler funcion - so put it in an Assembler file and don't mess about with the inline nonsense!

    "also suggest me to receive values store in k1,k2,k3,k4 in c function"

    You didn't listen last time - why should I say it again?!

  • 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:
    
    
    
    
    

  • 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.