1

I find godbolt very useful for comparing how different implementations of the same algorithm result in different assembly code. For example:

pub fn range_north(sq: u8) -> impl Iterator<Item = u8> {
    (sq..=(sq | 0b111000)).step_by(8)
}

pub fn range_north2(sq: u8) -> impl Iterator<Item = u8> {
    let (rk, fl) = (sq / 8, sq % 8);
    let n = 8 - rk;
    (sq..=63).step_by(8).take(n as usize)
}

gives

example::range_north:
        mov     rax, rdi
        movzx   ecx, sil
        or      sil, 56
        movzx   edx, sil
        shl     edx, 8
        or      edx, ecx
        mov     word ptr [rdi + 8], dx
        mov     word ptr [rdi + 10], 256
        mov     qword ptr [rdi], 7
        ret

example::range_north2:
        mov     rax, rdi
        movzx   ecx, sil
        shr     sil, 3
        mov     dl, 8
        sub     dl, sil
        or      ecx, 16128
        movzx   edx, dl
        mov     qword ptr [rdi], 7
        mov     word ptr [rdi + 8], cx
        mov     word ptr [rdi + 10], 256
        mov     qword ptr [rdi + 16], rdx
        ret

Nothing more. How can I produce this output offline with Rust's command line tools without the boilerplate code and other pieces that don't help me compare generated assembly, like the above?

  • 1
    [`cargo-asm`](https://crates.io/crates/cargo-asm) may be of help – user2722968 Oct 09 '22 at 20:34
  • Does this answer your question? [How to get assembly output from building with Cargo?](https://stackoverflow.com/questions/39219961/how-to-get-assembly-output-from-building-with-cargo) – Finomnis Oct 09 '22 at 21:15
  • 1
    For single files or functions without dependencies you can call `rustc` directly, instead of cargo: `rustc --emit=asm --crate-type=lib -O test.rs`. – rodrigo Oct 09 '22 at 21:21

1 Answers1

1

cargo rustc --release -- --emit asm -C "llvm-args=-x86-asm-syntax=intel" will produce optimized GAS with Intel syntax.

You can also use cargo asm.

Miiao
  • 751
  • 1
  • 8