x86_64 call & ret

This commit is contained in:
2026-06-06 23:26:17 -04:00
parent 69cd249671
commit 66710370bf
5 changed files with 26 additions and 1 deletions
+2
View File
@@ -8,6 +8,8 @@ pub struct Asm {
pub enum Instr {
Mov { dst: RegMode, src: RegImm },
Int { code: u8 },
Call(Symbol),
Ret,
Syscall,
Lea { dst: RegMode, sym: Symbol },
}
+11
View File
@@ -106,6 +106,15 @@ impl Encoder {
self.data.extend([0x0f, 0x05])
}
pub fn call(&mut self, sym: Symbol) {
self.data.push(0xe8);
self.sym_offset4(sym);
}
pub fn ret(&mut self) {
self.data.push(0xc3);
}
/// inserts a 32 bit offset from a symbol
pub fn sym_offset4(&mut self, sym: Symbol) {
let Some(addr) = self.sym_tab.get(sym) else {
@@ -123,6 +132,8 @@ impl Encoder {
Instr::Int { code } => self.int(code),
Instr::Syscall => self.syscall(),
Instr::Lea { dst, sym } => self.lea(dst, sym),
Instr::Call(sym) => self.call(sym),
Instr::Ret => self.ret(),
}
Ok(())
}
+13
View File
@@ -8,6 +8,18 @@ pub fn run() {
let mut program = Program::<X86_64>::default();
let text = b"Hello world!\n";
let text_sym = program.ro_data(text);
let text2 = "世界、こんにちは!\n";
let text_sym2 = program.ro_data(text2);
let hello2 = program.func([BInstr::Asm(Asm {
instrs: vec![
mov(ax, 1),
mov(di, 1),
lea(rsi, text_sym2),
mov(dx, text2.len() as u64),
Instr::Syscall,
Instr::Ret,
],
})]);
let entry = program.func([BInstr::Asm(Asm {
instrs: vec![
mov(ax, 1),
@@ -15,6 +27,7 @@ pub fn run() {
lea(rsi, text_sym),
mov(dx, text.len() as u64),
Instr::Syscall,
Instr::Call(hello2),
mov(ax, 0x3c),
mov(di, 39),
Instr::Syscall,
-1
View File
@@ -27,7 +27,6 @@ pub enum Instr<A: Arch> {
Set { dst: VarId, src: Vec<u8> },
Call { dst: FnId, args: Vec<VarId> },
Copy { dst: VarId, src: VarId },
Free(VarId),
Asm(A::Asm),
}
BIN
View File
Binary file not shown.