1 year ago

#363893

test-img

AndyB

Inline assembly language - Accessing an array's elements in C++

I'm upgrading a Borland C++ Builder 6 project to the latest Embarcadero C++ Builder (11.1)

It's a 32 bit windows app.

Some legacy code, not written by me, contains an array unsigned 32 bit integers, declared outside of a class like this:

UINT32 CRC32_TABLE[] =
    {
    0x000000000,0x077073096,0x0EE0E612C,0x0990951BA,
    0x0076DC419,0x0706AF48F,0x0E963A535,0x09E6495A3]; //the actual array is a lot bigger

Later on there is this function:

static UINT32 GenerateCRC(const void* p_data,
                      int         nbytes)
{
UINT32 rvalue;
__asm
    {
    mov esi,p_data
    mov ecx,nbytes
    mov ebx,0FFFFFFFFH          // Initialize CRC accumulator to -1

    /*--------------------------------------*/
    /* Accumulate the CRC in the specified  */
    /* range of memory.                     */
    /*--------------------------------------*/

loop2:                           // FOR i = 1 TO nbytes DO
    xor     eax,eax
    mov     al,[esi]             //   Get a byte to be checked
    inc     esi                  //   Bump index
    xor     al,bl                //   XOR NEW BYTE WITH LOW CRC
    shl     eax,2                //   MAKE IT A DWORD INDEX
    mov     edi,eax              //   
    shr     ebx,8                //   SHIFT OLD CRC RIGHT 8
    xor     ebx,CRC32_TABLE[edi] //   XOR SHIFTED CRC WITH CONSTANT
    loop    loop2                // ENDFOR
    not     ebx
    mov     rvalue,ebx
    }
return rvalue;
}

The current compiler wont accept xor ebx,CRC32_TABLE[edi], saying "cannot use a base register with variable reference".

It is decades since I've done any assembly language work, so any pointers to fixing this would be very much appreciated.

I'm very open to replacing the asm code with C++, btw. I have no idea why this code was written in assembly language. It's not used in a time critical section but to verify logins... I'd like to think there was a better reason than 'because I could' but, judging from the C++ code, this was written by someone (sadly, no longer with us) who liked to complicate things for no reason...

Andy

c++

windows

x86

inline-assembly

0 Answers

Your Answer

Accepted video resources