flat assembler
Message board for the users of flat assembler.

Index > High Level Languages > Assembly with Rust:

Author
Thread Post new topic Reply to topic
bitRAKE



Joined: 21 Jul 2003
Posts: 4073
Location: vpcmpistri
bitRAKE 02 Jun 2022, 08:10
Code:
#[allow(non_snake_case)]
#[inline(always)]
/// Try to showcase all the asm!() features in one routine, lol:
///
/// A000931: Padovan sequence (or Padovan numbers):
///      [1,0,0],1,0,1,1,1,2,2,3,4,5,7,9,12,16,21,28,37,49,65,86,114,151,...
/// `Plastic number, only real solution to x^3 = x + 1`
fn A000931(nth: u64 ) -> u64 {
    let mut result:u64 = unsafe { MaybeUninit::uninit().assume_init() };
    unsafe {asm!(

"           push    1
            pop     {p0}
            xor     {p1:e}, {p1:e}
            xor     eax, eax
        7:  lea     {p0}, [{p0}+{p1}]
            xchg    rax, {p1}
            xchg    rax, {p0}
            loop    7b
",
            p0 = out(reg) _,
            p1 = out(reg) _,
            inlateout("rcx") nth => _,
            inlateout("rax") result,
            options(pure, nomem, nostack),
    )};
    result
}    
... the flexibility is quite interesting, and I haven't starting creating Rust macros, yet. So, far it seems to be doing what I expect. This sample shows implied registers, literal registers, register modifiers, dynamic register selection, ...

In some cases, many hoops need to be jumped through to get around the language safety features. For example,
Code:
    let mut result:u64 = unsafe { MaybeUninit::uninit().assume_init() };    
... is just to say the variable doesn't need to be initialized. But that is because I erroneously inidicated that result is an input to the asm!.


Posting my discovery process on github:
https://github.com/bitRAKE/rust-playground

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 02 Jun 2022, 08:10
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8359
Location: Kraków, Poland
Tomasz Grysztar 02 Jun 2022, 10:27
Funny how this reminds me of my "I" language, which I was working on in high school, and which was supposed to be a HLL with ability to tightly integrate inline assembly blocks. I got the assembly part working, and this was my first assembler (the one before fasm), but in the process I discovered that I personally did not really need anything more than that, so the actual HLL part never got finished. And my next assembler, fasm, was designed right from the start to be just pure assembly.

And isn't it fun that it turns out that such inline assembly is still being useful after all these years? And/or intrinsics. I vaguely recall that 20 years ago many people would predict it all would be long gone and forgotten by now.
Post 02 Jun 2022, 10:27
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4073
Location: vpcmpistri
bitRAKE 02 Jun 2022, 14:02
The robustness of the HLL part is quite good, but it obviously comes at substantial cost: 100's of MB to the build system for each project; massive runtime overhead - which includes the loss of two general purpose registers; and a cryptic language which is evolving away from the low-level features.

Rust is just doing some substitution and breaking up lines for the backend (gcc/clang). Which is just enough to make it useable, imho. It's certainly better than trying to inline asm in clang/gcc directly.

I still need to try out some AVX algorithms. There are subtleties in the asm!() macro where it will silently uses the same register for multiple parameters, which is usually not the desired behavior. Even having the anonymous registers labeled didn't help. Marking all clobbered registers as outputs is a must.

As long as there is an assembly language I think people will program in it.
Post 02 Jun 2022, 14:02
View user's profile Send private message Visit poster's website Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1671
Location: Toronto, Canada
AsmGuru62 02 Jun 2022, 22:50
Recently I found out that all I need from HLL to code a large project is IF/ELSE/REPEAT/WHILE -- but FASM #1 already has all that!
I also have an excellent IDE -- coded it myself.
Post 02 Jun 2022, 22:50
View user's profile Send private message Send e-mail Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 2564
Furs 03 Jun 2022, 13:13
bitRAKE wrote:
Marking all clobbered registers as outputs is a must.
Maybe it's just me, but I don't mind that. I like inline asm (in GCC) and I never had problems specifying operands, because I always treat it as a black box to the compiler who only "knows" the "specification" aka inputs/outputs/clobbers and so on.

I really don't like Rust though. Is there a difference between Rust's inline asm and GCC's normal inline asm used in C/C++ code?
Post 03 Jun 2022, 13:13
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4073
Location: vpcmpistri
bitRAKE 03 Jun 2022, 14:02
Furs wrote:
Is there a difference between Rust's inline asm and GCC's normal inline asm used in C/C++ code?
I've been digging into this question. It seems that the code presented to LLVM is more verbose.

https://rust-lang.github.io/rfcs/2873-inline-asm.html

... outlines the strategy Rust uses, and contrasts with methods used in other languages.

Some examples, https://doc.rust-lang.org/rust-by-example/unsafe/asm.html
Dev Doc, https://rustc-dev-guide.rust-lang.org/asm.html

It seems mostly compatible with GCC's inline asm, but with some other features.

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 03 Jun 2022, 14:02
View user's profile Send private message Visit poster's website Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 2564
Furs 04 Jun 2022, 12:51
Thanks, that's very intriguing. Wink
Post 04 Jun 2022, 12:51
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4073
Location: vpcmpistri
bitRAKE 23 Jun 2022, 18:45

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 23 Jun 2022, 18:45
View user's profile Send private message Visit poster's website Reply with quote
sylware



Joined: 23 Oct 2020
Posts: 461
Location: Marseille/France
sylware 26 Jun 2022, 14:45
wonder if this inline assembly is supported in the rust-written rust compiler for supported targets.
Post 26 Jun 2022, 14:45
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4073
Location: vpcmpistri
bitRAKE 26 Jun 2022, 17:46
Everything I've read indicates the LLVM backend is still used to compile ASM for Cranelift. Cranelift itself states executing a external assembler is supported/configurable.

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 26 Jun 2022, 17:46
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.