CRC calculation in Assembly Language

K

Thread Starter

kranthi

I am finding problem calculating CRC in Assembly language using modbus. Can anyone help me out with this.
Regards
kranthi
 
There are a couple of different 16 bit CRC algorithms. You have to use the right one to be compatible with Modbus.

The fastest way to calculate is to use a lookup table driven approach. Some old BASIC language software used this method.

This is from a long time ago and others can point you to sample code, etc.
 
J

J.H.Hoffeldt

<P>This is a subroutine in borland pascal i have
written to do my crc calc. Just use the assembly code</P>

<PRE>
function DoCRC16( RtuWord : Word; CheckSumIn : Word ) : Word;
const
XORWord = $A001;

var
CheckSumOut : Word;

begin { DoCRC16 }

asm
mov ax, CheckSumIn
mov bx, RTUWord
and bx, 00ffh
xor al, bl
mov cx, 8
@DoAgain:
rcr ax, 1
jnc @NoCarry
xor ax, XORWord
@NoCarry:
loop @DoAgain
mov CheckSumOut, ax
end;
DoCRC16 := CheckSumOut;

end; { DoCRC16 }
</PRE>
 
D
I have written a CRC-16 for MODBUS in 8086 assembler if thats of any use I'll post a copy, its quite short.
 
S

Sanjay Sarwate

Following is the code of procedure calculating CRC. I used this procedure in my C/C++ code. The PROC calculates CRC for the bytes pointed by BP+4 Buffer for the # of bytes given in BP+6 and returns the CRC in AX register.
Hope you are aware with the calling conventions of C and also the NEAR & FAR procedures & passing parameters on stack.

In case of any further clarification contact me on [email protected].

SmS

==================================================

_CalcCRC PROC NEAR
;
; BP+4 = Offset of the Buffer
; BP+6 = Message Length
;
PUSH BP
MOV BP,SP
PUSH SI
;
MOV SI,[BP+4]
CLD
MOV CX,[BP+6]
MOV BX,-1
@20001:
LODSB
PUSH CX
MOV CX,8
XOR BL,AL
@20002:
SHR BX,1
JNC @20003
XOR BX,0A001h
@20003:
LOOP @20002
@20004:
POP CX
LOOP @20001
;
MOV AX,BX ; Return CRC in AX
POP SI
MOV SP,BP
POP BP
RET
;
_CalcCRC ENDP

> I am finding problem calculating CRC in Assembly language using modbus. Can anyone help me out with this.
> Regards
> kranthi
 
A

Alex Pavloff

Assembly for what platform?

x86?
68000?
PIC?
AVR?
ARM?
Z80?
Alpha?

A much better place to ask for for general programming questions are newsgroups. Find a newsgroup that covers your platform, post your code, post your problem, and people will help you out.

Alex Pavloff - [email protected]
Eason Technology -- www.eason.com
 
K

Kerry Schrank

I wrote an assembly language program in the Motorola 68000 family processors and can fax this to you...it is a CRC-16 and uses the looping method with the constant $FB08 in the compare. It was not for a Modbus interface, however.

Regards,

Kerry L. Schrank
[email protected]
 
Just use the sample c source and compile it with your own complier for your machine (68x,80x,51x,..) and output a asm file. that's it
 
> I have written a CRC-16 for MODBUS in 8086 assembler if thats of any use I'll post a copy, its quite short. <

I would like to see a copy of your assembler routines for the 8086.

Cheers.
 
Top