Casm wrote:Thare can, for example, be no instruction in a high level language equivalent to lgdt, or ltr, because they are very hardware specific.
Why
can't there be? Take a look at GCC's function attributes, for instance:
http://gcc.gnu.org/onlinedocs/gcc/Funct ... butes.html – some of those are very hardware-specific (interrupt, for instance). I can very well imagine either a variable attribute “register” or even builtin variables which map to (obviously hardware-specific) registers:
Code: Select all
register uintptr_t cr3 __attribute__((register("cr3")));
or directly a variable called “__cr3” which maps to the corresponding control register.
The same could obviously be used for the GDT:
Code: Select all
register struct { uintptr_t base; uint16_t limit; } __attribute__((packed)) gdtr __attribute__((register("gdtr")));
I don't see the point why access to CPU control registers, I/O ports* or the creation of interrupt handlers should actually be impossible in all high level languages. Of course it would possible, if they'd be designed with that usage in mind (and as you see in the GCC function attribute list, there actually
is already a way to create an interrupt handler in GCC-specific C (for certain architectures, at least – excluding x86)).
Especially when doing OS development, many of us feel already that standard C/C++ is often not sufficient. Take __attribute__((packed)), for instance – standard C makes no guarantee about the placement of fields in a structure, though sometimes, you need to know (for instance for network packet headers). This is already ignoring the fact that most of us (if not all) are horribly abusing C/C++ already by doing basically completely undefined pointer operations (ever casted an integer to a pointer that wasn't created by casting a pointer to an integer?).
So we're already using an extended version of C/C++ in an undefined way (most of us, at the very least). I see only one reason why we shouldn't just add more extensions to C/C++ which would make assembly optional: Because writing that bit of assembly is actually much easier. However, theoretically, we could do it. So there is no reason to say “we use assembly, because we must”, but rather, “because it is in some special cases easier than extending high level languages to do the same” (and “but GCC does not support the extensions you proposed” is not an excuse: GCC is free software, “just” patch it, if you really want to).
JAAman wrote:ultimately everything becomes assembly
That's the point. Since everything becomes assembly, your compiler is theoretically capable of emitting every assembly code you could also write yourself.
JAAman wrote:in other languages, you are not directly controlling the CPU, you are controlling the logic, therefore there can be nothing inherently in the language that can directly control the CPU...
And this, on the other hand, is wrong. Of course there can. There is no fundamental reason to restrict a compiler to emit only arithmetic-logical and branching instructions.
To conclude my opinion: We use assembly, “because it is in some special cases easier than extending high level languages to do the same”.
Also:
Kevin wrote:Assembly is a great choice whenever you feel development is becoming boring because you have too few bugs.
.
Yeaaaaaah, assembly can be a hell lot of fun!
*Especially accesses to I/O ports – inb and outb could well be just builtins directly provided by GCC.
Oh, fun fact, look, what I just found:
http://gcc.gnu.org/onlinedocs/gcc/Expli ... -Vars.html