home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.asm.x86      Ahh, the lost art of x86 assembly      4,675 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 3,659 of 4,675   
   Kerr-Mudd,John to All   
   DJB2   
   10 Nov 18 10:24:58   
   
   From: notsaying@nospicedham.invalid.org   
      
   hash(unsigned char *str)   
   {   
       unsigned long hash = 5381;   
       int c;   
      
       while (c = *str++)   
           hash = ((hash << 5) + hash) + c; /* hash * 33 + c */   
      
       return hash;   
      
      
   which I've translated as (16bit)   
      
      
   ReadBlock:   
         ;...   
         ; if no input exitprog   
         ; mov si,IBuffer   
         ; mov cx,IBuffLth   
      
   CalcHash: ; si->word cx=lth| ax=hash , uses bl   
            mov bl,33   
   NextWord:   
            lodsb               ; get 1st chr   
            dec cx   
            jcxz NextBlock   
            cmp al,0x20   
            jbe NextWord        ; skip leading spaces   
            mov dx,5381         ; DJB2 hash; not hex num   
   CalcNext:   
            cbw                 ; ah=0 for next mul   
            xchg ax,dx   
            mul bl              ; t*33   
            add dx,ax           ; t*33+c   
            lodsb               ; gnc   
            cmp al,0x20         ; End Word   
            jbe EndCalc   
            loop CalcNext   
   NextBlock:   
            jmp ReadBlock   
   EndCalc:   
            xchg ax,dx          ; result into ax   
      
   ; [prt ax as hex to Obuffer]   
      
            jmp NextWord   
      
      
   What's bugging me is that the initial seed of 0x1505 loses it's top bits   
   on the first character to be encoded!   
   0x1505*0x21->00A5+c   
   Have I read this correctly?   
   or is it (maybe I'm answering my own question) that the algorithm is   
   based on 32bit values.   
   --   
   Bah, and indeed, Humbug.   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca