4
\$\begingroup\$

I need to convert the following PIC assembly code (below) to an 8051 assembly. It is an inline assembly code. Actually, I intend to convert it to C. I am not familiar with the PIC MCU so I do not understand much of what happens. If I can an understanding of each instruction, I will try and input the equivalent assembly. Or better still, I will appreciate if I can get an equivalent 8051 assembly code of it. "toRotate" is a 32 bit variable. Thanks

    _asm
    movlb toRotate
    bcf STATUS,0,0
    btfsc toRotate+3,7,1
    bsf STATUS,0,0
    rlcf toRotate+0,1,1
    rlcf toRotate+1,1,1
    rlcf toRotate+2,1,1
    rlcf toRotate+3,1,1
    _endasm

This is the entire code below.....I really want to know what is done here. My intention is to convert it to C language.

DWORD leftRotateDWORD(DWORD val, BYTE bits)
{
    BYTE i, t;
    DWORD_VAL toRotate;
    toRotate.Val = val;

    for(i = bits; i >= 8u; i -= 8)
    {
        t = toRotate.v[3];
        toRotate.v[3] = toRotate.v[2];
        toRotate.v[2] = toRotate.v[1];
        toRotate.v[1] = toRotate.v[0];
        toRotate.v[0] = t;
    }

    for(; i != 0u; i--)
    {
        _asm
        movlb toRotate
        bcf STATUS,0,0
        btfsc toRotate+3,7,1
        bsf STATUS,0,0
        rlcf toRotate+0,1,1
        rlcf toRotate+1,1,1
        rlcf toRotate+2,1,1
        rlcf toRotate+3,1,1
        _endasm

    }
    return toRotate.Val;
}
\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

Your code snippet is for a PIC 18. It is a naively written routine to rotate the 32 bit variable toRotate left one bit. Here is the code with some annotation:

         movlb   toRotate    ;set bank for access to target variable
         bcf     STATUS, C   ;init bit to shift in to 0
         btfsc   toRotate+3, 7 ;bit to shift in really is 0 ?
         bsf     STATUS, C   ;no, set it to 1
         rlcf    toRotate+0, 1 ;do the left shift by one bit
         rlcf    toRotate+1, 1
         rlcf    toRotate+2, 1
         rlcf    toRotate+3, 1
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Thanks Olin. I am actually working on an HASH algorithm. I got this code from the MicroChip TCP/IP Stack. But I want to believe that i can used it for an 8 bit MCU, because I am much of an 8051 guy. Thanks \$\endgroup\$
    – Paul A.
    Commented Mar 9, 2012 at 15:23
  • \$\begingroup\$ Please I edited the question with the code. Thanks \$\endgroup\$
    – Paul A.
    Commented Mar 9, 2012 at 15:43
2
\$\begingroup\$

As Olin Lathrop said, it's a 32-bit left rotate.

Many compilers (a) recognize this idiom as a "32-bit rotate":

DWORD leftRotateDWORD(DWORD x, BYTE shift){
    y = (x << shift) | (x >> (32 - shift));
}

Those compilers will emit a single 32 bit rotate instruction on processors that have such an instruction, or a highly optimized sequence on other processors.

You might save a lot of time in testing and debugging by using one of the freely available implementations of SHA256 or SHA-3 proposed algorithms: ( a b c d e )

\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.