Skip to content

Commit f3bb05b

Browse files
committed
add some comments
1 parent dfad77a commit f3bb05b

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

sicas.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
import os
55
from sicxe import *
66

7+
# A class to indicate assembly error
78
class AssembleError(BaseException):
89
pass
910

11+
# class to store info of each source statements
1012
class Line:
1113
def __init__(self, assembly, lineno):
1214
self.src = assembly
@@ -23,9 +25,11 @@ def __str__(self):
2325
def __repr__(self):
2426
return str(self)
2527

28+
# split the source statement into several tokens
2629
def tokenize(self):
2730
return self.assembly.split()
2831

32+
# return a tuple for assembly listing
2933
def listing_tuple(self):
3034
locfmt = ""
3135
codefmt = ""
@@ -36,6 +40,7 @@ def listing_tuple(self):
3640
codefmt = codefmt % self.code
3741
return (self.lineno, locfmt, self.src.expandtabs(8), codefmt)
3842

43+
# class to store each program info
3944
class Program:
4045
def __init__(self, source):
4146
self.source = os.path.basename(source)
@@ -49,13 +54,15 @@ def __init__(self, source):
4954
self.symtab = PRELOAD_SYMTAB.copy()
5055
self.base = -1
5156

57+
# print error message indicating the line number and throw the error
5258
def error(self, msg, line = None):
5359
if line == None:
5460
line = self.current_line()
5561
print("\n%s:%s" % (self.source, str(line.lineno)) + " " + str(line))
5662
print("Error : " + msg + '\n')
5763
raise AssembleError
5864

65+
# assemble the program
5966
def assemble(self):
6067
for line in self.content:
6168
program.lineno += 1
@@ -75,6 +82,7 @@ def assemble(self):
7582
else:
7683
program.error("Except a directive, opcde or label.")
7784

85+
# write assembly listing to file
7886
def listing(self, filename):
7987
stmt_len = len(max(self.content, key=lambda stmt: len(stmt.src)).src) + 10
8088
fmt = "\n%%-8s%%-8s%%-%ds%%-10s" % stmt_len
@@ -83,9 +91,11 @@ def listing(self, filename):
8391
for line in self.content:
8492
f.write(fmt % line.listing_tuple())
8593

94+
# get current line
8695
def current_line(self):
8796
return self.content[self.lineno - 1]
8897

98+
# output object file
8999
def output(self, file_name):
90100
with open(file_name, "w") as f:
91101
f.write("H%-6s%06X%06X" % (self.name, self.start_addr, self.LOCCTR - self.start_addr))
@@ -145,7 +155,6 @@ def output(self, file_name):
145155
f.write("\nM%06X%02X" % (line.loc + 1, 5))
146156
f.write("\nE%06X" % self.start_exec)
147157

148-
149158
def handler_START(program, tokens):
150159
if "START" in tokens:
151160
# validate format
@@ -254,6 +263,7 @@ def handler_NOBASE(program, tokens):
254263
"NOBASE" : handler_NOBASE,
255264
}
256265

266+
# fill the instructions which referencing foward symbols
257267
def fill_forward(fwd_lst, addr, program):
258268
for line, ref, reftype in fwd_lst:
259269
if reftype == REF_OP:
@@ -285,8 +295,6 @@ def fill_forward(fwd_lst, addr, program):
285295
else:
286296
program.error("no enough length to hold the displacement, try format 4.", line)
287297

288-
289-
290298
def has_directives(program, tokens):
291299
for token in tokens:
292300
if token in DIRTAB:

0 commit comments

Comments
 (0)