aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJohannes Stoelp <johannes.stoelp@gmail.com>2023-03-05 22:20:17 +0100
committerJohannes Stoelp <johannes.stoelp@gmail.com>2023-03-05 22:20:17 +0100
commit005fca316085c3a2ce3e43b92531f006a15fbdd2 (patch)
tree2e252a7a5f54baf7ab6613e6dc12af2557cef0e2
parenta3321c416757d264957f4581bcb1bb164a9d696b (diff)
downloadjuicebox-asm-005fca316085c3a2ce3e43b92531f006a15fbdd2.tar.gz
juicebox-asm-005fca316085c3a2ce3e43b92531f006a15fbdd2.zip
Add jmp test
-rw-r--r--src/insn.rs1
-rw-r--r--src/insn/nop.rs7
-rw-r--r--tests/jmp.rs59
3 files changed, 67 insertions, 0 deletions
diff --git a/src/insn.rs b/src/insn.rs
index db62e6c..2a6ca42 100644
--- a/src/insn.rs
+++ b/src/insn.rs
@@ -6,6 +6,7 @@ mod jmp;
mod jnz;
mod jz;
mod mov;
+mod nop;
mod ret;
mod test;
diff --git a/src/insn/nop.rs b/src/insn/nop.rs
new file mode 100644
index 0000000..82057db
--- /dev/null
+++ b/src/insn/nop.rs
@@ -0,0 +1,7 @@
+use crate::Asm;
+
+impl Asm {
+ pub fn nop(&mut self) {
+ self.emit(&[0x90]);
+ }
+}
diff --git a/tests/jmp.rs b/tests/jmp.rs
new file mode 100644
index 0000000..2524218
--- /dev/null
+++ b/tests/jmp.rs
@@ -0,0 +1,59 @@
+use juicebox_asm::prelude::*;
+
+#[test]
+#[should_panic]
+fn unbound_label() {
+ let _l = Label::new();
+}
+
+#[test]
+#[should_panic]
+fn unbound_label2() {
+ let mut lbl = Label::new();
+ let mut asm = Asm::new();
+ asm.jmp(&mut lbl);
+}
+
+#[test]
+fn jmp_label() {
+ {
+ // Bind first.
+ let mut lbl = Label::new();
+ let mut asm = Asm::new();
+ asm.bind(&mut lbl);
+ asm.jmp(&mut lbl);
+ // 0xfffffffb -> -5
+ assert_eq!(asm.into_code(), [0xe9, 0xfb, 0xff, 0xff, 0xff]);
+ }
+ {
+ // Bind later.
+ let mut lbl = Label::new();
+ let mut asm = Asm::new();
+ asm.jmp(&mut lbl);
+ asm.bind(&mut lbl);
+ assert_eq!(asm.into_code(), [0xe9, 0x00, 0x00, 0x00, 0x00]);
+ }
+}
+
+#[test]
+fn jmp_label2() {
+ {
+ let mut lbl = Label::new();
+ let mut asm = Asm::new();
+ asm.jmp(&mut lbl);
+ asm.nop();
+ asm.nop();
+ asm.bind(&mut lbl);
+ assert_eq!(asm.into_code(), [0xe9, 0x02, 0x00, 0x00, 0x00, 0x90, 0x90]);
+ }
+ {
+ let mut lbl = Label::new();
+ let mut asm = Asm::new();
+ asm.jmp(&mut lbl);
+ for _ in 0..0x1ff {
+ asm.nop();
+ }
+ asm.bind(&mut lbl);
+ assert_eq!(asm.into_code()[..5], [0xe9, 0xff, 0x01, 0x00, 0x00]);
+ }
+}