Advertisement
Guest User

Untitled

a guest
Jan 9th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.56 KB | None | 0 0
  1. //assemble rout.asm on mac with following command
  2. //nasm -f macho64 rout.asm && clang rout.o -Wl,-no_pie -o a.out
  3.  
  4. use std::fs::File;
  5. use std::io::Write;
  6.  
  7. fn savetofile(filename: &str, data: &str){
  8.     let mut f = File::create(filename).expect("Unable to create file");
  9.     f.write_all(data.as_bytes()).expect("Unable to write data");
  10. }
  11.  
  12. fn main() {
  13.     let header = "default rel
  14.  
  15. %macro fs 2
  16.    push rax
  17.    push rbx
  18.    push rcx
  19.    push rdx
  20.    push rdi
  21.    push rsi
  22.    push rbp
  23.    push %2
  24.    %1
  25.    pop rbp
  26.    pop rsi
  27.    pop rdi
  28.    pop rdx
  29.    pop rcx
  30.    pop rbx
  31.    pop rax
  32. %endmacro
  33.  
  34. %macro outchar 0
  35.    pop rsi
  36.    push rbp
  37.    mov rbp, rsp
  38.    mov rdi, formatch
  39.    xor rax, rax
  40.    call _printf
  41.  
  42.    mov rsp, rbp
  43.    pop rbp
  44. %endmacro
  45.  
  46. extern _printf, _exit
  47.  
  48. section .data
  49.    formatch db \"%c\", 0, 0
  50.    tape times 1000 dq 0
  51.    
  52. section .text
  53.    global _main
  54. _main:
  55.    push 0
  56.    mov rbx, tape
  57.    ";
  58.  
  59.     let mut output = "".to_string();
  60.     output.push_str(header);
  61.     let s = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.";    
  62.     let mut loopi = 0;
  63.     let mut loopstack = Vec::new();
  64.     for c in s.chars() {
  65.         match c {
  66.             '+' => {
  67.                 output.push_str("mov rax, qword [rbx]\n");
  68.                 output.push_str("add rax, 1\n");
  69.                 output.push_str("mov qword [rbx], rax\n");
  70.             },
  71.             '-' => {
  72.                 output.push_str("mov rax, qword [rbx]\n");
  73.                 output.push_str("sub rax, 1\n");
  74.                 output.push_str("mov qword [rbx], rax\n");
  75.             },
  76.             '>' => {
  77.                 output.push_str("add rbx, 8\n");
  78.             },
  79.             '<' => {
  80.                 output.push_str("sub rbx, 8\n");
  81.             },
  82.             '[' => {
  83.                 let name = format!("loop{}", loopi);
  84.                 output.push_str(&format!("{}:\n", name));
  85.                 loopstack.push(name);
  86.                 loopi += 1;
  87.             },
  88.             ']' => {
  89.                 output.push_str("cmp qword [rbx], 0\n");
  90.                 output.push_str(&format!("jne {}\n", loopstack.last().unwrap()));
  91.                 loopstack.pop();
  92.             }
  93.             '.' => {
  94.                 output.push_str("mov rcx, qword [rbx]\n");
  95.                 output.push_str("fs outchar, rcx\n");
  96.             }
  97.             _ => {},
  98.         }
  99.     }
  100.     output.push_str("mov rdi, 0\n");
  101.     output.push_str("call _exit\n");
  102.     savetofile("rout.asm", &output);
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement