Andreik Posted November 8, 2011 Share Posted November 8, 2011 Hi guys,I wrote a factorial function in assembly and I called it from AutoIt like in example below:#AutoIt3Wrapper_UseX64=n #include <Memory.au3> $iNumber = 7 MsgBox(0,"",Factorial($iNumber)) Func Factorial($Number) $bOPCode = "0x5589E58B450883F8007C1A83F8017E0E89C149F7E183F90177F85DC20400B801000000EBF583C8FFEBF0" $iSize = BinaryLen($bOPCode) $pBuffer = _MemVirtualAlloc(0,$iSize,$MEM_COMMIT,$PAGE_EXECUTE_READWRITE) $tBuffer = DllStructCreate("byte[" & $iSize & "]",$pBuffer) DllStructSetData($tBuffer,1,$bOPCode) $aRet = DllCallAddress("int",$pBuffer,"int",$iNumber) _MemVirtualFree($pBuffer,$iSize,$MEM_RELEASE) If IsArray($aRet) Then Return $aRet[0] Else Return "ERROR" EndIf EndFuncThe assembly code from where I got the OP code is this one:use32 push ebp mov ebp, esp mov eax, [ebp + 08] cmp eax,0 jl Error cmp eax,1 jle Set1 mov ecx,eax Again: dec ecx mul ecx cmp ecx,1 ja Again Result: pop ebp ret 4 Set1: mov eax,1 jmp Result Error: or eax,0FFFFFFFFh jmp ResultAll this works good but I have the limitation of int data type, so one guy suggest me to work with some x87 instructions to extend this limitation. He provide me some code:factnr: fld1 fild dword[esp+4] ;arg1 redof: fld1 fcomip st,st1 jae exit1 fld st fld1 fsubp fxch fmulp st2,st jmp redof exit1: fstp st ; result on st0 ret 4but I don't know how to get the result from ST0. Any idea? When the words fail... music speaks. Link to comment Share on other sites More sharing options...
trancexx Posted November 8, 2011 Share Posted November 8, 2011 This is kind of confusing. What do you mean? You have the result where it should be. Your code returns float. That's all.DllCallAddress("float", ... What is it that's confusing to you? ♡♡♡ . eMyvnE Link to comment Share on other sites More sharing options...
Andreik Posted November 8, 2011 Author Share Posted November 8, 2011 Just for curious what op code you got? I still get program crash, I got this op code with ollydbg "0xD9E867DB442404D9E8DFF1730CD9C0D9E8DEE9D9C9DECAEBEEDDD8C20400" When the words fail... music speaks. Link to comment Share on other sites More sharing options...
trancexx Posted November 8, 2011 Share Posted November 8, 2011 Got? I didn't get any code. I run code like that in my head. ♡♡♡ . eMyvnE Link to comment Share on other sites More sharing options...
Andreik Posted November 8, 2011 Author Share Posted November 8, 2011 (edited) Ahhh your mind seems to be a processor I tried with double, with float but the program still crash. EDIT: Got it, I don't know why I got 1 byte more with OllyDbg, I got opcode with FASM Library and seems to work good. #include <Memory.au3> $iNumber = 7 MsgBox(0,"",Factorial($iNumber)) Func Factorial($Number) $bOPCode = "0xD9E8DB442404D9E8DFF1730CD9C0D9E8DEE9D9C9DECAEBEEDDD8C20400" $iSize = BinaryLen($bOPCode) $pBuffer = _MemVirtualAlloc(0,$iSize,$MEM_COMMIT,$PAGE_EXECUTE_READWRITE) $tBuffer = DllStructCreate("byte[" & $iSize & "]",$pBuffer) DllStructSetData($tBuffer,1,$bOPCode) $aRet = DllCallAddress("double",$pBuffer,"int",$iNumber) _MemVirtualFree($pBuffer,$iSize,$MEM_RELEASE) If IsArray($aRet) Then Return $aRet[0] Else Return "ERROR" EndIf EndFunc Thank you trancexx you help me a lot! Edited November 8, 2011 by Andreik When the words fail... music speaks. Link to comment Share on other sites More sharing options...
scan88 Posted December 16, 2011 Share Posted December 16, 2011 your death to get it running again if there is no Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted December 16, 2011 Moderators Share Posted December 16, 2011 scan88, Welcome to the AutoIt forum. Could you expand on that last post a bit - I am not at all sure what point you are making. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now