x64asm

Crates.iox64asm
lib.rsx64asm
version0.2.0
sourcesrc
created_at2022-04-25 11:05:20.819263
updated_at2023-01-06 22:40:30.137488
descriptionLibrary to write x64 Assembly code from Rust, more properly
homepage
repositoryhttps://github.com/antoninhrlt/x64asm/
max_upload_size
id573791
size21,222
Anтo (antoninhrlt)

documentation

README

x64asm

Library to write x64 Assembly code from Rust, more properly. Designed for the nasm assembler

How to use

let instructions = vec![
    i!(/* <mnemonic>, [operands, ...] */),
    // other instructions
];

let code = instructions.to_assembly(/*separator (space or tab)*/);
// Writes to a file
let mut stream = File::create(&Path::new("output.asm")).unwrap();
write!(stream, "{}", code).unwrap();

Installation

In your "Cargo.toml" file :

[dependencies]
x64asm = "*"

Check the current version on crates.io

Example

let instructions = vec![
    i!(Global, oplabel!("_start")),

    i!(section!(Text)),
    i!(label!("_start")),
    i!(Mov, reg!(Rax), Op::Literal(1)),
    i!(Mov, reg!(Rdi), Op::Literal(1)),
    i!(Mov, reg!(Rsi), oplabel!("msg")),
    i!(Mov, reg!(Rdx), oplabel!("msg_len")),
    i!(Syscall),

    i!(Mov, reg!(Rax), Op::Literal(60)),
    i!(Mov, reg!(Rdi), Op::Literal(0)),
    i!(Syscall),
    
    i!(section!(Data)),
    i!(label!("msg"), dd!(Db), opstring!("Hello world")),
    i!(label!("msg_len"), dd!(Equ), opexpr!("$ - msg")),
];

let code = instructions.to_assembly(Separator::Space);
let mut stream = File::create(&Path::new("output.asm")).unwrap();
write!(stream, "{}", code).unwrap();
Imports for the example
use std::path::Path;
use std::fs::File;
use std::io::prelude::*;

use x64asm::convert::{ ToAssembly, Separator };
use x64asm::macros::*;

And then, the generated "output.asm" file :

	global _start 
section .text 
_start: 
	mov rax, 1 
	mov rdi, 1 
	mov rsi, msg 
	mov rdx, msg_len 
	syscall 
	mov rax, 60 
	mov rdi, 0 
	syscall
section .data 
msg: db `Hello world` 
msg_len: equ $ - msg 

Notes

Originally inspired by GregoryComer/rust-x86asm.

Commit count: 44

cargo fmt