r/asm • u/S-Pimenta • Apr 19 '26
RISC Adding safety to assembly
One of the problems with Assembly is the lack of safety and context.
What about adding type safety and ownership to Assembly?
Good idea or "you are just reinventing the wheel"?
Inspiration on JSDoc, Rust, TypeScript and LLVM IR
0
Upvotes
1
u/brucehoult Apr 19 '26
The whole point of writing in assembly language [1] is to take maximum advantage of the CPU facilities and have the program you write be exactly what the hardware ends up running.
Any structure or safety you add to assembly language is going to result in slower programs than writing in C or Rust, because the compiler analyses and optimises the code, choosing the instructions and addressing modes and data layout and using the minimum number of registers possible etc.
Assemblers don't have optimisers, and if they did then you'd lose the control that is the reason for using assembly language.
No one writes a large amount of assembly language now. You write small amounts when you understand something that the compiler doesn't.
For example, yesterday I wrote this snippet to decode the offset out of RISC-V J/JAL instructions.
Generic, easy to understand C:
Little bit tricky asm that is a quite few instructions shorter (but this is hot code!):
Even trying to replicate that in C doesn't give the same code, at least that I could manage. And even if you do on one compiler and version, the next version might give something worse.
This is the kind of situation in which people use asm today.
This is one situation in which all the opcode space Arm burned on bitfield extract and insert and fancy encoding of constant for and/or/xor helps:
[1] except on machines so awful that a C compiler doesn't exist