AsmBB

Power
Login Register

Entropy
0

#16412 (ツ) six_L
Last edited: 21.01.2025 by six_L, read: 66 times

H(X) = - Σ (P(xi) * log2(P(xi))

/!attached/105

CalcEntropy proc uses rsi rdi plData:QWORD,dqDataLen:QWORD,lpOutEntropy:QWORD
	LOCAL count:QWORD
	LOCAL FreqCntTable[256]:DWORD

        finit

	lea     rdi,FreqCntTable
        mov     rcx,256
        xor     rax,rax
        rep     stosd		;zero to FreqCntTable buf

        lea     rdi,FreqCntTable
	mov     rsi,plData
        mov     rcx,dqDataLen
@1:
        xor     rax,rax
        lodsb
        shl     rax,2		;4 bytes, dword
        add     dword ptr [rdi+rax],1
        loop    @1

        fldz			;sum(0)=0.0

        mov     rcx,256
@2:
        dec     rcx

        mov     rax,rcx
        shl     rax,2
        mov     eax,dword ptr [rdi+rax]

	or      eax,eax
        jz      @f		;eax = 0 ?

        mov     count,rax

        fild    dqDataLen	; 
        fild    count		; 
        fdiv    st(0),st(1)	; P(i) = SUM(i)/total	; st0=P(i),st1=count,st2=dqDataLen,st3=sum(i)
        fst     st(1)		; st0 -> st1	;save st1=P(i)
        fchs			; change sign, st0
        fxch    st(1)		; st0<->st1 exchange
        fyl2x			; H(i) = -P(i)*log2(P(i)),before: st1 = y(P(i)),st0 = x[log2(P(i))];after:  st0 = -P(i)*log2(P(i))

	fadd    st(1),st(0)	; H = H+H(i),before: st0 = sum(i-1),st1 = -P(i)*log2(P(i)),after: st1 = sum(i-1) + -P(i)*log2(P(i))
        ffree   st(0)		; Free st0
        fincstp			; st1 -> st0
@@:
        or      rcx,rcx		;last ?.
        jnz     @2

        mov     rax,lpOutEntropy ;address of Entropy
        fstp    TBYTE PTR[rax]	;real10
        ;fstp    QWORD PTR[rax]	;real8
        ;fstp    DWORD PTR[rax]	;real4

        ffree   st(0)
        fincstp         

        ret
CalcEntropy endp

the above code is an entropy who used "BYTE" as unit to calculate. there has some another entropy.

"BIT" - 0,63 "BYTE" - 0,255 "WORD" - 0,65534 "DWORD" - 0,4294967294*

Attached files:
FileSizeUploadedDownloadsMD5 hash
1.png194003 bytes21.01.20254836335dfbe82f9e3f48947c932d2a9351

Entropy
0

AsmBB v3.0 (check-in: a316dab8b98d07d9); SQLite v3.42.0 (check-in: 831d0fb2836b71c9);
©2016..2023 John Found; Licensed under EUPL. Powered by Assembly language Created with Fresh IDE