86 lines
2.8 KiB
Rust
86 lines
2.8 KiB
Rust
use std::fs;
|
|
|
|
use assembler::{lexer, parser::Parser};
|
|
use common::prelude::{ITypeArgs, Instruction, RTypeArgs, Register};
|
|
|
|
fn main() {
|
|
// let program = fs::read_to_string("../resources/dsa/print.dsa").unwrap();
|
|
// let tokens = lexer::lexer(program).unwrap();
|
|
|
|
// println!("{:?}", tokens);
|
|
|
|
// let parser = Parser::new(tokens);
|
|
|
|
// for node in parser {
|
|
// println!("{:?}", node);
|
|
// }
|
|
|
|
// generate some instructions
|
|
let ins = vec![
|
|
// Initialize first two Fibonacci numbers
|
|
// F(0) = 0, F(1) = 1
|
|
|
|
// Load 0 into Rg0 (F(0))
|
|
Instruction::LoadLowerImmediate(ITypeArgs::new(0, None, Some(Register::Rg0))),
|
|
// Load 1 into Rg1 (F(1))
|
|
Instruction::LoadLowerImmediate(ITypeArgs::new(1, None, Some(Register::Rg1))),
|
|
// Load loop counter (how many more numbers to calculate)
|
|
// Let's calculate 8 Fibonacci numbers total (0,1,1,2,3,5,8,13)
|
|
Instruction::LoadLowerImmediate(ITypeArgs::new(6, None, Some(Register::Rg2))), // 6 more iterations
|
|
// Load 0 for comparison
|
|
Instruction::LoadLowerImmediate(ITypeArgs::new(0, None, Some(Register::Zero))),
|
|
// Fibonacci calculation loop starts here (address 4)
|
|
// Calculate next Fibonacci number: F(n) = F(n-1) + F(n-2)
|
|
Instruction::Add(RTypeArgs::new(
|
|
Some(Register::Rg0), // F(n-2)
|
|
Some(Register::Rg1), // F(n-1)
|
|
Some(Register::Rg3), // F(n) result
|
|
None,
|
|
)),
|
|
// Shift the sequence: Rg0 = Rg1, Rg1 = Rg3
|
|
Instruction::Mov(RTypeArgs::new(
|
|
Some(Register::Rg1),
|
|
Some(Register::NoReg),
|
|
Some(Register::Rg0),
|
|
None,
|
|
)),
|
|
Instruction::Mov(RTypeArgs::new(
|
|
Some(Register::Rg3),
|
|
Some(Register::NoReg),
|
|
Some(Register::Rg1),
|
|
None,
|
|
)),
|
|
// Decrement counter
|
|
Instruction::Decrement(RTypeArgs::new(
|
|
Some(Register::Rg2),
|
|
Some(Register::NoReg),
|
|
Some(Register::Rg2),
|
|
None,
|
|
)),
|
|
// Compare counter with 0
|
|
Instruction::Compare(RTypeArgs::new(
|
|
Some(Register::Rg2),
|
|
Some(Register::Zero),
|
|
Some(Register::NoReg),
|
|
None,
|
|
)),
|
|
// Jump back to loop if counter > 0 (address 4)
|
|
// JGT jumps if greater than flag is set
|
|
Instruction::JumpGt(ITypeArgs::new(4, None, None)),
|
|
// Program complete - the final Fibonacci number is in Rg1
|
|
// Let's move it to the accumulator for easy access
|
|
Instruction::Mov(RTypeArgs::new(
|
|
Some(Register::Rg1),
|
|
Some(Register::NoReg),
|
|
Some(Register::Acc),
|
|
None,
|
|
)),
|
|
// Halt the processor
|
|
Instruction::Halt,
|
|
];
|
|
|
|
for i in ins.iter() {
|
|
println!("0x{:08x}", i.encode());
|
|
}
|
|
}
|