def test_two_procs(self): """ Make sure second proc gets called correctly """ a = assembler() a.ADStr('hello_planets','Hello, all %i planets!\n\0') a.AP("_main") a.AI("CALL get_planets") a.AI("PUSH EAX") a.AI("PUSH hello_planets") a.AI("CALL _printf") a.AI("ADD ESP,0x8") #printf is _cdecl a.AI("XOR EAX,EAX") a.EP() #get_planets proc a.AP("get_planets") a.AI("MOV EAX,0x12") a.EP() cp = a.Compile() coff = CpToCoff(cp,"-defaultlib:LIBCPMTD -defaultlib:LIBCMTD -defaultlib:OLDNAMES ").makeReleaseCoff() f = file("output/testTwoProcs.obj","wb") coff.WriteToFile(f) f.close() if sys.platform == 'win32': self.assertEquals(os.system(linkCmd("testTwoProcs")), 0) self.assertEquals(os.popen("cd output && testTwoProcs.exe").read(), "Hello, all 18 planets!\n") else: print "Skipping linker test, coff files are only valid on win32 platforms"
def test_simple_function(self): a = assembler() a.ADStr("hello_world", "Hello world!\n\0") a.AP("test_print", PYTHON) a.AddLocal("self") a.AddLocal("args") #a.AI("INT 3") a.AI("PUSH hello_world") a.AI("CALL PySys_WriteStdout") a.AI("MOV EAX,%s" % id(None)) a.AI("ADD [EAX],0x1") #refcount a.EP() a.AP("test_print2", PYTHON) a.AddLocal("self") a.AddLocal("args") #a.AI("INT 3") a.AI("PUSH hello_world") a.AI("CALL PySys_WriteStdout") a.AI("MOV EAX,%s" % id(None)) a.AI("ADD [EAX],0x1") #refcount a.EP() mem = CpToMemory(a.Compile()) mem.MakeMemory() mem.BindPythonFunctions(globals()) test_print("Foo") test_print2('bar')
def test_proc(self): a = assembler() a.ADStr('hello_world', 'Hello, World\n\0') a.AP("_main") a.AI("PUSH hello_world") a.AI("CALL _printf") a.AI("ADD ESP,0x4") # _cdecl cleanup a.AI("XOR EAX,EAX") a.EP() cp = a.Compile() coff = CpToCoff( cp, "-defaultlib:LIBCPMTD -defaultlib:LIBCMTD -defaultlib:OLDNAMES " ).makeReleaseCoff() f = file("output/testProc.obj", "wb") coff.WriteToFile(f) f.close() if sys.platform == 'win32': self.assertEquals(os.system(linkCmd("testProc")), 0) self.assertEquals( os.popen("cd output && testProc.exe").read(), "Hello, World\n") else: print "Skipping linker test, coff files are only valid on win32 platforms"
def test_goodbye_world(self): """ Make sure we see the second param instead of defaulting to the first """ a = assembler() a.ADStr('hello_world','Hello, World\n\0') a.ADStr('Goodbye_World','Goodbye, World\n\0') a.AP("_main") a.AI("PUSH Goodbye_World") a.AI("CALL _printf") a.AI("ADD ESP,0x4") # _cdecl a.AI("XOR EAX,EAX") a.EP() cp = a.Compile() coff = CpToCoff(cp,"-defaultlib:LIBCPMTD -defaultlib:LIBCMTD -defaultlib:OLDNAMES ").makeReleaseCoff() f = file("output/testGoodbyeWorld.obj","wb") coff.WriteToFile(f) f.close() if sys.platform == 'win32': self.assertEquals(os.system(linkCmd("testGoodbyeWorld")), 0) self.assertEquals(os.popen("cd output &&testGoodbyeWorld.exe").read(), "Goodbye, World\n") else: print "Skipping linker test, coff files are only valid on win32 platforms"
def test_hello_world(self): a = assembler() a.ADStr('hello_world', 'Hello, World\n\0') a.AIL("_main") a.AI("PUSH EBP") a.AI("MOV EBP,ESP") a.AI("SUB ESP,0x40") a.AI("PUSH EBX") a.AI("PUSH ESI") a.AI("PUSH EDI") a.AI("LEA EDI,[EBP-0x40]") a.AI("MOV ECX,0x10") a.AI("MOV EAX,0x0CCCCCCCC") a.AI("REP STOS [EDI]") a.AI("PUSH hello_world") a.AI("CALL _printf") a.AI("ADD ESP,4") a.AI("XOR EAX,EAX") a.AI("POP EDI") a.AI("POP ESI") a.AI("POP EBX") a.AI("ADD ESP,0x40") a.AI("CMP EBP,ESP") a.AI("CALL __chkesp") a.AI("MOV ESP,EBP") a.AI("POP EBP") a.AI("RET") a.ADStr("goodbye_world", "GOODBYE WORLD!\n\0") cp = a.Compile() self.assertEquals( cp.Code, 'U\x8b\xec\x81\xec@\x00\x00\x00SVW\x8d}\xc0\xb9\x10\x00\x00\x00\xb8\xcc\xcc\xcc\xcc\xf3\xabh\x00\x00\x00\x00\xe8\x00\x00\x00\x00\x83\xc4\x043\xc0_^[\x81\xc4@\x00\x00\x00;\xec\xe8\x00\x00\x00\x00\x8b\xe5]\xc3' ) self.assertEquals(cp.CodePatchins, [('hello_world', 28, 2), ('_printf', 33, 1), ('__chkesp', 54, 1)]) self.assertEquals(cp.CodeSymbols, [('_main', 0, 0)]) self.assertEquals(cp.Data, 'Hello, World\n\x00GOODBYE WORLD!\n\x00') self.assertEquals(cp.DataSymbols, [('hello_world', 0), ('goodbye_world', 14)]) coff = CpToCoff( cp, "-defaultlib:LIBCPMTD -defaultlib:LIBCMTD -defaultlib:OLDNAMES " ).makeReleaseCoff() f = file("output/testHelloWorld.obj", "wb") coff.WriteToFile(f) f.close() if sys.platform == 'win32': self.assertEquals(os.system(linkCmd("testHelloWorld")), 0) self.assertEquals( os.popen("cd output && testHelloWorld.exe").read(), "Hello, World\n") else: print "Skipping linker test, coff files are only valid on win32 platforms"
def test_goodbye_world(self): """ Make sure we see the second param instead of defaulting to the first """ a = assembler() a.ADStr('hello_world', 'Hello, World\n\0') a.ADStr('Goodbye_World', 'Goodbye, World\n\0') a.AP("_main") a.AI("PUSH Goodbye_World") a.AI("CALL _printf") a.AI("ADD ESP,0x4") # _cdecl a.AI("XOR EAX,EAX") a.EP() cp = a.Compile() coff = CpToCoff( cp, "-defaultlib:LIBCPMTD -defaultlib:LIBCMTD -defaultlib:OLDNAMES " ).makeReleaseCoff() f = file("output/testGoodbyeWorld.obj", "wb") coff.WriteToFile(f) f.close() if sys.platform == 'win32': self.assertEquals(os.system(linkCmd("testGoodbyeWorld")), 0) self.assertEquals( os.popen("cd output &&testGoodbyeWorld.exe").read(), "Goodbye, World\n") else: print "Skipping linker test, coff files are only valid on win32 platforms"
def test_simple_function(self): a = assembler() a.ADStr("hello_world", "Hello world!\n\0") a.AP("test_print", PYTHON) a.AddLocal("self") a.AddLocal("args") #a.AI("INT 3") a.AI("PUSH hello_world") a.AI("CALL PySys_WriteStdout") a.AI("ADD ESP,0x4") #CDECL CLEANUP a.AI("MOV EAX,%s" % id(None)) a.AI("ADD [EAX],0x1") #refcount a.EP() a.AP("test_print2", PYTHON) a.AddLocal("self") a.AddLocal("args") #a.AI("INT 3") a.AI("PUSH hello_world") a.AI("CALL PySys_WriteStdout") a.AI("ADD ESP,0x4") #cdecl cleanup a.AI("MOV EAX,%s" % id(None)) a.AI("ADD [EAX],0x1") #refcount a.EP() mem = CpToMemory(a.Compile()) mem.MakeMemory() mem.BindPythonFunctions(globals()) test_print("Foo") test_print2('bar')
def test_hello_world(self): a = assembler() a.ADStr('hello_world','Hello, World\n\0') a.AIL("_main") a.AI("PUSH EBP") a.AI("MOV EBP,ESP") a.AI("SUB ESP,0x40") a.AI("PUSH EBX") a.AI("PUSH ESI") a.AI("PUSH EDI") a.AI("LEA EDI,[EBP-0x40]") a.AI("MOV ECX,0x10") a.AI("MOV EAX,0x0CCCCCCCC") a.AI("REP STOS [EDI]") a.AI("PUSH hello_world") a.AI("CALL _printf") a.AI("ADD ESP,4") a.AI("XOR EAX,EAX") a.AI("POP EDI") a.AI("POP ESI") a.AI("POP EBX") a.AI("ADD ESP,0x40") a.AI("CMP EBP,ESP") a.AI("CALL __chkesp") a.AI("MOV ESP,EBP") a.AI("POP EBP") a.AI("RET") a.ADStr("goodbye_world", "GOODBYE WORLD!\n\0") cp = a.Compile() self.assertEquals(cp.Code,'U\x8b\xec\x81\xec@\x00\x00\x00SVW\x8d}\xc0\xb9\x10\x00\x00\x00\xb8\xcc\xcc\xcc\xcc\xf3\xabh\x00\x00\x00\x00\xe8\x00\x00\x00\x00\x83\xc4\x043\xc0_^[\x81\xc4@\x00\x00\x00;\xec\xe8\x00\x00\x00\x00\x8b\xe5]\xc3') self.assertEquals(cp.CodePatchins,[('hello_world', 28, 2), ('_printf', 33, 1), ('__chkesp', 54, 1)]) self.assertEquals(cp.CodeSymbols,[('_main', 0, 0)]) self.assertEquals(cp.Data,'Hello, World\n\x00GOODBYE WORLD!\n\x00') self.assertEquals(cp.DataSymbols,[('hello_world', 0), ('goodbye_world', 14)]) coff = CpToCoff(cp,"-defaultlib:LIBCPMTD -defaultlib:LIBCMTD -defaultlib:OLDNAMES ").makeReleaseCoff() f = file("output/testHelloWorld.obj","wb") coff.WriteToFile(f) f.close() if sys.platform == 'win32': self.assertEquals(os.system(linkCmd("testHelloWorld")),0) self.assertEquals(os.popen("cd output && testHelloWorld.exe").read(),"Hello, World\n") else: print "Skipping linker test, coff files are only valid on win32 platforms"
def test_params(self): """ Make sure params get refrennced correctly """ a = assembler() a.ADStr('hello_planets', '3h + 12h + 12h = %xh\n\0') a.AP("_main") a.AI("PUSH EBX") a.AI("MOV EBX,0x12") a.AI("PUSH EBX") a.AI("MOV EBX,0x3") a.AI("PUSH EBX") a.AI("CALL get_x_plus_two_y") a.AI("PUSH EAX") a.AI("PUSH hello_planets") a.AI("CALL _printf") a.AI("ADD ESP,0x8") #printf is _cdecl #a.AI("XOR EAX,EAX") a.AI("POP EBX") a.EP() #get_planets proc a.AP("get_x_plus_two_y") a.AA("x") a.AA("y") a.AI("XOR EAX,EAX") a.AI("MOV EAX,x") a.AI("ADD EAX,y") a.AI("ADD EAX,y") a.EP() cp = a.Compile() coff = CpToCoff( cp, "-defaultlib:LIBCMT -defaultlib:OLDNAMES ").makeReleaseCoff() f = file("output/testParams.obj", "wb") coff.WriteToFile(f) f.close() if sys.platform == 'win32': self.assertEquals(os.system(linkCmd("testParams")), 0) self.assertEquals( os.popen("cd output && testParams.exe").read(), "3h + 12h + 12h = 27h\n") else: print "Skipping linker test, coff files are only valid on win32 platforms"
def test_linker(self): """ Make sure params get refrennced correctly """ a = assembler() a.ADStr('hello_planets','3h + 12h + 12h = %xh\n\0') a.AP("_main") a.AI("PUSH EBX") a.AI("MOV EBX,0x12") a.AI("PUSH EBX") a.AI("MOV EBX,0x3") a.AI("PUSH EBX") a.AI("CALL get_x_plus_two_y") a.AI("PUSH EAX") a.AI("PUSH hello_planets") a.AI("CALL _printf") a.AI("ADD ESP,0x8") #printf is _cdecl #a.AI("XOR EAX,EAX") a.AI("POP EBX") a.EP() #get_planets proc a.AP("get_x_plus_two_y") a.AA("x") a.AA("y") a.AI("XOR EAX,EAX") a.AI("MOV EAX,x") a.AI("ADD EAX,y") a.AI("ADD EAX,y") a.EP() cp = a.Compile() coff = CpToCoff(cp,"-defaultlib:LIBMT -defaultlib:OLDNAMES ").makeReleaseCoff() f = file("output/testLinker.obj","wb") coff.WriteToFile(f) f.close() if sys.platform == "win32": self.assertEquals(os.system(linkCmd("testLinker")), 0) self.assertEquals(os.popen("cd output && testLinker.exe").read(), "3h + 12h + 12h = 27h\n") else: print "Skipping linker test, coff files are only valid on win32 platforms"
def test_proc(self): a = assembler() a.ADStr('hello_world','Hello, World\n\0') a.AP("_main") a.AI("PUSH hello_world") a.AI("CALL _printf") a.AI("ADD ESP,0x4") # _cdecl cleanup a.AI("XOR EAX,EAX") a.EP() cp = a.Compile() coff = CpToCoff(cp,"-defaultlib:LIBCPMTD -defaultlib:LIBCMTD -defaultlib:OLDNAMES ").makeReleaseCoff() f = file("output/testProc.obj","wb") coff.WriteToFile(f) f.close() if sys.platform == 'win32': self.assertEquals(os.system(linkCmd("testProc")), 0) self.assertEquals(os.popen("cd output && testProc.exe").read(), "Hello, World\n") else: print "Skipping linker test, coff files are only valid on win32 platforms"
def test_two_procs(self): """ Make sure second proc gets called correctly """ a = assembler() a.ADStr('hello_planets', 'Hello, all %i planets!\n\0') a.AP("_main") a.AI("CALL get_planets") a.AI("PUSH EAX") a.AI("PUSH hello_planets") a.AI("CALL _printf") a.AI("ADD ESP,0x8") #printf is _cdecl a.AI("XOR EAX,EAX") a.EP() #get_planets proc a.AP("get_planets") a.AI("MOV EAX,0x12") a.EP() cp = a.Compile() coff = CpToCoff( cp, "-defaultlib:LIBCPMTD -defaultlib:LIBCMTD -defaultlib:OLDNAMES " ).makeReleaseCoff() f = file("output/testTwoProcs.obj", "wb") coff.WriteToFile(f) f.close() if sys.platform == 'win32': self.assertEquals(os.system(linkCmd("testTwoProcs")), 0) self.assertEquals( os.popen("cd output && testTwoProcs.exe").read(), "Hello, all 18 planets!\n") else: print "Skipping linker test, coff files are only valid on win32 platforms"
# Copyright 2004-2010 Grant T. Olson. # See license.txt for terms. from pyasm.x86asm import assembler from pyasm.x86cpToMemory import CpToMemory a = assembler() a("!COMMENT This is a samle hello world program") a("!COMMENT by Grant") a("!CHARS hello_str 'hello world\n\0'") a("!PROC hello_world PYTHON") a("!ARG self") a("!ARG args") #a(" INT 3") a(" PUSH hello_str") a(" CALL PySys_WriteStdout") a(" ADD ESP,0x4") #CDECL a(" MOV EAX,%s" % id(None)) a(" ADD [EAX], 0x1") a("!ENDPROC") cp = a.Compile() mem = CpToMemory(cp) mem.MakeMemory() mem.BindPythonFunctions(globals()) hello_world()
# Copyright 2004-2010 Grant T. Olson. # See license.txt for terms. from pyasm.x86asm import assembler, CDECL from pyasm.x86cpToMemory import CpToMemory nonePointer = id(None) noneRefcount = nonePointer a = assembler() a.ADStr("hello_world", "Hello world!\n\0") a.AP("test_print", CDECL) a.AddLocal("self") a.AddLocal("args") #a.AI("INT 3") a.AI("PUSH hello_world") a.AI("CALL PySys_WriteStdout") #a.AI("INT 3") a.AI("MOV EAX,%s" % id(None)) a.AI("ADD [EAX],0x1") a.EP() mem = CpToMemory(a.Compile()) mem.MakeMemory() mem.BindPythonFunctions(globals()) def normalHelloWorld(): print "Hello World!" return None
def test_no_endproc(self): a = assembler() a("!PROC foo") a("NOP") self.failUnlessRaises(x86asmError, a.Compile)