aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/add.rs
diff options
context:
space:
mode:
authorJohannes Stoelp <johannes.stoelp@gmail.com>2023-03-05 22:20:48 +0100
committerJohannes Stoelp <johannes.stoelp@gmail.com>2023-03-05 22:20:48 +0100
commit8316b628bbc9945fd1d08305317cf49a6482801f (patch)
treeffc730d9ad9a131a2dbe54d41a4ab026e06f1ff3 /examples/add.rs
parent005fca316085c3a2ce3e43b92531f006a15fbdd2 (diff)
downloadjuicebox-asm-8316b628bbc9945fd1d08305317cf49a6482801f.tar.gz
juicebox-asm-8316b628bbc9945fd1d08305317cf49a6482801f.zip
Added CALL and call extern fn example
Diffstat (limited to 'examples/add.rs')
-rw-r--r--examples/add.rs30
1 files changed, 30 insertions, 0 deletions
diff --git a/examples/add.rs b/examples/add.rs
new file mode 100644
index 0000000..ef010b6
--- /dev/null
+++ b/examples/add.rs
@@ -0,0 +1,30 @@
+use juicebox_asm::prelude::*;
+use juicebox_asm::Runtime;
+use Reg64::*;
+
+extern "C" fn add(a: u32, b: u32) -> u32 {
+ a + b
+}
+
+fn main() {
+ let mut asm = Asm::new();
+
+ // SystemV abi:
+ // rdi -> first argument
+ // rsi -> second argument
+ // rax -> return value
+ //
+ asm.mov(rsi, Imm64::from(42));
+ asm.mov(rax, Imm64::from(add as u64));
+ asm.call(rax);
+ asm.ret();
+
+ let code = asm.into_code();
+ std::fs::write("jit.asm", &code).unwrap();
+
+ let rt = Runtime::new(&code);
+ let add42 = unsafe { rt.as_fn::<extern "C" fn(u32) -> u32>() };
+
+ let res = add42(5);
+ assert_eq!(res, 47);
+}