Page 1 of 1

How to Optimize another CRC routine.

Posted: Fri Jun 27, 2014 6:44 pm
by swhite

Code: Select all

Procedure.a GetLRC(tcTxt.s)
   ;
   ; Assumes the text does not include the STX and ETX Characters
   ;
   Define lnLRC.i,ln,*ln1,lnLen.i
   lnLen=Len(tcTxt)-1
   *ln1=@tcTxt
   lnLRC=PeekA(*ln1)
   For ln=1 To lnLen
      lnLRC = lnLRC ! PeekA(*ln1+ln)
   Next
   If lnLRC=0
      lnLRC=255
   EndIf
   ProcedureReturn lnLRC
EndProcedure
The above code is used to produce a checksum for data sent to and from particular equipment we use. I received a lot for very good and appreciated help regarding my need for a CRC16CCITT algorithm so I wondered if someone might know how to optimize this in assembler.

Thanks,
Simon

Re: How to Optimize another CRC routine.

Posted: Fri Jun 27, 2014 8:08 pm
by wilbert
Assuming you compile your application in ASCII mode, you can do it like this

Code: Select all

Procedure.a GetLRC(tcTxt.s)
  !xor eax, eax
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    !mov rdx, [p.v_tcTxt]
    !getlrc0:
    !mov cl, [rdx]
    !inc rdx
  CompilerElse
    !mov edx, [p.v_tcTxt]
    !getlrc0:
    !mov cl, [edx]
    !inc edx
  CompilerEndIf
  !xor al, cl
  !and cl, cl
  !jnz getlrc0
  !and al, al
  !jnz getlrc1
  !mov al, 0xff
  !getlrc1:
  ProcedureReturn
EndProcedure

Debug GetLRC("hello")
Passing a pointer instead of a string might be faster.

Code: Select all

Procedure.a GetLRC(*tcTxt)
  !xor eax, eax
  CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
    !mov rdx, [p.p_tcTxt]
    !getlrc0:
    !mov cl, [rdx]
    !inc rdx
  CompilerElse
    !mov edx, [p.p_tcTxt]
    !getlrc0:
    !mov cl, [edx]
    !inc edx
  CompilerEndIf
  !xor al, cl
  !and cl, cl
  !jnz getlrc0
  !and al, al
  !jnz getlrc1
  !mov al, 0xff
  !getlrc1:
  ProcedureReturn
EndProcedure

Debug GetLRC(@"hello")

Re: How to Optimize another CRC routine.

Posted: Sat Jun 28, 2014 6:48 pm
by swhite
I will give it a try thank-you.

Simon