#include <ASM.au3>
; Initial an asm object
Global $Asm = AsmInit()
Demo1()
Demo2()
Demo3()
Demo4()
Func Demo1()
; Demo 1: Using Parameters
AsmReset($Asm)
AsmAdd($Asm, "push ebp")
AsmAdd($Asm, "mov ebp, esp")
AsmAdd($Asm, "mov eax, [ebp + 08]")
AsmAdd($Asm, "add eax, [ebp + 0c]")
AsmAdd($Asm, "pop ebp")
AsmAdd($Asm, "retn 8")
ConsoleWrite(String(AsmGetBinary($Asm)) & @CRLF)
$Ret = MemoryFuncCall("int", AsmGetPtr($Asm), "int", 1, "int", 2)
MsgBox(0, "Demo 1: Using Parameters", "1 + 2 = " & $Ret[0])
EndFunc
Func Demo2()
; Demo 2: Read Time-Stamp Counter
AsmReset($Asm)
AsmAdd($Asm, "push ebp")
AsmAdd($Asm, "mov ebp, esp")
AsmAdd($Asm, "rdtsc")
AsmAdd($Asm, "mov ecx, [ebp + 08]")
AsmAdd($Asm, "mov [ecx], edx")
AsmAdd($Asm, "pop ebp")
AsmAdd($Asm, "retn 4")
ConsoleWrite(String(AsmGetBinary($Asm)) & @CRLF)
$Ret = MemoryFuncCall("uint", AsmGetPtr($Asm), "uint*", 0)
MsgBox(0, "Demo 2: Read Time-Stamp Counter", "RDTSC = " & Hex($Ret[1]) & Hex($Ret[0], 8))
Return Hex($Ret[1]) & Hex($Ret[0], 8)
EndFunc
Func Demo3()
; Demo 3: Using Label
AsmReset($Asm)
AsmAdd($Asm, "mov eax, 0")
AsmAdd($Asm, "mov ecx, 10")
AsmAdd($Asm, "label:")
AsmAdd($Asm, "inc eax")
AsmAdd($Asm, "loop @label")
AsmAdd($Asm, "ret")
ConsoleWrite(String(AsmGetBinary($Asm)) & @CRLF)
$Ret = MemoryFuncCall("int", AsmGetPtr($Asm))
MsgBox(0, "Demo 3: Using Label", "loop label for " & $Ret[0] & " times")
EndFunc
Func Demo4()
; Demo 4: Call AutoIt Func From Assembly
$AutoItFunc = DllCallbackRegister("AutoItFunc", "int", "")
AsmReset($Asm)
AsmAdd($Asm, "call " & DllCallbackGetPtr($AutoItFunc))
AsmAdd($Asm, "shl eax, 1")
AsmAdd($Asm, "ret")
ConsoleWrite(String(AsmGetBinary($Asm)) & @CRLF)
$Ret = MemoryFuncCall("int", AsmGetPtr($Asm))
MsgBox(0, "Demo 4: AutoIt Function", "BitShift return value of AutoItFunc: " & $Ret[0])
DllCallbackFree($AutoItFunc)
EndFunc
Func AutoItFunc()
MsgBox(0, "AutoItFunc", "Called by Inline Assembly")
Return 123
EndFunc
; Release the asm object
AsmExit($Asm)
Exit