PDA

View Full Version : Hash Code Help



Bill Drummond
04-03-2009, 02:19 PM
I trying to port this pascal code to Spin


function ELFHash(const Str : String) : Int64;
var
i : integer;
x : Cardinal;
begin
Result := 0;
for i := 1 to Length(Str) do
begin
Result := (Result shl 4) + Ord(Str[i]);
x := Result and $F0000000;
if (x <> 0) then
begin
Result := Result xor (x shr 24);
end;
Result := Result and (not x);
end;
end;

This doesn't work..

''***************************************
''* From Keyboard Demo v1.0 *
''* Author: Chip Gracey *
''* Copyright (c) 2006 Parallax, Inc. *
''* See end of file for terms of use. *
''***************************************




CON


_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000


VAR
byte j
long x
OBJ


term : "tv_terminal"
kb : "keyboard"



PUB start | i


'start the tv terminal
term.start(12)
term.str(string("HashCode Demo...",13))


'start the keyboard
kb.start(26, 27)

term.Hex(Hash(String("DIR")),8)
term.out(13)
term.Hex(Hash(String("TYPE")),8)
term.out(13)
term.Hex(Hash(String("MOUNT")),8)
term.out(13)
term.Hex(Hash(String("UNMOUNT")),8)
term.out(13)

PUB Hash(string_ptr) : Result
result := 0
repeat strsize(string_ptr)
Result := (Result << 4) + byte[string_ptr++]
x := Result and $F0_00_00_00
if (x <> 0)
Result := Result ^ (x >> 24)
Result := result & (not x)

{{


Here is a windows program

kuroneko
04-03-2009, 02:33 PM
Change that (boolean AND)



x := Result and $F0_00_00_00


into that (bitwise AND)



x := Result & $F0_00_00_00

Bill Drummond
04-03-2009, 03:42 PM
Thanks, I need to study the manual some more..
any idea why it breaks with more than 6 characters?

Bill Drummond
04-03-2009, 04:51 PM
A Better Demo

''***************************************
''* From Keyboard Demo v1.0 *
''* Author: Chip Gracey *
''* Copyright (c) 2006 Parallax, Inc. *
''* See end of file for terms of use. *
''***************************************


CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
VAR
long x
byte HashStr[8]
OBJ
term : "tv_terminal"
kb : "keyboard"

PUB start | i
'start the tv terminal
term.start(12)
term.str(string("HashCode Demo...",13))
'start the keyboard
kb.start(26, 27)
repeat
term.str(string("Enter a word >"))
getstr
term.str(string("Hash Code for "))
term.str(@HashStr)
term.str(string(" is $"))
term.Hex(Hash(@HashStr),8)
term.out(13)

PUB GetStr | i,ch
i := 0
repeat until ch == 13
ch := kb.getkey
HashStr[i++] := ch
term.out(ch)
HashStr[i-1] := 0

PUB Hash(string_ptr) : Result
result := 0
repeat strsize(string_ptr)
Result := (Result << 4) + byte[string_ptr++]
x := Result & $F0_00_00_00
if (x <> 0)
Result := Result ^ (x >> 24)
Result := result & (not x)

{{
┌───────────────────────────────────────────────── ────────────────────────────────────────────────── ───────────────────────────┐
│ TERMS OF USE: MIT License │
├───────────────────────────────────────────────── ────────────────────────────────────────────────── ───────────────────────────┤
│Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation │
│files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, │
│modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software│
│is furnished to do so, subject to the following conditions: │
│ │
│The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.│
│ │
│THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE │
│WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR │
│COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │
│ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │
└───────────────────────────────────────────────── ────────────────────────────────────────────────── ───────────────────────────┘
}}

Post Edited (Bill Drummond) : 4/3/2009 10:08:26 AM GMT

kuroneko
04-03-2009, 05:01 PM
Bill Drummond said...
any idea why it breaks with more than 6 characters?

I'm just guessing here, maybe because your HashStr array only has space for 6 (now 8) bytes?

Post Edited (kuroneko) : 4/3/2009 10:17:26 AM GMT

Bill Drummond
04-03-2009, 05:19 PM
The first try didn't have the HashStr Array. I increased it to 8 in the next one, but still won't do more than 6 chars correctly.
I edited the Windows program so everybody uses hex.

kuroneko
04-03-2009, 05:33 PM
So what happens after 6 characters? Having space for 8 bytes still lets you only safely enter 7 characters (+terminator). As for the latter, the terminator seems to override the last entered character (that is if SPIN uses post-increment, i++ vs i-1).

And I'm also not too comfortable with repeat strsize(). Is this evaluated only at the beginning of the loop or everytime the loop is run? If you put some debug tracing in there, how far does it get?

Brian Fairchild
04-03-2009, 06:05 PM
My Pascal is very ropey but doesn't the 'int64' in the Pascal function definition imply that the result is a 64-bit value?

Bill Drummond
04-03-2009, 06:29 PM
So what happens after 6 characters? Having space for 8 bytes still lets you only safely enter 7 characters (+terminator). As for the latter, the terminator seems to override the last entered character (that is if SPIN uses post-increment, i++ vs i-1).
I changed from:


byte HashStr[8] to byte HashStr[12]
and

repeat strsize(string_ptr) to repeat while byte[string_ptr] <> 0
same result.

My Pascal is very ropey but doesn't the 'int64' in the Pascal function definition imply that the result is a 64-bit value?

That's true, but I changed it to Cardinal(unsigned 32 bit)·and it still works.

I need to Learn how ViewPort works... got too many projects going at same time.

mpark
04-03-2009, 10:08 PM
What do you mean by "it breaks"? What results are you seeing and what are you expecting to see?

Brian Fairchild
04-03-2009, 10:47 PM
I've bashed the code into a proto board and when entering 7 characters the hash value is returned as '00000000'. An eight character string returns the hash value of just the 8th character.

Brian Fairchild
04-03-2009, 11:34 PM
OK, simple really when you spot it.

Change...



Result := result & (not x)



...which is a boolean 'NOT' to...



Result := result & (!x)



...which is a bitwise 'NOT'.

Bill Drummond
04-03-2009, 11:50 PM
What do you mean by "it breaks"? What results are you seeing and what are you expecting to see?

If I key in : 123456···· ·I see $03456786· - is good
If I key in : 1234567·· · I see $00000000 should be $045678A7
If I key in : 123456789 I see $000003B9 should be $0678AEE9
If I key in :·MOUNT····· I see $00524A34··- is good
If I key in : UNMOUNT· I see $00000000 should be $0A324A64

Download the File in my first post to run the Hash Code generator on a PC.

Bill Drummond
04-04-2009, 12:12 AM
OK, simple really when you spot it.
Yea, I'm ! used to programming with funny paper cuss words yet.http://forums.parallax.com/images/smilies/smhair.gif , but this is how you learn.
··