flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Hash value for words.

Author
Thread Post new topic Reply to topic
Roman



Joined: 21 Apr 2012
Posts: 1840
Roman 29 Oct 2024, 07:46
I want write macro hashWord.
Code:
HashWord "hello" ;get dd 22, sizeWord
                 ;db "hello" 
HashWord "understood" ;get dd 0x34aa22,sizeWord
    

For easy find/search or compared words

Hash Something like this
Code:
const int k = 31, mod = 1e9+7;

string s = "abracadabraz";
long long h = 0, m = 1;
for (char c : s) {
    int x = (int) (c - 'a' + 1);
    h = (h + m * x) % mod;
    m = (m * k) % mod;
}
;or this variant
long long compute_hash(string const& s) {
    const int p = 31;
    const int m = 1e9 + 9;
    long long hash_value = 0;
    long long p_pow = 1;
    for (char c : s) {
        hash_value = (hash_value + (c - 'a' + 1) * p_pow) % m;
        p_pow = (p_pow * p) % m;
    }
    return hash_value;
}
    
Post 29 Oct 2024, 07:46
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1840
Roman 29 Oct 2024, 10:45
Post 29 Oct 2024, 10:45
View user's profile Send private message Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1840
Roman 31 Oct 2024, 12:19
https://board.flatassembler.net/topic.php?t=10010
Code:
macro embed_crc start,finish {
 local b,c
   c = 0xffffffff
      repeat finish-start
         load b byte from start+%-1
          c = c xor b
         repeat 8
                    c = (c shr 1) xor (0xedb88320 * (c and 1))
          end repeat
  end repeat
  dd c xor 0xffffffff
}    

start is inclusive and finish is exclusive.

Use like this
Code:

MyCode:
   mov     eax,0x12345678
      xor     eax,edx
     ret

     embed_crc $$,$    
Post 31 Oct 2024, 12:19
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.