-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathassembler
executable file
·88 lines (73 loc) · 2.23 KB
/
assembler
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
#!/bin/bash
if [ -z $1 ]
then
echo >&2 "Error: specify program file"
exit 1
fi
DIRNAME=$( dirname $1 )
FILENAME=$( basename $1 )
BASENAME="${FILENAME%.*}"
LABELFILE=$( mktemp )
TMPFILE=$DIRNAME/$BASENAME.TXT
BINFILE=$DIRNAME/$BASENAME.BIN
cat $1 | tr -s "[]" "@@" | sed -r -e "s/([a-zA-Z+-]+)\s+@([0-9]+)@/i\1 \2/g" |
awk -v lfile=$LABELFILE -v binfile=$BINFILE >$TMPFILE '
BEGIN {
addr=0;
ass["INBOX"]="00"; i["INBOX"]=1;
ass["OUTBOX"]="10"; i["OUTBOX"]=1;
ass["COPYFROM"]="20"; i["COPYFROM"]=2;
ass["COPYTO"]="30"; i["COPYTO"]=2;
ass["ADD"]="40"; i["ADD"]=2;
ass["SUB"]="50"; i["SUB"]=2;
ass["BUMP+"]="60"; i["BUMP+"]=2;
ass["BUMP-"]="70"; i["BUMP-"]=2;
ass["iCOPYFROM"]="28";i["iCOPYFROM"]=2;
ass["iCOPYTO"]="38"; i["iCOPYTO"]=2;
ass["iADD"]="48"; i["iADD"]=2;
ass["iSUB"]="58"; i["iSUB"]=2;
ass["iBUMP+"]="68"; i["iBUMP+"]=2;
ass["iBUMP-"]="78"; i["iBUMP-"]=2;
ass["JUMP"]="80"; i["JUMP"]=2;
ass["JUMPZ"]="90"; i["JUMPZ"]=2;
ass["JUMPN"]="A0"; i["JUMPN"]=2;
ass["SET"]="E0"; i["SET"]=2;
ass["HALT"]="F0"; i["HALT"]=1;
print "v2.0 raw" > binfile;
}
{
if( $0 ~ /:/ ) {
sub( ":", "", $0 );
labelAddr[$0]=addr;
printf("%s:\n",$0);
}
else {
if( $0 ~ /JUMP/ ) {
printf(" %02x: %s %-2s ; %s %s\n", addr, ass[$1], $2, $1, $2);
printf("%s ",ass[$1]) >> binfile;
if( i[$1] == 2) printf("%s ", $2) >> binfile;
} else {
if( i[$1] == 1) printf(" %02x: %s ; %s\n", addr, ass[$1], $1);
if( i[$1] == 2) printf(" %02x: %s %02x ; %s %s\n", addr, ass[$1], $2, $1, $2);
printf("%s ",ass[$1]) >> binfile;
if( i[$1] == 2) printf("%02x ", $2) >> binfile;
}
addr+=i[$1];
}
}
END {
for (label in labelAddr)
{ printf("%s %02x\n", label, labelAddr[label]) >> lfile } ;
print "" >> binfile
}
'
cat $LABELFILE | while read LABEL ADDR
do
sed -i -e "s/ $LABEL / $ADDR /g" $TMPFILE
sed -i -e "s/ $LABEL / $ADDR /g" $BINFILE
done
sed -i -r -e "s/; i([a-zA-Z+-]+)\s+([0-9]+)/; \1 \[\2\]/g" $TMPFILE
cat $TMPFILE
echo ---
cat $BINFILE
rm $LABELFILE #$TMPFILE $BINFILE