[06] Assembler done and tested

Also started a clone assembler in rust
This commit is contained in:
Nemo 2020-05-30 02:11:31 +05:30
parent abbb826dd0
commit 09bf3f1e67
5 changed files with 46 additions and 10 deletions

View File

@ -86,6 +86,6 @@ Counting number of instructions by `wc -l $file.hack`
### Symbolic Programs
- [ ] `Max.asm`
- [ ] `Rect.asm`
- [ ] `Pong.asm`
- [x] `Max.asm`
- [x] `Rect.asm`
- [x] `Pong.asm`

1
assembler/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
assembler

View File

@ -33,15 +33,15 @@ class SymbolTable
end
def add(symb, address)
@st[symb] = address
@st[symb.to_sym] = address
end
def includes?
@st.has? symb
def has?(symb)
@st.has_key? symb.to_sym
end
def get(symb)
@st[symb]
@st[symb.to_sym]
end
end
@ -96,6 +96,7 @@ class Code
end
def self.comp(str)
str.strip!
a = c1 = c2 = c3 = c4 = c5 = c6 = 0
a = str.include? 'M'
key = str.gsub(/[AM]/, 'Y')
@ -187,10 +188,16 @@ class Parser
@ic = 0
@lines = []
File.readlines(fn).each do |line|
line.chomp!
line.strip!
next if line.empty?
next if line[0...2] == '//'
if line.include? '//'
comment_start = line.index('//')
line = line[0...comment_start]
line.strip!
end
@lines << line
end
end
@ -213,8 +220,9 @@ class Assembler
if command[0] == :L_COMMAND
symbol = command[1][0]
@st.add(symbol, ic)
else
ic+=1
end
ic+=1
end
# we rewind our parser
parser.reset
@ -229,7 +237,7 @@ class Assembler
val =@st.get(symbol)
else
val = memory_pointer
@st.add_var(symbol, memory_pointer)
@st.add(symbol, memory_pointer)
memory_pointer+=1
end
else

17
assembler/assembler.rs Normal file
View File

@ -0,0 +1,17 @@
use std::fs::File;
use std::env;
use std::io::{self, BufRead};
use std::path::Path;
fn main() -> std::io::Result<()> {
let fileName = env::args().nth(1);
let fileLines = read_lines(fileName);
Ok(())
}
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where P: AsRef<Path>, {
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}

View File

@ -1,5 +1,15 @@
#!/bin/bash
# Symbol Less assembly programs
diff ../projects/06/add/Add.hack <(ruby assembler.rb ../projects/06/add/Add.asm)
diff ../projects/06/max/MaxL.hack <(ruby assembler.rb ../projects/06/max/MaxL.asm)
diff ../projects/06/rect/RectL.hack <(ruby assembler.rb ../projects/06/rect/RectL.asm)
diff ../projects/06/pong/PongL.hack <(ruby assembler.rb ../projects/06/pong/PongL.asm)
diff ../projects/06/max/Max.hack <(ruby assembler.rb ../projects/06/max/Max.asm)
diff ../projects/06/rect/Rect.hack <(ruby assembler.rb ../projects/06/rect/Rect.asm)
diff ../projects/06/pong/Pong.hack <(ruby assembler.rb ../projects/06/pong/Pong.asm)