-
Notifications
You must be signed in to change notification settings - Fork 55
/
utils.sh
30 lines (27 loc) · 1.3 KB
/
utils.sh
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
assemble() {
asm=$1
filename=$2
asm=`sed "
s/|15/.byte 0x66,0x66,0x66,0x66,0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00;/g
s/|14/.byte 0x66,0x66,0x66,0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00;/g
s/|13/.byte 0x66,0x66,0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00;/g
s/|12/.byte 0x66,0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00;/g
s/|11/.byte 0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00;/g
s/|10/.byte 0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00;/g
s/|9/.byte 0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00;/g
s/|8/.byte 0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00;/g
s/|7/.byte 0x0f,0x1f,0x80,0x00,0x00,0x00,0x00;/g
s/|6/.byte 0x66,0x0f,0x1f,0x44,0x00,0x00;/g
s/|5/.byte 0x0f,0x1f,0x44,0x00,0x00;/g
s/|4/.byte 0x0f,0x1f,0x40,0x00;/g
s/|3/.byte 0x0f,0x1f,0x00;/g
s/|2/.byte 0x66,0x90;/g
s/|1/nop;/g
" <<< "$asm"`
asm=`python3 -c 'import sys, re; print(re.sub(r"(\d*)\*\|(.*?)\|", lambda m: int(m.group(1))*(m.group(2)+";"), sys.argv[1]))' "$asm"`
echo ".intel_syntax noprefix" > asm-tmp.s
echo "$asm" >> asm-tmp.s
as asm-tmp.s -o asm-tmp.o || exit
objcopy -j .text -O binary asm-tmp.o "$filename"
rm asm-tmp.*
}