lots of refactoring

This commit is contained in:
2026-06-11 00:15:09 -04:00
parent bc922a6086
commit ddf63ad817
9 changed files with 345 additions and 160 deletions
+24 -21
View File
@@ -5,11 +5,12 @@ use crate::{
use std::{fs::OpenOptions, io::Write, os::unix::fs::OpenOptionsExt, process::Command};
pub fn run() {
windows();
// linux();
if let Err(err) = linux() {
println!("{err:?}");
}
}
fn linux() {
fn linux() -> Result<(), CompilerMsg> {
let mut program = Program::<X86_64>::default();
let text = b"Hello world!\n";
let text_sym = program.ro_data("hello_en", text);
@@ -19,10 +20,10 @@ fn linux() {
"hello2",
[BInstr::Asm(Asm {
instrs: vec![
mov(ax, 1),
mov(di, 1),
mov(ax, 1)?,
mov(di, 1)?,
lea(rsi, text_sym2),
mov(dx, text2.len() as u64),
mov(dx, text2.len() as u64)?,
Instr::Syscall,
Instr::Ret,
],
@@ -32,15 +33,15 @@ fn linux() {
"main",
[BInstr::Asm(Asm {
instrs: vec![
mov(di, 39),
push(rdi),
mov(ax, 1),
mov(di, 1),
mov(di, 39)?,
push(rdi)?,
mov(ax, 1)?,
mov(di, 1)?,
lea(rsi, text_sym),
mov(dx, text.len() as u64),
mov(dx, text.len() as u64)?,
Instr::Syscall,
Instr::Call(hello2),
mov(ax, 0x3c),
mov(ax, 0x3c)?,
pop(rdi),
Instr::Syscall,
],
@@ -73,9 +74,10 @@ fn linux() {
if let Some(code) = status.code() {
std::process::exit(code);
}
Ok(())
}
fn windows() {
fn windows() -> Result<(), CompilerMsg> {
let mut program = Program::<X86_64>::default();
let [get_std_handle, write_file, exit_process] =
program.external("KERNEL32.dll", ["GetStdHandle", "WriteFile", "ExitProcess"]);
@@ -88,22 +90,22 @@ fn windows() {
instrs: vec![
Instr::Sub,
// stdout
mov(ecx, -11),
Instr::CallMem(get_std_handle),
mov(ecx, -11)?,
Instr::Callm(get_std_handle),
// write
mov(rcx, rax),
mov(rcx, rax)?,
lea(rdx, text_sym),
mov(r8d, text.len() as u64),
mov(r8d, text.len() as u64)?,
lea(r9, written),
Instr::MemMov {
Instr::Movm {
reg: rsp,
offset: 0x20,
val: 0,
},
Instr::CallMem(write_file),
Instr::Callm(write_file),
// exit
mov(ecx, 39),
Instr::CallMem(exit_process),
mov(ecx, 39)?,
Instr::Callm(exit_process),
],
})],
);
@@ -121,6 +123,7 @@ fn windows() {
if let Some(code) = status.code() {
std::process::exit(code);
}
Ok(())
}
fn write(path: &str, binary: &[u8]) {