annotate test/testx86asm.py @ 333:dcae6574c974

Increment to qt5
author Windel Bouwman
date Sun, 09 Feb 2014 15:27:57 +0100
parents 534b94b40aa8
children 3b0c495e3008
rev   line source
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
1 #!/usr/bin/python
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
2
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
3 import unittest
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
4 from testasm import AsmTestCaseBase
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
5
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
6
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
7 class AssemblerTestCase(AsmTestCaseBase):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
8 """
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
9 test methods start with 'test*'
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
10 Checks several assembly constructs agains their bytecodes
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
11 """
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
12 def setUp(self):
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
13 self.skipTest('not implemented yet')
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
14 self.assembler = Assembler('x86-64')
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
15 a = Assembler()
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
16
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
17 @unittest.skip
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
18 def testX86(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
19 self.feed('mov rax, rbx') # ; 0x48, 0x89, 0xd8
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
20 self.feed('xor rcx, rbx') # ; 0x48, 0x31, 0xd9
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
21 self.feed('inc rcx') # ; 0x48 0xff 0xc1
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
22 self.check('48 89 d8 48 31 d9 48 ff c1')
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
23
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
24 def tstAssembler(self):
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
25 """ Check all kind of assembler cases """
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
26 assert(assembler.shortjump(5) == [0xeb, 0x5])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
27 assert(assembler.shortjump(-2) == [0xeb, 0xfc])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
28 assert(assembler.shortjump(10,'GE') == [0x7d, 0xa])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
29 assert(assembler.nearjump(5) == [0xe9, 0x5,0x0,0x0,0x0])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
30 assert(assembler.nearjump(-2) == [0xe9, 0xf9, 0xff,0xff,0xff])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
31 assert(assembler.nearjump(10,'LE') == [0x0f, 0x8e, 0xa,0x0,0x0,0x0])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
32
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
33 def testCall(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
34 self.feed('call r10')
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
35 self.check('')
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
36 self.feed('call rcx')
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
37
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
38 # assert(assembler.call('r10') == [0x41, 0xff, 0xd2])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
39 # assert(assembler.call('rcx') == [0xff, 0xd1])
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 284
diff changeset
40
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
41 def testXOR(self):
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
42 assert(assembler.xorreg64('rax', 'rax') == [0x48, 0x31, 0xc0])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
43 assert(assembler.xorreg64('r9', 'r8') == [0x4d, 0x31, 0xc1])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
44 assert(assembler.xorreg64('rbx', 'r11') == [0x4c, 0x31, 0xdb])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
45
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
46 def testINC(self):
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
47 assert(assembler.increg64('r11') == [0x49, 0xff, 0xc3])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
48 assert(assembler.increg64('rcx') == [0x48, 0xff, 0xc1])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
49
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
50 def testPush(self):
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
51 assert(assembler.push('rbp') == [0x55])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
52 assert(assembler.push('rbx') == [0x53])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
53 assert(assembler.push('r12') == [0x41, 0x54])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
54
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
55 def testPop(self):
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
56 self.feed('pop rbx')
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
57 self.feed('pop rbp')
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
58 self.feed('pop r12')
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
59 assert(assembler.pop('rbx') == [0x5b])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
60 assert(assembler.pop('rbp') == [0x5d])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
61 assert(assembler.pop('r12') == [0x41, 0x5c])
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
62
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
63 def testAsmLoads(self):
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
64 # TODO constant add testcases
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
65 assert(assembler.mov('rbx', 'r14') == [0x4c, 0x89, 0xf3])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
66 assert(assembler.mov('r12', 'r8') == [0x4d, 0x89, 0xc4])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
67 assert(assembler.mov('rdi', 'rsp') == [0x48, 0x89, 0xe7])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
68
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
69 def testAsmMemLoads(self):
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
70 assert(assembler.mov('rax', ['r8','r15',0x11]) == [0x4b,0x8b,0x44,0x38,0x11])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
71 assert(assembler.mov('r13', ['rbp','rcx',0x23]) == [0x4c,0x8b,0x6c,0xd,0x23])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
72
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
73 assert(assembler.mov('r9', ['rbp',-0x33]) == [0x4c,0x8b,0x4d,0xcd])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
74 #assert(assembler.movreg64('rbx', ['rax']) == [0x48, 0x8b,0x18])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
75
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
76 assert(assembler.mov('rax', [0xb000]) == [0x48,0x8b,0x4,0x25,0x0,0xb0,0x0,0x0])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
77 assert(assembler.mov('r11', [0xa0]) == [0x4c,0x8b,0x1c,0x25,0xa0,0x0,0x0,0x0])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
78
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
79 assert(assembler.mov('r11', ['RIP', 0xf]) == [0x4c,0x8b,0x1d,0x0f,0x0,0x0,0x0])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
80
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
81 def testAsmMemStores(self):
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
82 assert(assembler.mov(['rbp', 0x13],'rbx') == [0x48,0x89,0x5d,0x13])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
83 assert(assembler.mov(['r12', 0x12],'r9') == [0x4d,0x89,0x4c,0x24,0x12])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
84 assert(assembler.mov(['rcx', 0x11],'r14') == [0x4c,0x89,0x71,0x11])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
85
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
86
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
87 assert(assembler.mov([0xab], 'rbx') == [0x48,0x89,0x1c,0x25,0xab,0x0,0x0,0x0])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
88 assert(assembler.mov([0xcd], 'r13') == [0x4c,0x89,0x2c,0x25,0xcd,0x0,0x0,0x0])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
89
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
90 assert(assembler.mov(['RIP', 0xf], 'r9') == [0x4c,0x89,0x0d,0x0f,0x0,0x0,0x0])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
91
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
92 def testAsmMOV8(self):
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
93 assert(assembler.mov(['rbp', -8], 'al') == [0x88, 0x45, 0xf8])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
94 assert(assembler.mov(['r11', 9], 'cl') == [0x41, 0x88, 0x4b, 0x09])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
95
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
96 assert(assembler.mov(['rbx'], 'al') == [0x88, 0x03])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
97 assert(assembler.mov(['r11'], 'dl') == [0x41, 0x88, 0x13])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
98
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
99 def testAsmLea(self):
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
100 assert(assembler.leareg64('r11', ['RIP', 0xf]) == [0x4c,0x8d,0x1d,0x0f,0x0,0x0,0x0])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
101 assert(assembler.leareg64('rsi', ['RIP', 0x7]) == [0x48,0x8d,0x35,0x07,0x0,0x0,0x0])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
102
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
103 assert(assembler.leareg64('rcx', ['rbp', -8]) == [0x48,0x8d,0x4d,0xf8])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
104
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
105 def testAssemblerCMP(self):
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
106 assert(assembler.cmpreg64('rdi', 'r13') == [0x4c, 0x39, 0xef])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
107 assert(assembler.cmpreg64('rbx', 'r14') == [0x4c, 0x39, 0xf3])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
108 assert(assembler.cmpreg64('r12', 'r9') == [0x4d, 0x39, 0xcc])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
109
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
110 assert(assembler.cmpreg64('rdi', 1) == [0x48, 0x83, 0xff, 0x01])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
111 assert(assembler.cmpreg64('r11', 2) == [0x49, 0x83, 0xfb, 0x02])
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
112
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
113 def testAssemblerADD(self):
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
114 assert(assembler.addreg64('rbx', 'r13') == [0x4c, 0x01, 0xeb])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
115 assert(assembler.addreg64('rax', 'rbx') == [0x48, 0x01, 0xd8])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
116 assert(assembler.addreg64('r12', 'r13') == [0x4d, 0x01, 0xec])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
117
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
118 assert(assembler.addreg64('rbx', 0x13) == [0x48, 0x83, 0xc3, 0x13])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
119 assert(assembler.addreg64('r11', 0x1234567) == [0x49, 0x81, 0xc3, 0x67, 0x45,0x23,0x1])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
120 assert(assembler.addreg64('rsp', 0x33) == [0x48, 0x83, 0xc4, 0x33])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
121
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
122 def testAssemblerSUB(self):
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
123 assert(assembler.subreg64('rdx', 'r14') == [0x4c, 0x29, 0xf2])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
124 assert(assembler.subreg64('r15', 'rbx') == [0x49, 0x29, 0xdf])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
125 assert(assembler.subreg64('r8', 'r9') == [0x4d, 0x29, 0xc8])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
126
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
127 assert(assembler.subreg64('rsp', 0x123456) == [0x48, 0x81, 0xec, 0x56,0x34,0x12,0x0])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
128 assert(assembler.subreg64('rsp', 0x12) == [0x48, 0x83, 0xec, 0x12])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
129
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
130 def testAssemblerIDIV(self):
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
131 assert(assembler.idivreg64('r11') == [0x49, 0xf7, 0xfb])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
132 assert(assembler.idivreg64('rcx') == [0x48, 0xf7, 0xf9])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
133 assert(assembler.idivreg64('rsp') == [0x48, 0xf7, 0xfc])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
134
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 287
diff changeset
135 def testAssemblerIMUL(self):
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
136 assert(assembler.imulreg64_rax('rdi') == [0x48, 0xf7, 0xef])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
137 assert(assembler.imulreg64_rax('r10') == [0x49, 0xf7, 0xea])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
138 assert(assembler.imulreg64_rax('rdx') == [0x48, 0xf7, 0xea])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
139
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
140 assert(assembler.imulreg64('r11', 'rdi') == [0x4c, 0xf, 0xaf, 0xdf])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
141 assert(assembler.imulreg64('r12', 'rbx') == [0x4c, 0xf, 0xaf, 0xe3])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
142 # nasm generates this machine code: 0x4d, 0x6b, 0xff, 0xee
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
143 # This also works: 4D0FAFFE (another variant?? )
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
144 assert(assembler.imulreg64('r15', 'r14') == [0x4d, 0x0f, 0xaf, 0xfe])
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
145
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
146
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
147 if __name__ == '__main__':
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
148 unittest.main()
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
149