First of all, I don't know about modern CPUs and operating systems. For this reason, I will explain my explanation over the intel 8085 processor. And of course, I would like you to imagine that there is an operating system that can run on the intel 8085.
we have such assembly code:
MVI A,16
MVI B,16
ADD B
HLT
This code is very simple. When this code runs, it does the following: It loads the number 16 into registers a and b of the intel 8085 processor. And then it adds the value of these two registers.
Of course, when we try to run this code in our operating system, most likely nothing will happen.
What I want to ask is: How can I run a code that does not contain any system calls (or anything operating system specific) on the operating system (by-passing the operating system)? And I don't want the operating system to crash while doing this.
Clearly you are running an operating system, so why not try it yourself?
Processors only know how to run their instruction set and there are countless programming languages you can use to generate those instructions, including assembly language.
so.c
fun.c
so
So now let's use assembly language:
But now let us move it up a level:
Switching architectures, because I feel like it...
and I get a seg fault (same with x86 doing a retq). As pointed out in the comments some will let you just return but you probably have to make an exit system call. Note I did not crash the operating system. I just crashed that program.
Assembly language is not the real problem but getting the program into the OS and running it. And how you do that normally is creating a binary file that is a file format supported by the operating system (including various linker specific things as to memory space, entry point, etc). Otherwise you have to try to hack the operating system to shove it into memory and then convincing the operating system to run that code.
You could create or choose an operating system for your 8085 that allows a simple return as a way to exit a program, or modify the operating system to allow that, then you can just perform a return from subroutine/function call and it is just a few pure instructions with no system calls.
There is no magic to assembly language, just more freedom. The processor can only run its instructions, it does not know how to run C language or C++, or rust, or python, etc. Just its own instructions. And assembly language is just one programming language you can use. Most of the problem is not the program but the file format, the operating systems rules, and how to exit cleanly.