-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimg2asm.py
172 lines (142 loc) · 3.79 KB
/
img2asm.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
import cv2
import os
import sys
import gen
import toml
INSTRUCTIONS = 26
WIDTH = 100
HEIGHT = 100
TIED = False
IMAGE = None
with open("config.toml", "rt") as fp:
config = toml.load(fp)
PLATFORM = config["Option"]["compiler"]
LARGEADDRESSAWARE_YES = config["Option"]["large_address_aware_yes"]
if PLATFORM == "msvc":
OUTPUT_FILENAME = "Native.asm"
else:
OUTPUT_FILENAME = "Native.s"
def image_load(filename):
global IMAGE
img = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
img = 255 - img
img = cv2.resize(img, dsize=(WIDTH, HEIGHT))
th, img = cv2.threshold(img, 128, 192, cv2.THRESH_OTSU)
IMAGE = img
def p(code):
return " " + code + "\n"
def widen():
if PLATFORM == "msvc":
return p("vfmaddsub132ps xmm0,xmm1,[rdi+rsi*4+1111111111111111h]")
else:
return p("vfmaddsub132ps xmm0,xmm1,[rdi+rsi*4+0x11111111]")
def init_widen():
code = ""
code += p("xor rdi, rdi")
code += p("xor rsi, rsi")
return code
def s():
code = "s:\n"
for i in range(WIDTH + 1):
if PLATFORM == "msvc":
code += p(f"dq e_0_{i}")
else:
code += p(f".quad e_0_{i}")
return code
def check(x, y):
return IMAGE[y, x]
def block_fill(r, c):
asmcode = ""
pixel = 0
if c == 0:
pixel = 255
else:
T = c - 1
pixel = check(T, r)
asmcode += widen()
lines = int(pixel * INSTRUCTIONS / 255)
for i in range(lines):
asmcode += p(gen.rand_insn())
return asmcode
def diag(row, column, width, height, done):
asmcode = ""
r = row
c = column
for i in range(256):
nr = r + 1
if TIED:
nc = c + 1
else:
nc = c
if TIED:
if nc >= width:
asmcode += widen()
asmcode += p(f"jmp e_{r}_{c}")
asmcode += f"e_{r}_{c}:\n"
if nr >= height:
asmcode += block_fill(r, c)
elif TIED and (nc >= width):
asmcode += block_fill(r, c)
asmcode += p(f"je e_{nr}_{c}")
else:
if c == 0:
asmcode += block_fill(r, c)
asmcode += p(f"jmp e_{nr}_{nc}")
if TIED:
break
else:
asmcode += block_fill(r, c)
if TIED:
asmcode += p(f"je e_{nr}_{nc}")
else:
asmcode += p(f"jmp e_{nr}_{nc}")
r = r + 1
if TIED:
c = c - 1
if r >= height:
asmcode += p(f"jmp {done}")
break
return asmcode
def main(filename):
image_load(filename)
asmcode = ""
if PLATFORM == "gnu":
asmcode += ".intel_syntax noprefix\n"
if PLATFORM == "msvc":
asmcode += ".code\n"
asmcode += "asmcode proc EXPORT\n"
else:
asmcode += ".global asmcode\n"
asmcode += "asmcode:\n"
asmcode += init_widen()
asmcode += p("nop")
if LARGEADDRESSAWARE_YES:
for i in range(int(WIDTH / 2) + 1):
asmcode += p(f"je e_0_{i}")
asmcode += p(f"jne e_0_{WIDTH - i}")
else:
asmcode += p("mov eax, 0")
asmcode += p("jmp [s + eax *8]")
CC = 0
for i in range(WIDTH + 1):
asmcode += diag(0, CC, WIDTH + 1, HEIGHT, "done")
CC += 1
if TIED:
RC = 1
for i in range(HEIGHT - 1):
asmcode += diag(RC, WIDTH, WIDTH + 1, HEIGHT, "done")
RC += 1
asmcode += "done:\n"
asmcode += p("ret")
if not LARGEADDRESSAWARE_YES:
asmcode += s()
if PLATFORM == "msvc":
asmcode += "asmcode endp\n"
asmcode += "end"
else:
pass
with open(OUTPUT_FILENAME, mode="w") as f:
f.write(asmcode)
if __name__ == "__main__":
args = sys.argv
main(args[1])