Last active
January 23, 2020 08:04
-
-
Save NishanthSpShetty/57205bee8edd7f4128af297a7b8efbef to your computer and use it in GitHub Desktop.
Stack-VM core arithemtic unit.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn do_primitive(&mut self) { | |
match self.current_data() & 0xCfffff { | |
0 => { | |
debug!("[ HALT ] : Stopping VM "); | |
self.set_running(false); | |
return; | |
} | |
1 => { | |
let top_1 = self.stack.pop(); | |
let top_2 = self.stack.pop(); | |
debug!("[ ADD ] : {} {} ", top_1, top_2); | |
self.stack.push(top_1 + top_2); | |
} | |
2 => { | |
let top_1 = self.stack.pop(); | |
let top_2 = self.stack.pop(); | |
debug!("[ SUB ] : {} {} ", top_1, top_2); | |
self.stack.push(top_1 - top_2); | |
} | |
3 => { | |
let top_1 = self.stack.pop(); | |
let top_2 = self.stack.pop(); | |
debug!("[ MULT ] : {} {} ", top_1, top_2); | |
self.stack.push(top_1 * top_2); | |
} | |
4 => { | |
let top_1 = self.stack.pop(); | |
let top_2 = self.stack.pop(); | |
debug!("[ DIV ] : {} {} ", top_1, top_2); | |
self.stack.push(top_1 / top_2); | |
} | |
_ => { | |
panic!("[ exec ] : Undefined instruction "); | |
} | |
} | |
debug!(" TOS now : {}", self.stack.peek()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment