Im trying to convert a function written in C code to assembly, it computes the CRC of data;

#define CRC_MAGIC 0xd9216290L
unsigned long ComputeCRC(Ptr pt, long len)
{
unsigned long crc = CRC_MAGIC;
unsigned char *p = (unsigned char *) pt;
while (len--)
crc = ((crc << 1L) | ((crc & 0x80000000L)? 1 : 0) ^ *(p++);
return crc;
}

heres what i get from it
mov crc,D9216290h
"<< 1L" is SHL crc,1h
"|" is OR
"& 0x80..." is AND crc,80000000h
"^" is XOR

I hate C, this is also new to me "(...)? 1 : 0" what the heck?

can someone explain?
Posted on 2002-01-22 15:30:35 by Aaron
You might be able to get away with compliling it as library, then calling from ASM, You only have to call the function
So that would be your only conversion
Posted on 2002-01-22 15:45:10 by andy981
I got it to complile

but


I changed
unsigned long ComputeCRC(Ptr pt, long len)
to
unsigned long ComputeCRC(int *pt, long len)

I added a ) at the end of
was
crc = ((crc << 1L) | ((crc & 0x80000000L)? 1 : 0) ^ *(p++);
now
crc = ((crc << 1L) | ((crc & 0x80000000L)? 1 : 0) ^ *(p++));

If you want I can make the lib and email it to you
but
unsigned long ComputeCRC(int *pt, long len)
the int *pt may not be what you want

but Ptr is not a insintric data type in C, I think like int or float


#define CRC_MAGIC 0xd9216290L
unsigned long ComputeCRC(int *pt, long len)
{
unsigned long crc = CRC_MAGIC;
unsigned char *p = (unsigned char *) pt;
while (len--)
crc = ((crc << 1L) | ((crc & 0x80000000L)? 1 : 0) ^ *(p++));
return crc;
}

Andy981

:)
Posted on 2002-01-22 16:02:26 by andy981

blah = (boolean expression) ? x : y


is the same as



if(boolean expression) {
blah = x;
} else {
blah = y;
}


With the correct compiler (and compiler settings), the "?" way of
writing it can be optimized to a CMOV.

Btw, if you don't understand the C CRC code... why don't you just
find an ASM equivalent? Doing a google search (or even a search
on this board) should come up with working stuff.
Posted on 2002-01-22 16:08:49 by f0dder
I hate C, this is also new to me "(...)? 1 : 0" what the heck?

Think of it like an 'If' statement. If the expression before the '?' evalutes to true, then use the value '1', if it evalutes to false, use the value '0'.

This might help you understand it:
<expression to evaluate> ? <if true, return this value> : <if false, return this value>


VB also has a statement that does this, it is 'IIf'. The only difference is that with the C version, it allows shortcutting, while the VB one won't (which can be a real pain in the ass). Shortcutting means that only the true or false return values will get evaluated, not both, but in VB both get evaluated.
Posted on 2002-01-22 17:11:42 by sluggy


#define CRC_MAGIC 0xd9216290L
unsigned long ComputeCRC(Ptr pt, long len)
{
unsigned long crc = CRC_MAGIC;
unsigned char *p = (unsigned char *) pt;
while (len--)
crc = ((crc << 1L) | ((crc & 0x80000000L)? 1 : 0) ^ *(p++);
return crc;
}


can be equivalent to:



CRC_MAGIC = 0d9216290h

ComputeCRC proc uses esi edi, pt:dword, len:dword
mov eax, CRC_MAGIC
mov esi, pt
and esi, esi
jz @@quit
mov ecx, len
and ecx, ecx
jz @@quit
xor edx, edx
.repeat
mov dl, [esi]
rol eax, 1
xor eax, edx
dec ecx
.until ZERO?
@@quit:
ret
ComputeCRC


In this case:


rol eax, 1

is the equivalent to:


((crc << 1L) | ((crc & 0x80000000L)? 1 : 0)


This is one of the rare cases that the assembly equivalent is actually shorter than the C one. I dare the compiler to think about this optimization :grin:

^ means XOR
(x) ? a: b; means if x is true then do a, else do b.
Posted on 2002-01-22 18:15:27 by dxantos
I got it to work, thanks for you help, this is probaly a bad way
to compute the CRC but i needed the exact output.

;COMPUTE CRC
;data$ points to the start of the data
;op1 op2 op3 are for storing the values

xor eax,eax
mov ebx, LenOfData
mov crc, 0d9216290h

@CMPCRC:

mov ecx,crc
shl ecx,1
mov op1,ecx

mov ecx,crc
and ecx,80000000h

.IF ecx
mov op2,1
.ELSE
mov op2,0
.ENDIF

xor ecx,ecx
mov cl,
mov op3,ecx

mov ecx,op1
or ecx,op2
xor ecx,op3
mov crc,ecx
inc eax
dec ebx

cmp ebx,0
jg @CMPCRC
Posted on 2002-01-22 18:42:22 by Aaron