Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Machine code functions: Bit Wizardry


  • Please log in to reply
144 replies to this topic
Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
Cool! With the pre-computed table this works fast!

An application is to find duplicate files, even if they have different names. Compute the CRC of the files and attach them to the file size, stored in AHK variables. Sort them and search for equal ones (which got next to each other). We only have to compare files byte-by-byte, which have equal {file_size, CRC} pairs, which happen very rarely for different files.

Thalon
  • Members
  • 641 posts
  • Last active: Jan 02 2017 12:17 PM
  • Joined: 12 Jul 2005

it would be nice if ahk could also handle C code being written directly to it, similar to calling assembler within C++

Something similiar to this?

I'm very interested in this topic as it shows some nice things.
I've never tried to convert C-Code in Hex-Equivalent, I always did it directly in ASM (and to be honestly: I'm at ASM-Beginner-Level).

Another interesting aspect would be to add features (or to solve bugs) to applications without source available (of course, the autor has to give the permission). I've a bit experience in this topic, but it could be much easier now (if using AHK for these things).
Does anyone know an application that shows C-Source out of ASM? I know I had a tool from "Keil" in school, but this one had a very limited command-list...

Thalon

tic
  • Members
  • 1934 posts
  • Last active: May 30 2018 08:13 PM
  • Joined: 22 Apr 2007
Thsnks Thalon. I'll have a look.

That examle crc32 doesnt work for me as there is a call to a non-existant function MCode(CRC32_Init,.....

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
tic: Copy over the MCode() function from the first post.

tic
  • Members
  • 1934 posts
  • Last active: May 30 2018 08:13 PM
  • Joined: 22 Apr 2007

tic: Copy over the MCode() function from the first post.


ha! cheers. hadnt looked at the 1st page. ill give the crc32 a good old test now as i can think of a very good use for it for the program MOVEit i wrote a while ago, as well as using ReadDirectoryChangesW to completely cut down on the rescanning it does. thanks all

tic
  • Members
  • 1934 posts
  • Last active: May 30 2018 08:13 PM
  • Joined: 22 Apr 2007
I don't really have time where I am to try figure this out, but why do both of these pieces of code give res as 4294967295?

#NoEnv

MCode(ByRef code, hex) { ; allocate memory and write Machine Code there
   VarSetCapacity(code,StrLen(hex)//2)
   Loop % StrLen(hex)//2
      NumPut("0x" . SubStr(hex,2*A_Index-1,2), code, A_Index-1, "Char")
}

MCode(CRC32_Init, "5589E583EC10C745FC2083B8EDC745F400000000817DF4FF0000007F508B45F48945F8C745F008000000837DF0007E238B45F883"
. "E00185C0740D8B45F8D1E83345FC8945F8EB058D45F8D1288D45F0FF08EBD78B45F48D0C85000000008B55088B45F88904118D45F4FF00EBA7C9C3")
MCode(CRC32_Get, "5589E583EC08C745FCFFFFFFFFC745F8000000008B45F83B450C732E8B45080345F80FB6003345FC25FF0000008D0C85000000008"
. "B55108B45FCC1E8083304118945FC8D45F8FF00EBCA8B45FCC9C3")

FileRead, a, %A_AhkPath%
FileGetSize, size, %A_AhkPath%
res := DllCall(&CRC32_Get, "uint",&a, "uint",size, "uint",&CRC32LookupTable)
Display()

Display()
{
global res
IfLess, res, 0x104C11DB7
  res := ~res
IfGreater, res, 0x104C11DB7
  res := res ^ 0xFFFFFFFF
MsgBox % res
return
}

and

#NoEnv

MCode(ByRef code, hex) { ; allocate memory and write Machine Code there
   VarSetCapacity(code,StrLen(hex)//2)
   Loop % StrLen(hex)//2
      NumPut("0x" . SubStr(hex,2*A_Index-1,2), code, A_Index-1, "Char")
}

MCode(CRC32_Init, "5589E583EC10C745FC2083B8EDC745F400000000817DF4FF0000007F508B45F48945F8C745F008000000837DF0007E238B45F883"
. "E00185C0740D8B45F8D1E83345FC8945F8EB058D45F8D1288D45F0FF08EBD78B45F48D0C85000000008B55088B45F88904118D45F4FF00EBA7C9C3")
MCode(CRC32_Get, "5589E583EC08C745FCFFFFFFFFC745F8000000008B45F83B450C732E8B45080345F80FB6003345FC25FF0000008D0C85000000008"
. "B55108B45FCC1E8083304118945FC8D45F8FF00EBCA8B45FCC9C3")

FileRead, a, %A_AhkPath%
FileGetSize, size, %A_AhkPath%
res := DllCall(&CRC32_Get, "uint",&a, "uint",size, "uint",&CRC32LookupTable)
Display()

Display()
{
global res
IfLess, res, 0x104C11DB7
  res := ~res
IfGreater, res, 0x104C11DB7
  res := res ^ 0xFFFFFFFF
MsgBox % res
return
}

[color=red]ExitApp ; A little bit of extra code[/color]


engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
I don't see why the result should be different?

tic
  • Members
  • 1934 posts
  • Last active: May 30 2018 08:13 PM
  • Joined: 22 Apr 2007
I've done something horribly wrong but cant really check what right now. the results should be different as the files are different, so should return a different crc32. hopefully someone will fix it for me :) or ill have a look later. thanks all

ahklerner
  • Members
  • 1386 posts
  • Last active: Oct 08 2014 10:29 AM
  • Joined: 26 Jun 2006

...4294967295...

That is the same errorlevel i get sometimes when a sendmessage has not succeeded.
Posted Image
ʞɔпɟ əɥʇ ʇɐɥʍ

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
it is -1, or 0xFFFFFFFF

olfen
  • Members
  • 115 posts
  • Last active: Dec 25 2012 09:48 AM
  • Joined: 04 Jun 2005
2tic:

You need to build the CRC32 lookup table once beforce executing CRC_Get:
VarSetCapacity(CRC32LookupTable, 256*4) 
DllCall(&CRC32_Init, "uint",&CRC32LookupTable)


Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
tic: "res := ~res" and "res := res ^ 0xFFFFFFFF" do the same thing, so in the Display function something is fishy.

olfen
  • Members
  • 115 posts
  • Last active: Dec 25 2012 09:48 AM
  • Joined: 04 Jun 2005

tic: "res := ~res" and "res := res ^ 0xFFFFFFFF" do the same thing, so in the Display function something is fishy.

Can you explain this:
#NoEnv 



MCode(CRC32_Init, "5589E583EC10C745FC2083B8EDC745F400000000817DF4FF0000007F508B45F48945F8C745F008000000837DF0007E238B45F883" 

. "E00185C0740D8B45F8D1E83345FC8945F8EB058D45F8D1288D45F0FF08EBD78B45F48D0C85000000008B55088B45F88904118D45F4FF00EBA7C9C3") 

MCode(CRC32_Get, "5589E583EC08C745FCFFFFFFFFC745F8000000008B45F83B450C732E8B45080345F80FB6003345FC25FF0000008D0C85000000008" 

. "B55108B45FCC1E8083304118945FC8D45F8FF00EBCA8B45FCC9C3") 

 

VarSetCapacity(CRC32LookupTable, 256*4) 

DllCall(&CRC32_Init, "uint",&CRC32LookupTable) 



SetFormat, Integer, Hex

 

a = ABCDEFGHIJKLMNOPQRSTUVWXY

res := DllCall(&CRC32_Get, "uint",&a, "uint",StrLen(a), "uint",&CRC32LookupTable) 



MsgBox % res



r1 := ~res

MsgBox % r1 ; right answer: 0x292f0b53

r2 := res ^ 0xFFFFFFFF

MsgBox % r2 ; wrong answer: -0xd6d0f4ad
?

tic
  • Members
  • 1934 posts
  • Last active: May 30 2018 08:13 PM
  • Joined: 22 Apr 2007
Thanks olfen, but the following always gives 1059178012

#NoEnv

MCode(ByRef code, hex) { ; allocate memory and write Machine Code there
   VarSetCapacity(code,StrLen(hex)//2)
   Loop % StrLen(hex)//2
      NumPut("0x" . SubStr(hex,2*A_Index-1,2), code, A_Index-1, "Char")
}

MCode(CRC32_Init, "5589E583EC10C745FC2083B8EDC745F400000000817DF4FF0000007F508B45F48945F8C745F008000000837DF0007E238B45F883"
. "E00185C0740D8B45F8D1E83345FC8945F8EB058D45F8D1288D45F0FF08EBD78B45F48D0C85000000008B55088B45F88904118D45F4FF00EBA7C9C3")
MCode(CRC32_Get, "5589E583EC08C745FCFFFFFFFFC745F8000000008B45F83B450C732E8B45080345F80FB6003345FC25FF0000008D0C85000000008"
. "B55108B45FCC1E8083304118945FC8D45F8FF00EBCA8B45FCC9C3")

VarSetCapacity(CRC32LookupTable, 256*4)
DllCall(&CRC32_Init, "uint",&CRC32LookupTable)

FileRead, a, %A_AhkPath%
FileGetSize, size, %A_AhkPath%
res := DllCall(&CRC32_Get, "uint",&a, "uint",size, "uint",&CRC32LookupTable)
Display()

Display()
{
global res
IfLess, res, 0x104C11DB7
  res := ~res
IfGreater, res, 0x104C11DB7
  res := res ^ 0xFFFFFFFF
MsgBox % res
return
}


olfen
  • Members
  • 115 posts
  • Last active: Dec 25 2012 09:48 AM
  • Joined: 04 Jun 2005

Thanks olfen, but the following always gives 1059178012

It gives 434084816 on my computer, which is 0x19DF9BD0. Do you have installed AHK v1.0.47.04?