Repository navigation
Expand file tree
/
Copy pathcalculator.rs
More file actions
86 lines (68 loc) · 2.18 KB
/
Copy pathcalculator.rs
File metadata and controls
86 lines (68 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use std::process::Command;
use std::fs;
use std::io::{self};
use std::path::Path;
fn main() -> io::Result<()> {
let asm_file = "calculator.asm";
let obj_file = "calculator.o";
let exe_file = "calculator";
// Check if assembly file exists
if !Path::new(asm_file).exists() {
eprintln!("Error: {} not found!", asm_file);
eprintln!("Please ensure the assembly file is in the same directory.");
return Ok(());
}
println!("Compiling assembly calculator...");
let nasm_output = Command::new("nasm")
.args(&["-f", "elf64", asm_file, "-o", obj_file])
.output();
match nasm_output {
Ok(output) => {
if !output.status.success() {
eprintln!("NASM assembly failed:");
eprintln!("{}", String::from_utf8_lossy(&output.stderr));
return Ok(());
}
println!("Assembly successful!");
}
Err(e) => {
eprintln!("Error running nasm: {}", e);
eprintln!("Make sure nasm is installed (sudo apt install nasm)");
return Ok(());
}
}
// Link with ld
let ld_output = Command::new("ld")
.args(&[obj_file, "-o", exe_file])
.output();
match ld_output {
Ok(output) => {
if !output.status.success() {
eprintln!("Linking failed:");
eprintln!("{}", String::from_utf8_lossy(&output.stderr));
return Ok(());
}
println!("Linking successful!");
}
Err(e) => {
eprintln!("Error running ld: {}", e);
return Ok(());
}
}
println!("Running Assembly Calculator");
// Run asm calculator
let run_output = Command::new(format!("./{}", exe_file))
.spawn();
match run_output {
Ok(mut child) => {
let status = child.wait()?;
println!("Calculator exited with status: {}", status);
}
Err(e) => {
eprintln!("Error running calculator: {}", e);
}
}
// Cleanup object file
let _ = fs::remove_file(obj_file);
Ok(())
}