diff options
-rw-r--r-- | .gitignore | 2 | ||||
-rwxr-xr-x | asmblr.py | 114 | ||||
-rw-r--r-- | problem1.asm | 18 | ||||
-rw-r--r-- | problem2.asm | 20 | ||||
-rw-r--r-- | problem3.asm | 27 | ||||
-rw-r--r-- | problem4.asm | 23 |
6 files changed, 204 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a60d365 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ + +*.un~ diff --git a/asmblr.py b/asmblr.py new file mode 100755 index 0000000..52431bf --- /dev/null +++ b/asmblr.py @@ -0,0 +1,114 @@ +#!/usr/bin/env python2 + +import sys + + +def tokenize(i): + if i[0] == ';': return None + x = i.strip().split(' ') + if len(x) == 3 and ',' in x[1]: + x[1] = x[1].split(',')[0] + return x + +def genaddresses(tokens): + offset = 31 + lblcnt = 0 + table = {} + for t in tokens: + if ':' not in t[0]: + continue + label = t[0].split(':')[0] + table[label] = offset + lblcnt + lblcnt += 1 + return table + +def gencode(tokens, addresses, ops): + out = [] + regs = {'R1': 0, 'R2': 1, 'R3': 3, 'R4': 4, 'R5' : 5, 'R6' : 6} + for t in tokens: + word = t[0] + if word == "halt": + out.append(opcodes[word]) + elif word in ["inc", "je", "jne", "jmp"]: + out.append(handle1arg(t, ops, regs, addresses)) + elif word in ["add", "sub", "xor", "cmp"]: + out.append(handle2arg(t, ops, regs)) + elif word == "mov": + out.append(handlemov(t, ops, regs)) + return out + +def handle1arg(l, ops, regs, ads): + op = ops[l[0]] << 12 + if l[0] == 'inc': + op += regs[l[1]] + else: + try: + op += int(l[1], 16) + except ValueError: + op += ads[l[1]] + return op + +def handle2arg(l, ops, regs): + op = ops[l[0]] << 12 + op += regs[l[1]] << 6 + op += regs[l[2]] + return op + +def handlemov(l, ops, regs): + if "[" in l[1]: + op = ops['cpt'] << 12 + op += regs[l[1][1:3]] << 6 + op += regs[l[2]] + elif "[" in l[2]: + op = ops['cpf'] << 12 + op += regs[l[1]] << 6 + op += regs[l[2][1:3]] + elif "R" in l[1] and "R" in l[2]: + op = ops['cpy'] << 12 + op += regs[l[1]] << 6 + op += regs[l[2]] + else: + op = ops['set'] << 12 + op += regs[l[1]] << 6 + op += int(l[2], 16) + return op + +def formatcode(code): + lines = [] + for i in code: + lines.append("{:0>4X}".format(i)) + return ''.join(lines) + +f = sys.argv[1] + +opcodes = { + 'halt': 0x0, + 'inc' : 0x1, + 'jmp' : 0x2, + 'je' : 0x3, + 'jne' : 0x4, + 'add' : 0x5, + 'sub' : 0x6, + 'xor' : 0x7, + 'cmp' : 0x8, + 'set' : 0x9, + 'cpy' : 0xA, + 'cpt' : 0xB, + 'cpf' : 0xC } + +prgm = open(f).readlines() +tokens = [] + +for i in prgm: + tokens.append(tokenize(i)) + +tokens = filter(None, tokens) + +addresses = genaddresses(tokens) + +code = gencode(tokens, addresses, opcodes) +print code + +binary = formatcode(code) + +print binary diff --git a/problem1.asm b/problem1.asm new file mode 100644 index 0000000..e35d134 --- /dev/null +++ b/problem1.asm @@ -0,0 +1,18 @@ +; (a-b)*c + mov R5, 1 + mov R1, [R5] + inc R5 + mov R2, [R5] + inc R5 + mov R3, [R5] + sub R1, R2 + mov R2, 1 + mov R1, R4 +loop: + inc R2 + add R1, R4 + cmp R2, R3 + jne loop + mov R2, 0x1E + mov [R2], R1 + halt diff --git a/problem2.asm b/problem2.asm new file mode 100644 index 0000000..315ec4d --- /dev/null +++ b/problem2.asm @@ -0,0 +1,20 @@ + +; first 10 fibonacci in 200-209 + + mov R5, 0x32 + mov R5, R4 + add R5, R4 + add R5, R4 + add R5, R4 + mov R0, 0x1 + mov R1, 0x1 + mov R3, 0xA + mov R4, 1 +loop: + inc R4 + mov R3, R2 + add R2, R1 + mov R3, R1 + cmp R4, R3 + jne loop + halt diff --git a/problem3.asm b/problem3.asm new file mode 100644 index 0000000..6786770 --- /dev/null +++ b/problem3.asm @@ -0,0 +1,27 @@ + +; copy 101-110 in reverse order in 51-60. add 51, 53, and 60. store in 30 + + mov R0, 0x32 + mov R1, 0x32 + add R1, R0 + mov R2, 0xA + mov R4, R1 + add R1, R2 + mov R2, 0x1 +loop: + inc R0 + mov R3, [R1] + mov [R0], R3 + sub R1, R2 + cmp R2, R4 + jne loop + mov R1, [R0] + mov R0, 0x35 + mov R2, [R0] + add R1, R2 + mov R0, 0x33 + mov R2, [R0] + add R1, R2 + mov R0, 0x1E + mov [R0], R1 + halt diff --git a/problem4.asm b/problem4.asm new file mode 100644 index 0000000..50dd510 --- /dev/null +++ b/problem4.asm @@ -0,0 +1,23 @@ + +; n^m n at 1, m at 2. both unsigned + + mov R0, 0x1 + mov R1, 0x2 + mov R0, [R0] + mov R1, [R1] + mov R4, R0 + mov R3, 1 +outer: + mov R2, 1 + inc R3 +inner: + inc R2 + add R0, R4 + cmp R2, R4 + jne inner + cmp R3, R1 + jne outer + mov R2, 0x1E + mov [R2], R0 + halt + |