Advertisement
Breeze

LFSR 128-bit algorithm (MMX required)

Mar 15th, 2012
1,218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; =========================================================================
  2. ; LFSR 128-bit algorithm (MMX required)
  3. ; by Andrey Breeze
  4. ; Implemented in x86 assembly using MASM32
  5. ; Bus taps: 128,126,101,99
  6. ; =========================================================================
  7. ; A linear feedback shift register (LFSR) is a shift register
  8. ; whose input bit is a linear function of its previous state.
  9. ; =========================================================================
  10.   .586
  11.   .MMX
  12.   .MODEL flat,stdcall
  13.    option casemap :none
  14. ; =========================================================================
  15.  
  16. ; includes
  17. ; -------------------------------------------------------------------------
  18.   include \masm32\macros\macros.asm
  19.   include \masm32\include\masm32.inc
  20.   include \masm32\include\kernel32.inc
  21.  
  22. ; libraries
  23. ; -------------------------------------------------------------------------
  24.   includelib \masm32\lib\masm32.lib
  25.   includelib \masm32\lib\kernel32.lib
  26.   includelib \masm32\lib\msvcrt.lib
  27.  
  28. ; prototypes
  29. ; -------------------------------------------------------------------------
  30.   LFSR PROTO STDCALL :DWORD,:DWORD,:QWORD,:QWORD,:QWORD,:QWORD
  31.  
  32. ; =========================================================================
  33.  
  34.   .data
  35.     SH_ dq 0FFFFFFFFFFFFFFFFh ; Sequence[127-64]
  36.     SL_ dq 0FFFFFFFFFFFFFFFFh ;         [63-0]
  37.     MH_ dq 00000000000000000h ; Mask[127-64] taps: 128,126,101,99
  38.     ML_ dq 00000000028000005h ;     [63-0]
  39.     RR_ dd 00000FFFFh         ; Amount of result DWORDS
  40.  
  41.   .code
  42.   start:
  43.  
  44. ; =========================================================================
  45.  
  46.   call main
  47.   inkey
  48.   exit
  49.  
  50. ; =========================================================================
  51.  
  52.   main proc
  53.     cls
  54.     mov ecx, RR_
  55.   l1:
  56.     push ecx
  57.     lea eax,SH_
  58.     lea ebx,SL_
  59.     invoke LFSR,eax,ebx,SH_,SL_,MH_,ML_
  60.     call ShowBinary
  61.     pop ecx
  62.     loop l1
  63.     ret
  64.   main endp
  65.  
  66. ; -------------------------------------------------------------------------
  67. ; inс: eax
  68.   ShowBinary proc
  69.     mov ecx,020h
  70.     mov ebx,eax
  71.     mov esi,04h
  72.     xor edx,edx
  73.     push edx
  74.   l1:
  75.     xor eax,eax
  76.     inc eax
  77.     and eax,ebx
  78.     ror ebx,01h
  79.     add eax,030h
  80.     or dl,al
  81.     dec esi
  82.     test esi,esi
  83.     jz l3
  84.     sal edx,08h
  85.     loop l1
  86.   l3:
  87.     push edx
  88.     xor edx,edx
  89.     mov esi,04h
  90.     loop l1
  91.   l4:
  92.     mov edx,esp
  93.     print edx
  94.     mov ecx,09h
  95.   l2:
  96.     pop eax
  97.     loop l2
  98.     ret
  99.   ShowBinary endp  
  100.  
  101. ; -------------------------------------------------------------------------
  102. ; out: eax
  103.   LFSR proc STDCALL ptrSH:DWORD,ptrSL:DWORD,SH:QWORD,SL:QWORD,\
  104.                     MH:QWORD,ML:QWORD
  105.     inRSH   = 0
  106.     inRSL   = 1
  107.     inRMH   = 2
  108.     inRML   = 3
  109.     inRSHcp = 4
  110.     inRSLcp = 5
  111.     inRTmpH = 6
  112.     inRTmpL = 7
  113.     inRTmp  = 6
  114.  
  115.     ; Initialize MMX registers
  116.     movq mm(inRSH),SH
  117.     movq mm(inRSL),SL
  118.     movq mm(inRMH),MH
  119.     movq mm(inRML),ML
  120.     movq mm(inRSHcp),mm(inRSH)
  121.     movq mm(inRSLcp),mm(inRSL)
  122.  
  123.     ; edx - DWORD of result code
  124.     xor edx,edx
  125.     mov si,0
  126.  
  127.   l1:
  128.     ; Apply LFSR mask
  129.     movq mm(inRTmpH),mm(inRSHcp)
  130.     pand mm(inRTmpH),mm(inRMH)
  131.     movq mm(inRTmpL),mm(inRSLcp)
  132.     pand mm(inRTmpL),mm(inRML)
  133.  
  134.     ; Reset parity
  135.     xor ecx,ecx
  136.  
  137.     ; Parity check for high bits [127-64]
  138.     ; skiped, because 128-bit LFSR
  139.     ; has bus taps: 128,126,101,99
  140.     ;movd ebx,mm(inRTmpH)
  141.     ;call P1
  142.     ;psrlq mm(inRTmpH),020h
  143.     ;movd ebx,mm(inRTmpH)
  144.     ;call P1
  145.  
  146.     ; Parity check for low bits [63-0]
  147.     movd ebx,mm(inRTmpL)
  148.     call P1
  149.     ; psrlq mm(inRTmpL),020h
  150.     ; movd ebx,mm(inRTmpL)
  151.     ; call P1
  152.  
  153.     sar cl,02h
  154.  
  155.     ; Right-shift low bits by 1 bit
  156.     psrlq mm(inRSLcp),01h
  157.     movq mm(inRTmp),mm(inRSHcp)
  158.     psllq mm(inRTmp),03Fh
  159.     por mm(inRSLcp),mm(inRTmp)
  160.  
  161.     ; Right-shift high bits by 1 bit
  162.     psrlq mm(inRSHcp),01h
  163.     movd mm(inRTmp), ecx
  164.     psllq mm(inRTmp),03Fh
  165.     por mm(inRSHcp),mm(inRTmp)
  166.  
  167.     ; Append LFSR result bit to EDX as least significant bit
  168.     movd ecx,mm(inRSLcp)
  169.     and ecx,01h
  170.     or dl,cl
  171.     inc si
  172.     cmp si,020h
  173.     je l2
  174.     sal edx,01h
  175.     jmp l1
  176.  
  177.   l2:
  178.     ; Store new sequence bits in memory
  179.     mov edi,ptrSH
  180.     movq [edi],mm(inRSHcp)
  181.     mov edi,ptrSL
  182.     movq [edi],mm(inRSLcp)
  183.  
  184.     ; Move 32 bits of LFSR result to EAX and return
  185.     mov eax,edx
  186.  
  187.     ret
  188.  
  189.   LFSR endp
  190.  
  191. ; -------------------------------------------------------------------------
  192. ; inс: ebx, cl
  193. ; out: cl
  194. ; Parity check for EBX
  195.  
  196.   P1 proc
  197.     or ebx,0h
  198.     lahf
  199.     xor cl,ah
  200.     sar ebx,08h
  201.     lahf
  202.     xor cl,ah
  203.     sar ebx,08h
  204.     lahf
  205.     xor cl,ah
  206.     sar ebx,08h
  207.     lahf
  208.     xor cl,ah
  209.     ret
  210.   P1 endp
  211.  
  212. ; =========================================================================
  213.  
  214. end start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement