2

Before that I asked about piece of code that used in this algorithm (here). Now I have a bit problem with the rest of algorithm. So I have to describe this with detail. Sorry if i vast your time for read it.
I have shown an algorithm below. It seems is a CRC16 algorithm.

CRC_16_Calculate:                       
                                        
                mov     [-r0], r9
                mov     [-r0], r8
                mov     [-r0], r7
                mov     [-r0], r6
                mov     r9, r12
                mov     r8, r15
                jmpr    cc_UC, loc_29D6
; ---------------------------------------------------------------------------

loc_29A4:                               
                mov     r4, r13
                mov     r5, r14
                add     r13, #1
                addc    r14, #0
                exts    r5, #1
                movb    rl3, [r4]
                movbz   r7, rl3
                movb    rl6, #0

loc_29B4:                               
                mov     r4, r9
                xor     r4, r7
                and     r4, #1
                jmpr    cc_Z, loc_29C8
                mov     r4, r9
                shr     r4, #1
                xor     r4, #0A001h
                mov     r9, r4
                jmpr    cc_UC, loc_29CA
; ---------------------------------------------------------------------------

loc_29C8:                               
                shr     r9, #1

loc_29CA:                               
                shr     r7, #1
                addb    rl6, #1
                cmpb    rl6, #8
                jmpr    cc_C, loc_29B4
                sub     r8, #1

loc_29D6:                               
                cmp     r8, #0
                jmpr    cc_UGT, loc_29A4
                mov     r4, r9
                mov     r6, [r0+]
                mov     r7, [r0+]
                mov     r8, [r0+]
                mov     r9, [r0+]
                rets

As you see in below I converted that to C++ code:

static uint16_t intCrc16(uint8_t* cBuffer)
{
    uint16_t wPreset = 0xFFFF;
    uint16_t iBufLen = 6;
    uint16_t temp = 0;
    while (iBufLen--)
    {
        temp = *cBuffer++;
        for (uint8_t j = 0; j < 8; ++j)
        {
            if ((wPreset ^ temp) & 0x01) wPreset = (wPreset >> 1) ^ 0xA001;
            else wPreset >>= 1;
            temp >>= 1;
        }
    }
    return wPreset;
}

It seems everything is good but the problem appear right here. The main loop of this function run for 6 times(iBufLen) but the input argument(cBuffer) length is always 4 Bytes. To prove this point we check where this algorithm invoked. Look at below:

mov     r13, r4
mov     r14, r5
mov     r15, #6
calls   0, CRC_16_Calculate

As you see, r13 and r14 are input argument as cBuffer and r15 equivalent to iBufLen. So, what's my mistake? By the way, r4 and r5 are the returned value of function that I pointed (here).

7
  • Please paste the code as text, not as screenshot
    – Igor Skochinsky
    Commented Sep 23, 2020 at 11:51
  • Thanks dear Igor. But how I can paste Ida diagrams as Text? Each code pasted as Text, only diagram is as screenshot.
    – Unicornux
    Commented Sep 24, 2020 at 4:47
  • you can paste the text listing, it doesn't have to be a graph.
    – Igor Skochinsky
    Commented Sep 24, 2020 at 12:09
  • OK. Sure. Thanks.
    – Unicornux
    Commented Sep 26, 2020 at 5:29
  • Why do you think there’s a mistake in your conversion?
    – Igor Skochinsky
    Commented Sep 30, 2020 at 18:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.