r/AskProgramming • u/Realistic_Debate1704 • Sep 04 '26
Architecture Can I build a programming language that “learns” assembly implementations? (Idea)
Is this dumb idea??
I had this random idea for a programming language and I'm wondering if something like this is actually possible.
Basically, the idea is that I could teach the compiler how to implement things in assembly.
For example, I could write something like:
teach get(x):
asm:
; assembly code for getting input
The compiler remembers that. Then later I can just write:
get(age)
and it knows to use the assembly code I taught it earlier.
So instead of the compiler already knowing what every function does, I'm basically teaching it new things by giving it the assembly implementation once.
The goal would be a really simple language to write, but where the compiler generates native assembly/machine code.
Is this actually possible? And would it be worth building as a project?
I don't know low-level programming or compiler stuff that well, so consider me a complete noob and feel free to correct me if I'm misunderstanding something.
3
u/Suitable-Season-4847 Sep 04 '26
Congratulations, you've just invented a helper function.
2
u/mxldevs Sep 04 '26
So you are basically writing a language and then writing how the syntax gets compiled?
How is it different from any existing programming language?
2
u/KindaLegallyBlindDev Sep 04 '26 edited Sep 04 '26
What you are describing is what, in some sense, any programming language that compiles directly to assembly does, be it C, C++, Go, Rust, etc. The functions you write will get compiled down to assembly, and then they will linked together wherever they are called. It might even optimize away the function call and just add the assembly instructions straight into the calling function (inlining).
EDIT: I suggest you look at some basic examples of C, or Rust, and put them in their respective subdomains of the compiler explorer, godbolt. Like rust.godbolt.org you can experiment with the code to the left to see the resulting assembly in the right side (assuming you are on PC). One thing to note is that on the right side, above the assembly output, you will see a small text box, in there you should add options for compiler optimizations, with something like -O3 for C and -O for Rust.
2
u/0jdd1 Sep 04 '26
When I first skimmed over your question, it reminded me of Massalin’s work on superoptimizers back in the 1980s. (It’s not really the question you asked, of course, but it’s related and you might find it interesting.) https://en.wikipedia.org/wiki/Superoptimization
2
u/BobbyThrowaway6969 Sep 04 '26
As others said, it could be a fun educational project to try but it doesn't have any practical use.
2
u/Strict-Department286 Sep 05 '26
Are you going to let people teach the compiler new keywords and control flow statements?
Could get interesting...
8
u/aioeu Sep 04 '26 edited Sep 04 '26
While most of the other commenters have directed you to functions, which are frequently the best way to reuse code, many assemblers have macro facilities that do pretty much exactly what you have described. Indeed, these assemblers are often literally called "macro assemblers".
Macros can be useful when you need to generate functionally similar sequences of code. The use cases for them are reasonably rare, but they do exist.
5
u/dmazzoni Sep 04 '26
In addition to what others said, it's important to know that "getting input" is not an assembly language instruction. It's a call to the operating system, and it's probably hundreds of thousands of lines of code to implement.
Your CPU can only do basic arithmetic, move memory around, and execute an if/then type instruction to decide what to do next. That's pretty much it - there are more instructions but they're just variations on those.
How you get from just those basic instructions to a working computer is basically 7 decades worth of abstractions on top of abstractions.
At a high level, getting input involves handling incoming packets from the USB controller, interpreting HID packets from a keyboard, turning those into key down and key up events, mapping the physical key to the character it represents, queueing or buffering that from kernel space to user space, and then finally responding to your get() function.
4
u/ficelle3 Sep 04 '26
That just sounds like declaring a function with inline assembly, unless I'm missing something?
You can do that in C, and there's a few very good reason it's not commonly used.
Don't get me wrong, it's an interesting skill to learn and you should absolutely mess around with it. Just be aware of the drawbacks if you're considering using it in a project.
3
u/IAmInBed123 Sep 04 '26
Yeah sure, that's what low code does like xcp on documentum on java on compilercode. It's just a bit of work and pretty difficult.
4
u/nedal8 Sep 04 '26
https://en.wikipedia.org/wiki/Cross_compiler
2
u/LunkWillNot Sep 04 '26
I haven’t used either, but from reading about it, Forth and AmForth could give you some inspiration.
2
u/GoogleIsYourFrenemy Sep 04 '26
Sure but why? The assembly language isn't going to change.
LLVM has their compiler split in two. They have front ends that convert arbitrary code (c, c++, etc.) into bytecode and then the backend that converts the bytecode into assembly. Makes it easy to support new processors and new languages without having to know everything about processors and languages.
28
u/[deleted] Sep 04 '26
[removed] — view removed comment