2023-06-08から1日間の記事一覧

アセンブラを作る / Hackアセンブラベーシック版

Chapter 6 テキストの手順に従ってベーシック版(変数もジャンプラベルもなく即値のみを前提とする)のアセンブラを作る。 from Parser import * from Code import * class Hack_Assember_Basic: def __init__(self, input_file_path): # 出力ファイル名は入力…

Hackアセンブラ / 非公式訳

p.112 Hackアセンブラ これが、Parser、Codeの両モジュールの各種メソッドを使ってアセンブルプロセス全体を司るメインプログラムです。ここで説明するベーシック版アセンブラは、ソースアセンブリコードにシンボル参照が一切含まれていないことを前提としま…

アセンブラを作る / Codeクラス完成

Chapter 6 class Code: def __init__(self): pass def dest(self, mnemonic): A = (1 << 2) D = (1 << 1) M = (1 << 0) adm = 0 if "A" in mnemonic: adm |= A if "D" in mnemonic: adm |= D if "M" in mnemonic: adm |= M return bin(adm)[2:].zfill(3) def…

アセンブラを作る / Parserクラス完成

要領が分かったのでParserクラスのメソッドを全部作る。 class Parser: def __init__(self, file_path): self.file = open(file_path, "r") self.file.seek(0, 2) self.EOF_POS = self.file.tell() self.file.seek(0) self.current_inst = "" def __readline…

アセンブラを作る / instruction_type()

class Parser: def __init__(self, file_path): self.file = open(file_path, "r") self.file.seek(0, 2) self.EOF_POS = self.file.tell() self.file.seek(0) self.current_line = "" def has_more_lines(self): return self.file.tell() != self.EOF_POS d…

アセンブラを作る / advance() / 1行読み込んで余計な部分を削ぎ落とす

pp.109ff. class Parser: def __init__(self, file_path): self.file = open(file_path, "r") self.file.seek(0, 2) self.EOF_POS = self.file.tell() self.file.seek(0) self.current_line = "" def has_more_lines(self): return self.file.tell() != self…

アセンブラを作る / has_more_lines() / 読み込むべき行がまだ残っているかどうかを確かめる

pp.109ff. ここからは未知の領域である。 テキストどおりParserクラスを設けてそこに各メソッドを作り込む。メソッドごとの担当もテキストどおりに切り分ける。まず、読み込むべき行が残っているかどうかを確認できるようにする。 class Parser: def __init_…