xoru

Untitled

Sep 6th, 2012
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.84 KB | None | 0 0
  1. local function print(...)
  2.     local szText = Input.GetText("Input1");
  3.     for i, v in pairs{...}do
  4.         szText = szText .. tostring(v).."\t";
  5.     end
  6.    
  7.     Input.SetText("Input1", szText.."\r\n");
  8. end;
  9.  
  10. if(not ASM.Initialize())then
  11.     error("Assembler couldn't be initialized.");
  12. end
  13.  
  14. -- Calculate some stuff using your input!
  15. local function asm_test_1()
  16.     local hAssembly = ASM.Assemble[[
  17.         USE32                       ;-- Important, use 32 bits. Don't use USE16 and USE64, this is a 32 bit application!
  18.         ORG     100h                ;-- The base
  19.  
  20.         XOR     EAX, EAX
  21.         MOV     EAX, [ESP + 4]      ;-- Place argument 1 in EAX for operation! Arguments to a function call are placed in ESP normally.
  22.         SAL     EAX, 2              ;-- Shift it left 2 bits.
  23.         SUB     EAX, 100            ;-- Subtract 100.
  24.         SAR     EAX, 1              ;-- Shift it right 1 bit.
  25.        
  26.         RETN                        ;-- return, the value in EAX is usually returned to the calling process.
  27.     ]];
  28.    
  29.     if(hAssembly.assembled)then
  30.         local result = hAssembly(120); -- Call the above block of assembly with the argument 120 in ESP
  31.         print(result);
  32.     else
  33.         local asmError = hAssembly:GetError();
  34.         if(asmError)then
  35.             if(asmError.errorCode ~= 0)then -- If it wasn't successful and errorCode is 0, then you probably tried assembling an empty block.
  36.                 Dialog.Message("Assembler error: "..tostring(asmError.errorCode), "Line "..tostring(asmError.errorLine)..": "..asmError.errorMessage.."\r\nCode at line: "..asmError.errorLineString);
  37.             end
  38.         end
  39.     end
  40.    
  41.     hAssembly:Free();
  42. end
  43.  
  44. -- Call MessageBox A and proxy the arguments through assembly!
  45. local function asm_test_2()
  46.     local user32 = Library.Load("user32.dll");
  47.     if(not user32)then return;end;
  48.     local MessageBoxA = user32:GetProcAddress_("MessageBoxA");
  49.     if(not MessageBoxA)then return;end;
  50.  
  51.     local hAssembly = ASM.Assemble([[
  52.         USE32                           ;-- Important, use 32 bits. Don't use USE16 and USE64, this is a 32 bit application!
  53.         ORG     100h                    ;-- The base
  54.        
  55.         PUSH    EBP                     ;-- Push EBP on the stack
  56.         MOV     EBP, ESP                ;-- Move the arguments in ESP to EBP
  57.        
  58.         PUSH    DWORD [EBP + 20]        ;-- EBP contains the arguments, last argument at offset 20, first argument at offset 8.
  59.         PUSH    DWORD [EBP + 16]        ;-- PUSH pushes these values onto the stack for CALL to pass onto the called code
  60.         PUSH    DWORD [EBP + 12]        ;-- Push the last argument first!
  61.         PUSH    DWORD [EBP + 08]        ;-- So, the first PUSH rule pushes the flags (68), 2nd pushes message, 3rd pushes title, 4th pushes window handle
  62.        
  63.         XOR     EAX, EAX                ;-- Reset the EAX register to 0
  64.        
  65.         ;-- Move the pointer to MessageBoxA into EAX, so we can call that function
  66.         ;MOV        EAX, DWORD ]]..tostring(MessageBoxA)..[[ ;-- If you use this form of concatenation, use a space after the opening operator! If you do not, newlines are ignored.
  67.         MOV     EAX, DWORD [EBP + 24]   ;-- Instead of concatenating the ASM, you can also provide this function with a pointer to MessageBoxA
  68.         CALL    EAX                     ;-- Call the function address stored in EAX
  69.         POP     EBP                     ;-- POP (remove) EBP from the stack, restoring the stack and allowing EAX to get returned.
  70.        
  71.         RETN
  72.     ]]);
  73.    
  74.     if(hAssembly.assembled)then
  75.         -- The 68 as argument 4 below is Bitwise.Or(MB_ICONINFORMATION, MB_YESNO)
  76.         local result = hAssembly(Application.GetWndHandle(), "Hello World!", "Title", 68, MessageBoxA); -- Call the above block of assembly.
  77.         if(result == IDYES)then
  78.             print("You clicked yes! :D");
  79.         else
  80.             print("You clicked no! :(");
  81.         end
  82.     else
  83.         local asmError = hAssembly:GetError();
  84.         if(asmError)then
  85.             if(asmError.errorCode ~= 0)then -- If it wasn't successful and errorCode is 0, then you probably tried assembling an empty block.
  86.                 Dialog.Message("Assembler error: "..tostring(asmError.errorCode), "Line "..tostring(asmError.errorLine)..": "..asmError.errorMessage.."\r\nCode at line: "..asmError.errorLineString);
  87.             end
  88.         end
  89.     end
  90.    
  91.     hAssembly:Free();
  92. end
  93.  
  94. -- Calculate a CRC32 checksum on data you provide!
  95. local function CRC32(szString, initCRC)
  96.     local CRC       = 0;
  97.     if(type(initCRC) == "number")then
  98.         CRC = initCRC;
  99.     end
  100.     local hAssembly = ASM.Assemble[[
  101.         USE32
  102.         ORG         100h
  103.        
  104.         PUSH        EBP                   ;-- Push EBP on the stack so it'll hold our arguments.
  105.         MOV         EBP, ESP              ;-- Move ESP into EBP, we use EBP to get to our arguments.
  106.        
  107.         ;-- Read all the arguments again and place them in registers.
  108.         MOV         EDI, DWORD [EBP + 12] ;-- length
  109.         MOV         ESI, DWORD [EBP + 08] ;-- pointer, the function call methods in MemoryEx obtain a pointer to the strings you provide :)
  110.         MOV         ECX, DWORD [EBP + 16] ;-- init crc32
  111.         NOT         ECX                   ;-- to mimic the behaviour of PB's CRC32Fingerprint function, we have to NOT the init crc. Usually the init crc is -1, PB works with 0. NOT 0 = -1 (0xFFFFFFFF)
  112.         MOV         EDX, -1               ;-- EDX = -1
  113.        
  114.         nextbyte:                         ;-- A label that gets the next byte from the buffer.
  115.             XOR     EAX, EAX              ;-- EAX = 0
  116.             XOR     EBX, EBX              ;-- EBX = 0
  117.             DB      0xAC                  ;-- LODSB instruction to get the next byte
  118.             XOR     AL, CL                ;-- XOR AL with CL
  119.             MOV     CL, CH                ;-- CL = CH
  120.             MOV     CH, DL                ;-- CH = DL
  121.             MOV     DL, DH                ;-- DL = DH
  122.             MOV     DH, 8                 ;-- DH = 8
  123.            
  124.         nextbit:                          ;-- A label that gets the next bit from the byte.
  125.             SHR     BX, 1                 ;-- Shift the bits in BX right by 1 bit.
  126.             RCR     AX, 1                 ;-- (rotate through carry) Rotate bits in AX by 1
  127.             JNC     nocarry               ;-- Jump to nocarry if carry flag wasn't set
  128.             XOR     AX, 0x08320           ;-- XOR AX with 33568
  129.             XOR     BX, 0x0EDB8           ;-- XOR BX with 60856
  130.            
  131.         nocarry:                          ;-- If the carry flag wasn't set
  132.             DEC     DH                    ;-- DH = DH - 1
  133.             JNZ     nextbit               ;-- If DH isn't zero, jump to nextbit
  134.             XOR     ECX, EAX              ;-- XOR ECX with EAX
  135.             XOR     EDX, EBX              ;-- XOR EDX with EBX
  136.             DEC     EDI                   ;-- EDI = EDI - 1, finished a byte.
  137.             JNZ     nextbyte              ;-- If EDI isn't zero, jump to nextbyte
  138.             NOT     EDX                   ;-- Invert EDX bits - 1s complement
  139.             NOT     ECX                   ;-- Invert ECX bits - 1s complement
  140.             MOV     EAX, EDX              ;-- Move EDX into EAX
  141.             ROL     EAX, 16               ;-- Rotate bits in EAX left by 16 places
  142.             MOV     AX, CX                ;-- Move CX into AX
  143.            
  144.         POP     EBP                       ;-- Remove EBP from the stack, which held the arguments.
  145.         RETN                              ;-- Return, the value in EAX contains the CRC checksum.
  146.     ]];
  147.    
  148.     if(hAssembly.assembled)then
  149.         CRC = hAssembly(szString, szString:len(), CRC); -- Call the CRC code
  150.     else
  151.         local asmError = hAssembly:GetError();
  152.         if(asmError)then
  153.             if(asmError.errorCode ~= 0)then -- If it wasn't successful and errorCode is 0, then you probably tried assembling an empty block.
  154.                 Dialog.Message("Assembler error: "..tostring(asmError.errorCode), "Line "..tostring(asmError.errorLine)..": "..asmError.errorMessage.."\r\nCode at line: "..asmError.errorLineString);
  155.             end
  156.         end
  157.     end
  158.    
  159.     hAssembly:Free();
  160.    
  161.     return CRC;
  162. end
  163.  
  164. -- Check if your processor supports the SSE2 instruction set.
  165. local function CPU_SSE2()
  166.     local sse2 = false;
  167.     local hAssembly = ASM.Assemble[[
  168.         USE32                       ;-- Important, use 32 bits. Don't use USE16 and USE64, this is a 32 bit application!
  169.         ORG     100h                ;-- The base
  170.  
  171.         MOV    EAX, 1h
  172.         CPUID
  173.         TEST   ECX, 2000000h
  174.         JNZ    @F
  175.             MOV EAX,   0
  176.         @@:
  177.             MOV EAX,   1
  178.        
  179.         RETN                        ;-- return, the value in EAX is usually returned to the calling process.
  180.     ]];
  181.    
  182.     if(hAssembly.assembled)then
  183.         sse2 = (hAssembly() == 1);
  184.     else
  185.         local asmError = hAssembly:GetError();
  186.         if(asmError)then
  187.             if(asmError.errorCode ~= 0)then -- If it wasn't successful and errorCode is 0, then you probably tried assembling an empty block.
  188.                 Dialog.Message("Assembler error: "..tostring(asmError.errorCode), "Line "..tostring(asmError.errorLine)..": "..asmError.errorMessage.."\r\nCode at line: "..asmError.errorLineString);
  189.             end
  190.         end
  191.     end
  192.    
  193.     hAssembly:Free();
  194.    
  195.     return sse2;
  196. end
  197.  
  198. asm_test_1();
  199. asm_test_2();
  200. print(CPU_SSE2())
  201.  
  202. local crc = CRC32("Hello World!!");
  203. print("CRC32 of \"Hello World!!\"", crc, "("..tostring(Bitwise.And(crc, 0xFFFFFFFF)).." unsigned)");
Advertisement
Add Comment
Please, Sign In to add comment