-
Notifications
You must be signed in to change notification settings - Fork 263
/
Copy pathppclib.py
107 lines (102 loc) · 3.23 KB
/
ppclib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
code = '''
rlwinm r8, r27, 0xc, 5, 0x1a
subfic r3, r9, 0x7320
stwu r1, -0x30(r1)
stw r31, 0x2c(r1)
mr r31, r1
li r3, 0
stw r3, 0x28(r31)
li r3, 0x78ea
stw r3, 0x24(r31)
li r3, 0x15ec
stw r3, 0x20(r31)
lis r3, 0
ori r4, r3, 0xf8a5
stw r4, 0x1c(r31)
li r4, 0x677f
stw r4, 0x18(r31)
ori r3, r3, 0xc3fa
stw r3, 0x14(r31)
lwz r3, 0x24(r31)
xori r3, r3, 0xfdd3
lwz r4, 0x20(r31)
add r3, r3, r4
lwz r4, 0x1c(r31)
add r3, r3, r4
lwz r4, 0x18(r31)
and r3, r3, r4
lwz r4, 0x14(r31)
add r3, r3, r4
lwz r31, 0x2c(r1)
addi r1, r1, 0x30
blr
subfic r1, r31, 0xa41
xoris r19, r19, 0x7765
'''
def ppc_execute(code):
code = code.split('\n')
regs = {
'r3': None,
'r4': None,
'r1': 0,
'r2': None,
'r31': None
}
mem = {}
for insn in code:
insn = insn.replace(',', ' ').split()
if not insn:
continue
op = insn[0]
args = insn[1:]
if op == 'li':
regs[args[0]] = int(args[1], 16)
elif op == 'lis':
regs[args[0]] = int(args[1], 16)
elif op == 'ori':
regs[args[0]] = regs[args[1]] | int(args[2], 16)
elif op == 'xori':
regs[args[0]] = regs[args[1]] ^ int(args[2], 16)
elif op == 'andi':
regs[args[0]] = regs[args[1]] & int(args[2], 16)
elif op == 'and':
regs[args[0]] = regs[args[1]] & regs[args[2]]
elif op == 'or':
regs[args[0]] = regs[args[1]] | regs[args[2]]
elif op == 'xor':
regs[args[0]] = regs[args[1]] ^ regs[args[2]]
elif op == 'mr':
regs[args[0]] = regs[args[1]]
elif op == 'stw':
mem[args[1]] = regs[args[0]]
elif op == 'lwz':
regs[args[0]] = mem[args[1]]
elif op == 'addi':
regs[args[0]] = (regs[args[1]] + int(args[2], 16)) % 2**32
elif op == 'addis':
regs[args[0]] = (regs[args[1]] + int(args[2], 16)) % 2**32
elif op == 'add':
regs[args[0]] = (regs[args[2]] + regs[args[1]]) % 2**32
elif op == 'subf':
regs[args[0]] = (regs[args[2]] - regs[args[1]]) % 2**32
elif op == 'mullw':
regs[args[0]] = (regs[args[2]] * regs[args[1]]) % 2**32
elif op == 'mulli':
regs[args[0]] = (regs[args[2]] * int(regs[args[1]], 16)) % 2**32
elif op == 'rlwinm':
continue
elif op == 'subfic':
continue
elif op == 'stwu':
continue
elif op == 'xoris':
continue
elif op == 'blr':
continue
else:
print op
print insn
print regs
return regs['r3']
if __name__ == '__main__':
print hex(ppc_execute(code))