Skip to content

Instantly share code, notes, and snippets.

@NishanthSpShetty
Last active January 23, 2020 08:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NishanthSpShetty/57205bee8edd7f4128af297a7b8efbef to your computer and use it in GitHub Desktop.
Save NishanthSpShetty/57205bee8edd7f4128af297a7b8efbef to your computer and use it in GitHub Desktop.
Stack-VM core arithemtic unit.
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