-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasm-transfer.py
712 lines (644 loc) · 36 KB
/
asm-transfer.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
import re
import os
import binascii
REG = r"(?:[xw](?:\d+|zr)|sp)"
WHITE = r"[ \t]*"
IMM = r"(?:#?(?:0x)?[0-9a-fA-F]+)"
# ext:"lsl"等
# amount "3" 等
EXTEND = {"uxtw": 0b010, "lsl": 0b011, "sxtw": 0b110, "sxtx": 0b111}
AMOUNT = {0: 0, 2: 1, 3: 1}
def encode_shift(ext:str, amount:str):
if (not ext or not amount): return 0
return (EXTEND[ext] << 13) | (AMOUNT[int(amount)] << 12)
# rm: "x3"、"w4"等
REGENC = {"xzr": 31, "wzr": 31, "sp": 31, 'w0': 0, 'x0': 0, 'w1': 1, 'x1': 1, 'w2': 2, 'x2': 2, 'w3': 3, 'x3': 3, 'w4': 4, 'x4': 4, 'w5': 5, 'x5': 5, 'w6': 6, 'x6': 6, 'w7': 7, 'x7': 7, 'w8': 8, 'x8': 8, 'w9': 9, 'x9': 9, 'w10': 10, 'x10': 10, 'w11': 11, 'x11': 11, 'w12': 12, 'x12': 12, 'w13': 13, 'x13': 13, 'w14': 14, 'x14': 14, 'w15': 15, 'x15': 15, 'w16': 16, 'x16': 16, 'w17': 17, 'x17': 17, 'w18': 18, 'x18': 18, 'w19': 19, 'x19': 19, 'w20': 20, 'x20': 20, 'w21': 21, 'x21': 21, 'w22': 22, 'x22': 22, 'w23': 23, 'x23': 23, 'w24': 24, 'x24': 24, 'w25': 25, 'x25': 25, 'w26': 26, 'x26': 26, 'w27': 27, 'x27': 27, 'w28': 28, 'x28': 28, 'w29': 29, 'x29': 29, 'w30': 30, 'x30': 30, 'w31': 31, 'x31': 31}
def encode_regs(rm:str, rn:str, rt:str):
if not rm: rm = "xzr"
rm = REGENC[rm] << 16
rn = REGENC[rn] << 5
rt = REGENC[rt]
return rm | rn | rt
def encode_reg_imms(imm:int, rn:str, rt:str):
assert imm & 1 == 0, "imm in padd1 must be even"
imm = (imm >> 1) << 10
rn = REGENC[rn] << 5
rt = REGENC[rt]
return imm | rn | rt
malu12regpat = re.compile(f'(p(?:add|sub|or|xor|and)[12]){WHITE}({REG}){WHITE},{WHITE}\[{WHITE}({REG}){WHITE}(?:,{WHITE}({REG}))?{WHITE}(?:,{WHITE}(uxtw|lsl|sxtw|sxtx){WHITE}#?(0|2|3))?{WHITE}\]')
malu1immpat = re.compile(f'(p(?:add|sub|or|xor|and)1){WHITE}({REG}){WHITE},{WHITE}\[{WHITE}({REG}){WHITE},{WHITE}({IMM}){WHITE}\]')
# movpat = re.compile(f'mov{WHITE}({REG}){WHITE},{WHITE}({IMM})')
malu12_reg_base = {
"padd1": 0x2000000,
"psub1": 0x2200000,
"pand1": 0x2400000,
"por1": 0x2600000,
"pxor1": 0x2800000,
"padd2": 0x3000000,
"psub2": 0x3200000,
"pand2": 0x3400000,
"por2": 0x3600000,
"pxor2": 0x3800000,
}
malu1_imm_base = {
"padd1": 0x22000000,
"psub1": 0x22200000,
"pand1": 0x22400000,
"por1": 0x22600000,
"pxor1": 0x22800000,
}
plats_regpat = re.compile(f'plats{WHITE}({REG}){WHITE},{WHITE}({REG}){WHITE},{WHITE}({REG}){WHITE}')
plats_reg_base = 0xe2200000
pll_regpat = re.compile(f'pll{WHITE}({REG}){WHITE},{WHITE}({REG}){WHITE},{WHITE}({REG}){WHITE}')
pll_reg_base = 0x8c000000
# 要使用此函数,先安装llvm,apt install llvm
def llvm_asm(row):
import subprocess
try:
# 调用 llvm-mc 来获取机器码
completed_process = subprocess.run(
["llvm-mc", "-triple=aarch64-none-elf", "-assemble", "-show-encoding"],
input=row,
text=True,
capture_output=True
)
# 提取输出中的机器码部分
output = completed_process.stdout
encoded_bytes = output.split("encoding: [")[1].split("]")[0].split(",")
# 将字节列表转换为单个16进制数
encoded_hex = "".join(reversed([e[2:] for e in encoded_bytes]))
# 将16进制数转换为整数
machine_code = int(encoded_hex, 16)
return machine_code
except Exception as e:
return None
def assemble(row):
if (row.strip().startswith("padd") or row.strip().startswith("psub") or row.strip().startswith("pand") or row.strip().startswith("por") or row.strip().startswith("pxor")):
reglist = re.findall(malu12regpat, row)
if reglist:
inst = malu12_reg_base[reglist[0][0]]
rm = reglist[0][3]
rn = reglist[0][2]
rt = reglist[0][1]
extend = reglist[0][4]
amount = reglist[0][5]
size = (1 if rt[0] == 'x' else 0) << 30
inst |= encode_regs(rm, rn, rt)
inst |= size
inst |= encode_shift(extend, amount)
return inst
else:
reglist = re.findall(malu1immpat, row)
if reglist:
inst = malu1_imm_base[reglist[0][0]]
rn = reglist[0][2]
rt = reglist[0][1]
imm = reglist[0][3]
if imm[0] == "#": imm = imm[1:]
imm = int(imm, 0)
size = (1 if rt[0] == 'x' else 0) << 30
inst |= encode_reg_imms(imm, rn, rt)
inst |= size
return inst
elif row.strip().startswith("plats"):
reglist = re.findall(plats_regpat, row)
inst = plats_reg_base
rt = reglist[0][2]
rn = reglist[0][1]
rm = reglist[0][0]
inst |= encode_regs(rm, rn, rt)
return inst
elif row.strip().startswith("pll"):
reglist = re.findall(pll_regpat, row)
inst = pll_reg_base
rt = reglist[0][2]
rn = reglist[0][1]
rm = reglist[0][0]
inst |= encode_regs(rm, rn, rt)
return inst
elif row.strip().startswith("mov") or row.strip().startswith("add"):
return llvm_asm(row)
elif row.strip().startswith("nop"):
return 0xd503201f
else:
return None
# 修改指令
# 修改指令
def modify_instructions_malu1(input_filename, output_filename):
with open(input_filename, 'r') as infile:
lines = infile.readlines()
output_lines = []
for i in range(0, len(lines), 3):
line=lines[i]
next_line=lines[i+1]
# for i, line in enumerate(lines):
parts = line.strip().split()
if i+1<len(lines):
second_line = lines[i+1]
if i+2<len(lines):
third_line=lines[i+2]
# print(parts)
second_part=second_line.strip().split()
#print("second_part:",second_part)
third_part = third_line.strip().split()
if 1:
if 1:
reg_all= " ".join(parts[-2:])
reg_all5=" ".join(parts[-1:])
reg_t1 = parts[3].split(',')[0]
reg_t2=second_part[3].split(',')[0]
reg_t=second_part[4].split(',')[0]
#print(reg_t1)
reg_delta = " ".join(second_part[-1:]).strip('[]').strip(',')
reg_addr2=" ".join(third_part[-1:]).strip('[]').strip(',')
reg_arg2="["+reg_addr2+","+reg_t1+"]"
reg_arg3="["+reg_t2+","+reg_t1+"]"
if re.search(r'\bldr\b', line):
if '#' in next_line and re.search(r'\badd\b', next_line) and len(parts)==5:
mov_instruction = "{} {} mov {},{}\n".format(parts[0], parts[1],reg_t1, reg_delta)
output_lines.append(mov_instruction)
# 修改add指令为padd1指令
padd1_instruction = "{} {} padd1 {},{}\n".format(second_part[0],second_part[1], reg_t1, reg_all5)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(nop_instruction)
#print(output_lines)
# check = 1
if '#' in next_line and re.search(r'\badd\b', next_line) and len(parts)==6:
mov_instruction = "{} {} mov {},{}\n".format(parts[0], parts[1],reg_t1, reg_delta)
output_lines.append(mov_instruction)
# 修改add指令为padd1指令
padd1_instruction = "{} {} padd1 {},{}\n".format(second_part[0],second_part[1], reg_t1, reg_all)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(nop_instruction)
#print(output_lines)
# check = 1
# 提取add指令的寄存器和立即数
elif '#' not in next_line and re.search(r'\badd\b', next_line) and len(parts)==5:
if 1:
#notimmediate=first_reg
padd1_instruction = "{} {} padd1 {},{}\n".format(parts[0], parts[1], reg_delta, reg_all5)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} nop\n".format(second_part[0], second_part[1])
output_lines.append(nop_instruction)
nop_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(nop_instruction)
elif '#' not in next_line and re.search(r'\badd\b', next_line) and len(parts)==6:
if 1:
#notimmediate=first_reg
padd1_instruction = "{} {} padd1 {},{}\n".format(parts[0], parts[1], reg_delta, reg_all)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} nop\n".format(second_part[0], second_part[1])
output_lines.append(nop_instruction)
nop_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(nop_instruction)
elif '#' not in next_line and re.search(r'\bsub\b', next_line) and len(parts)==5:
if 1:
#notimmediate=first_reg
padd1_instruction = "{} {} psub1 {},{}\n".format(parts[0], parts[1], reg_delta, reg_all5)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} nop\n".format(second_part[0], second_part[1])
output_lines.append(nop_instruction)
nop_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(nop_instruction)
elif '#' not in next_line and re.search(r'\bsub\b', next_line) and len(parts)==6:
if 1:
#notimmediate=first_reg
padd1_instruction = "{} {} psub1 {},{}\n".format(parts[0], parts[1], reg_delta, reg_all)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} nop\n".format(second_part[0], second_part[1])
output_lines.append(nop_instruction)
nop_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(nop_instruction)
elif '#' not in next_line and re.search(r'\band\b', next_line) and len(parts)==5:
if 1:
#notimmediate=first_reg
padd1_instruction = "{} {} pand1 {},{}\n".format(parts[0], parts[1], reg_delta, reg_all5)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} nop\n".format(second_part[0], second_part[1])
output_lines.append(nop_instruction)
nop_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(nop_instruction)
elif '#' not in next_line and re.search(r'\band\b', next_line) and len(parts)==6:
if 1:
#notimmediate=first_reg
padd1_instruction = "{} {} pand1 {},{}\n".format(parts[0], parts[1], reg_delta, reg_all)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} nop\n".format(second_part[0], second_part[1])
output_lines.append(nop_instruction)
nop_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(nop_instruction)
elif '#' not in next_line and re.search(r'\bor\b', next_line) and len(parts)==5:
if 1:
#notimmediate=first_reg
padd1_instruction = "{} {} por1 {},{}\n".format(parts[0], parts[1], reg_delta, reg_all5)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} nop\n".format(second_part[0], second_part[1])
output_lines.append(nop_instruction)
nop_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(nop_instruction)
elif '#' not in next_line and re.search(r'\bor\b', next_line) and len(parts)==6:
if 1:
#notimmediate=first_reg
padd1_instruction = "{} {} por1 {},{}\n".format(parts[0], parts[1], reg_delta, reg_all)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} nop\n".format(second_part[0], second_part[1])
output_lines.append(nop_instruction)
nop_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(nop_instruction)
elif '#' not in next_line and re.search(r'\bxor\b', next_line) and len(parts)==5:
if 1:
#notimmediate=first_reg
padd1_instruction = "{} {} pxor1 {},{}\n".format(parts[0], parts[1], reg_delta, reg_all5)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} nop\n".format(second_part[0], second_part[1])
output_lines.append(nop_instruction)
nop_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(nop_instruction)
elif '#' not in next_line and re.search(r'\bxor\b', next_line) and len(parts)==6:
if 1:
#notimmediate=first_reg
padd1_instruction = "{} {} pxor1 {},{}\n".format(parts[0], parts[1], reg_delta, reg_all)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} nop\n".format(second_part[0], second_part[1])
output_lines.append(nop_instruction)
nop_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(nop_instruction)
elif '#' in next_line and re.search(r'\bsub\b', next_line) and len(parts)==5:
mov_instruction = "{} {} mov {},{}\n".format(parts[0], parts[1],reg_t1, reg_delta)
output_lines.append(mov_instruction)
# 修改add指令为padd1指令
padd1_instruction = "{} {} psub1 {},{}\n".format(second_part[0],second_part[1], reg_t1, reg_all5)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(nop_instruction)
elif '#' in next_line and re.search(r'\bsub\b', next_line) and len(parts)==6:
mov_instruction = "{} {} mov {},{}\n".format(parts[0], parts[1],reg_t1, reg_delta)
output_lines.append(mov_instruction)
# 修改add指令为padd1指令
padd1_instruction = "{} {} psub1 {},{}\n".format(second_part[0],second_part[1], reg_t1, reg_all)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(nop_instruction)
elif '#' in next_line and re.search(r'\band\b', next_line) and len(parts)==5:
mov_instruction = "{} {} mov {},{}\n".format(parts[0], parts[1],reg_t1, reg_delta)
output_lines.append(mov_instruction)
# 修改add指令为padd1指令
padd1_instruction = "{} {} pand1 {},{}\n".format(second_part[0],second_part[1], reg_t1, reg_all5)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(nop_instruction)
elif '#' in next_line and re.search(r'\band\b', next_line) and len(parts)==6:
mov_instruction = "{} {} mov {},{}\n".format(parts[0], parts[1],reg_t1, reg_delta)
output_lines.append(mov_instruction)
# 修改add指令为padd1指令
padd1_instruction = "{} {} pand1 {},{}\n".format(second_part[0],second_part[1], reg_t1, reg_all)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(nop_instruction)
elif '#' in next_line and re.search(r'\bor\b', next_line) and len(parts)==5:
mov_instruction = "{} {} mov {},{}\n".format(parts[0], parts[1],reg_t1, reg_delta)
output_lines.append(mov_instruction)
# 修改add指令为padd1指令
padd1_instruction = "{} {} por1 {},{}\n".format(second_part[0],second_part[1], reg_t1, reg_all5)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(nop_instruction)
elif '#' in next_line and re.search(r'\bor\b', next_line) and len(parts)==6:
mov_instruction = "{} {} mov {},{}\n".format(parts[0], parts[1],reg_t1, reg_delta)
output_lines.append(mov_instruction)
# 修改add指令为padd1指令
padd1_instruction = "{} {} por1 {},{}\n".format(second_part[0],second_part[1], reg_t1, reg_all5)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(nop_instruction)
elif '#' in next_line and re.search(r'\bxor\b', next_line) and len(parts)==5:
mov_instruction = "{} {} mov {},{}\n".format(parts[0], parts[1],reg_t1, reg_delta)
output_lines.append(mov_instruction)
# 修改add指令为padd1指令
padd1_instruction = "{} {} pxor1 {},{}\n".format(second_part[0],second_part[1], reg_t1, reg_all5)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(nop_instruction)
elif '#' in next_line and re.search(r'\bxor\b', next_line) and len(parts)==6:
mov_instruction = "{} {} mov {},{}\n".format(parts[0], parts[1],reg_t1, reg_delta)
output_lines.append(mov_instruction)
# 修改add指令为padd1指令
padd1_instruction = "{} {} pxor1 {},{}\n".format(second_part[0],second_part[1], reg_t1, reg_all)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(nop_instruction)
with open(output_filename, 'w') as outfile:
outfile.writelines(output_lines)
def modify_instructions_malu2(input_filename, output_filename):
with open(input_filename, 'r') as infile:
lines = infile.readlines()
output_lines = []
for i in range(0, len(lines), 3):
line=lines[i]
next_line=lines[i+1]
# for i, line in enumerate(lines):
parts = line.strip().split()
if i+1<len(lines):
second_line = lines[i+1]
if i+2<len(lines):
third_line=lines[i+2]
# print(parts)
second_part=second_line.strip().split()
#print("second_part:",second_part)
third_part = third_line.strip().split()
if 1:
# 没有offset
if len(parts) == 5:
reg_addr1 = " ".join(parts[-1:]).strip('[]')
reg_addr2 = " ".join(third_part[-1:]).strip('[]')
reg_delta=" ".join(second_part[-1:]).strip('[]').strip(',')
reg_val="["+reg_addr2+","+reg_addr1+"]"
# reg_val = "["+reg_val1 + "," + reg_val2+"]"
# print(reg_val)
reg_t1 = parts[3].split(',')[0]
reg_t2=second_part[3].split(',')[0]
# 带有offset
elif len(parts) == 6:
reg_all= " ".join(parts[-2:]).strip('[]')
reg_all2=" ".join(third_part[-2:]).strip('[]')
reg_t1 = parts[3].split(',')[0]
reg_t2=second_part[3].split(',')[0]
reg_t=second_part[4].split(',')[0]
reg_delta = " ".join(second_part[-1:]).strip('[]').strip(',')
reg_addr1 = " ".join(third_part[-2:]).strip('[]')
reg_addr1 = reg_addr1.split(',')[0].strip()
#print(reg_addr1)
reg_addr2=" ".join(third_part[-1:]).strip('[]').strip(',')
reg_arg2="["+reg_addr1+","+reg_t1+"]"
reg_arg3="["+reg_t2+","+reg_t1+"]"
if re.search(r'\bldr\b', line):
if '#' in line and len(parts)==6 and len(third_part)==5 and re.search(r'\badd\b', next_line):
mov_instruction = "{} {} add {},{}\n".format(parts[0], parts[1],reg_t1, reg_all)
output_lines.append(mov_instruction)
# 修改add指令为padd1指令
padd1_instruction = "{} {} padd2 {},{}\n".format(second_part[0],second_part[1], reg_delta, reg_arg2)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(nop_instruction)
#print(output_lines)
# check = 1
# 提取add指令的寄存器和立即数
elif len(parts)==5 and re.search(r'\badd\b', next_line):
padd1_instruction = "{} {} padd2 {},{}\n".format(parts[0], parts[1], reg_t2, reg_val)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} nop\n".format(second_part[0], second_part[1])
output_lines.append(nop_instruction)
nop_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(nop_instruction)
elif len(parts)==5 and re.search(r'\bsub\b', next_line):
padd1_instruction = "{} {} psub2 {},{}\n".format(parts[0], parts[1], reg_t2, reg_val)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} nop\n".format(second_part[0], second_part[1])
output_lines.append(nop_instruction)
nop_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(nop_instruction)
elif len(parts)==5 and re.search(r'\band\b', next_line):
padd1_instruction = "{} {} pand2 {},{}\n".format(parts[0], parts[1], reg_t2, reg_val)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} nop\n".format(second_part[0], second_part[1])
output_lines.append(nop_instruction)
nop_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(nop_instruction)
elif len(parts)==5 and re.search(r'\bor\b', next_line):
padd1_instruction = "{} {} por2 {},{}\n".format(parts[0], parts[1], reg_t2, reg_val)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} nop\n".format(second_part[0], second_part[1])
output_lines.append(nop_instruction)
nop_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(nop_instruction)
elif len(parts)==5 and re.search(r'\bxor\b', next_line):
padd1_instruction = "{} {} pxor2 {},{}\n".format(parts[0], parts[1], reg_t2, reg_val)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} nop\n".format(second_part[0], second_part[1])
output_lines.append(nop_instruction)
nop_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(nop_instruction)
# 带偏移的处理
elif len(parts)==6 and '#' in line and len(third_part)==6 and re.search(r'\badd\b', next_line):
#print("success")
mov_instruction = "{} {} add {},{}\n".format(parts[0], parts[1],reg_t1, reg_all)
output_lines.append(mov_instruction)
mov_instruction = "{} {} padd2 {},{}\n".format(second_part[0], second_part[1],reg_t2, reg_arg2)
output_lines.append(mov_instruction)
mov_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(mov_instruction)
elif '#' in line and len(parts)==6 and len(third_part)==5 and re.search(r'\bsub\b', next_line):
mov_instruction = "{} {} add {},{}\n".format(parts[0], parts[1],reg_t1, reg_all)
output_lines.append(mov_instruction)
# 修改add指令为padd1指令
padd1_instruction = "{} {} psub2 {},{}\n".format(second_part[0],second_part[1], reg_t2, reg_arg2)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(nop_instruction)
elif '#' in line and len(parts)==6 and len(third_part)==5 and re.search(r'\band\b', next_line):
mov_instruction = "{} {} add {},{}\n".format(parts[0], parts[1],reg_t1, reg_all)
output_lines.append(mov_instruction)
# 修改add指令为padd1指令
padd1_instruction = "{} {} pand2 {},{}\n".format(second_part[0],second_part[1], reg_t2, reg_arg2)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(nop_instruction)
elif '#' in line and len(parts)==6 and len(third_part)==5 and re.search(r'\bor\b', next_line):
mov_instruction = "{} {} add {},{}\n".format(parts[0], parts[1],reg_t1, reg_all)
output_lines.append(mov_instruction)
# 修改add指令为padd1指令
padd1_instruction = "{} {} por2 {},{}\n".format(second_part[0],second_part[1], reg_t2, reg_arg2)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(nop_instruction)
elif '#' in line and len(parts)==6 and len(third_part)==5 and re.search(r'\bxor\b', next_line):
mov_instruction = "{} {} add {},{}\n".format(parts[0], parts[1],reg_t1, reg_all)
output_lines.append(mov_instruction)
# 修改add指令为padd1指令
padd1_instruction = "{} {} pxor2 {},{}\n".format(second_part[0],second_part[1], reg_t2, reg_arg2)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(nop_instruction)
with open(output_filename, 'w') as outfile:
outfile.writelines(output_lines)
# plats
def modify_instructions_plats(input_filename, output_filename):
with open(input_filename, 'r') as infile:
lines = infile.readlines()
output_lines = []
count = 3
for i in range(0, len(lines), 3):
line=lines[i]
# for i, line in enumerate(lines):
check = 0
parts = line.strip().split()
if i+1<len(lines):
second_line = lines[i+1]
if i+2<len(lines):
third_line=lines[i+2]
# print(parts)
second_part=second_line.strip().split()
#print("second_part:",second_part)
third_part = third_line.strip().split()
if 1:
# 没有offset
if len(parts) == 5:
reg_data = third_part[3].split(',')[0]
reg_addr1 = " ".join(parts[-1:]).strip('[]')
reg_addr2 = " ".join(third_part[-1:]).strip('[]')
reg_delta=" ".join(second_part[-1:]).strip('[]').strip(',')
reg_val="["+reg_addr2+","+reg_addr1+"]"
# reg_val = "["+reg_val1 + "," + reg_val2+"]"
# print(reg_val)
# 带有offset
elif len(parts) == 6:
reg_data=third_part[3].split(',')[0]
reg_all= " ".join(parts[-2:]).strip('[]')
reg_all2=" ".join(third_part[-1:]).strip('[]')
reg_t1 = parts[3].split(',')[0]
reg_t2=second_part[3].split(',')[0]
reg_t=second_part[4].split(',')[0]
reg_delta = " ".join(second_part[-1:]).strip('[]').strip(',')
reg_addr2=" ".join(third_part[-1:]).strip('[]').strip(',')
reg_arg2="["+reg_addr2+","+reg_t1+"]"
reg_arg3="["+reg_t2+","+reg_t1+"]"
if re.search(r'\bldr\b', line):
if '#' in line and len(parts)==6 and len(third_part)==6:
mov_instruction = "{} {} add {},{}\n".format(parts[0], parts[1],reg_t1, reg_all)
output_lines.append(mov_instruction)
# 修改add指令为padd1指令
padd1_instruction = "{} {} add {},{},{}\n".format(second_part[0],second_part[1], reg_t2, reg_delta, reg_addr2)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} plats {},{}, {}\n".format(third_part[0], third_part[1], reg_t2, reg_t1, reg_data)
output_lines.append(nop_instruction)
#print(output_lines)
# check = 1
# 提取add指令的寄存器和立即数
elif len(parts)==5:
if 1:
padd1_instruction = "{} {} plats {},{},{}\n".format(parts[0], parts[1], reg_delta, reg_addr1, reg_data)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} nop\n".format(second_part[0], second_part[1])
output_lines.append(nop_instruction)
nop_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(nop_instruction)
with open(output_filename, 'w') as outfile:
outfile.writelines(output_lines)
# pll
def modify_instructions_pll(input_filename, output_filename):
with open(input_filename, 'r') as infile:
lines = infile.readlines()
output_lines = []
for i in range(0, len(lines), 3):
line=lines[i]
# for i, line in enumerate(lines):
parts = line.strip().split()
if i+1<len(lines):
second_line = lines[i+1]
if i+2<len(lines):
third_line=lines[i+2]
# print(parts)
second_part=second_line.strip().split()
#print("second_part:",second_part)
third_part = third_line.strip().split()
if 1:
# 没有offset
if len(parts) == 5:
reg_data = third_part[3].split(',')[0]
#print(reg_data)
reg_addr1 = " ".join(parts[-1:]).strip('[]')
reg_addr2 = " ".join(third_part[-1:]).strip('[]')
reg_delta=" ".join(second_part[-1:]).strip('[]').strip(',')
reg_val="["+reg_addr2+","+reg_addr1+"]"
# reg_val = "["+reg_val1 + "," + reg_val2+"]"
# print(reg_val)
# 带有offset
elif len(parts) == 6:
reg_data=third_part[3].split(',')[0]
reg_all= " ".join(parts[-2:]).strip('[]')
reg_all2=" ".join(third_part[-1:]).strip('[]')
reg_t1 = parts[3].split(',')[0]
reg_t2=second_part[3].split(',')[0]
reg_t=second_part[4].split(',')[0]
reg_delta = " ".join(second_part[-1:]).strip('[]').strip(',')
reg_addr2=" ".join(third_part[-1:]).strip('[]').strip(',')
reg_arg2="["+reg_addr2+","+reg_t1+"]"
reg_arg3="["+reg_t2+","+reg_t1+"]"
if re.search(r'\bldr\b', line):
if '#' in line and len(parts)==6 and len(third_part)==6:
mov_instruction = "{} {} add {},{}\n".format(parts[0], parts[1],reg_t1, reg_all)
output_lines.append(mov_instruction)
# 修改add指令为padd1指令
padd1_instruction = "{} {} add {},{},{}\n".format(second_part[0],second_part[1], reg_data, reg_delta,reg_addr2)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} pll {},{}, {}\n".format(third_part[0], third_part[1], reg_data, reg_t1, reg_data)
output_lines.append(nop_instruction)
#print(output_lines)
# check = 1
# 提取add指令的寄存器和立即数
elif len(parts)==5:
if 1:
# print("in")
#notimmediate=first_reg
padd1_instruction = "{} {} pll {},{},{}\n".format(parts[0], parts[1], reg_data, reg_addr1, reg_delta)
output_lines.append(padd1_instruction)
nop_instruction = "{} {} nop\n".format(second_part[0], second_part[1])
output_lines.append(nop_instruction)
nop_instruction = "{} {} nop\n".format(third_part[0], third_part[1])
output_lines.append(nop_instruction)
with open(output_filename, 'w') as outfile:
outfile.writelines(output_lines)
def new_instructions(input_filename):
with open(input_filename, 'r') as file:
# 逐行读取文件内容
result=[]
for line in file:
# 使用split()函数分割行,以空格为分隔符,默认分割符
parts = line.strip()
# print(parts)
instruction_parts = parts[16:]
#print("222",instruction_parts)
inst = assemble(instruction_parts)
#print("333",inst)
if (inst):
hex_inst= "%.8x" % inst
# print(hex_inst)
hex_inst_str = str(hex_inst)
little_inst=binascii.unhexlify(hex_inst_str)
big_inst=little_inst[::-1]
big_inst=binascii.hexlify(big_inst).decode('utf-8')
#print(big_inst)
# value = struct.unpack('<I', hex_inst_str)[0]
# change_value=struct.pack('>I', value)
# print(change_value)
# print(parts[7:15])
parts_list = list(parts)
parts_list[7:15] = list(big_inst)
new_parts=''.join(parts_list)
result.append(new_parts+'\n')
#print(result)
with open('res.txt', 'w') as newfile:
newfile.writelines(result)
def process_file(input_file, output_file, modify_function):
if os.path.getsize(input_file) > 0:
modify_function(input_file, output_file)
with open(output_file, 'r') as file:
return file.read()
return ""
if __name__ == "__main__":
# 修改指令
content1 = content2 = content3 = content4 = "" # 初始化内容变量为空字符串
content1 = process_file('raw_malu1.txt', 'res_malu1.txt', modify_instructions_malu1)
content2 = process_file('raw_malu2.txt', 'res_malu2.txt', modify_instructions_malu2)
content3 = process_file('raw_plats.txt', 'res_malu2.txt', modify_instructions_plats)
content4 = process_file('raw_pll.txt', 'res_malu2.txt', modify_instructions_pll)
res_file = content1 + content2 + content3 + content4
# 合并内容到新文件
with open('res.txt', 'w') as merged_file:
merged_file.write(res_file)
# merged_file.write(content3)
# 修改PC值
new_instructions('res.txt')
# new_instructions('res_padd2.txt')