Computers are so weird, and it’s honestly a miracle any of this shit runs at all...
Quin @TheQuinbox@dragonscave.space
Computers are so weird, and it’s honestly a miracle any of this shit runs at all.
Take something as simple as x86 integer division. The div instruction doesn't take two operands, it takes one. And, it doesn't do the seemingly logical thing of x / y; it implicitly takes the 128-bit value mashed together across rdx and rax, divides that by whatever arbitrary register you hand it, and dumps the quotient into rax and the remainder into rdx. If you don't zero out or sign-extend rdx first? Have fun with UB that you might not hit until 25000 runs of your program, because the quotient doesn't fit in 64 bits.
And then you zoom out to basic program execution. When you write int main() in C, supposedly the language of bare metal, you think your code is the entry point. But nope. The C runtime has _start, which sets up your registers, initializes the runtime, and passes the address of your main to __libc_start_main. Except you haven't even reached that yet. Typing ./test in your terminal made your shell fork, then call execve. Then, the kernel parsed the ELF binary, memory-mapped your segments, spawned the dynamic linker into user space to map all your shared libraries, and then handed off execution. And before main gets touched, libc is running .init_array, setting up TLS, and registering atexit hooks.
I learned some of this when I got really into Win32 programming using Assembly with Masm back in 2024, but learning about it on Linux actually lets me understand how it works rather than how Microsoft kludged it together, and this is insane.