Learn x86-64 assembly by writing a GUI from scratch (2023)
gaultier.github.io
gaultier.github.io
was a bit confused about the segfault stuff mentioned towards the beginning of the article. but got quite quickly disabused of that notion with gdb etc.
Most "from scratch" would be some sort of microcontroller - write directly to hardware, program registers directly. It's the best feeling when you know every single instructions on the CPU is under your control, there is no 20 million lines of kernel code that do something.
But the MCU needs special hardware (MCU itself, display, programmer) so it is not a good starting project. This brings us to good old MS-DOS. Sure, you need to call BIOS to set up graphics (mode 13h, "320x200x256 colors" is the best for beginners), but after that, you are talking directly to hardware.
The biggest pain in doing "raw" X is the async nature of the protocol - to write a robust X client you really want an event-loop driven approach that embraces that like XCB does, instead of trying to paper over it (like Xlib did).
Kudos for actually getting somewhere in their attempt to do this, a further state than I ever managed.
I never expected to write programs in pure machine code before, but here I am. Writing my own assembler now :)
Way easier than C++ LOL
I highly recommend it.
In my youth, kids learned assembly to crack games.
What certainly helped was that I had did some digital design and instruction set architecture, etc.
Later on, I did some real-world assembly programming for the PIC microcontrollers and some inlined assembly in C, which I did not find daunting at all because of my previous experience.
I guess the best prerequisite for this material is having done some low-level C, the kind where you know about text/data sections and being comfortable with calling conventions, the run time and the linking process.
(but yes, I also would have expected a bit more "from scratch". Is there an annotated disassembly of, say, AmigaOS around?)
Learn x86-64 assembly by writing a GUI from scratch - https://news.ycombinator.com/item?id=36153237 - June 2023 (146 comments)
- small, known size moves are stitched from a fixed number of mov instructions, sometimes overlapped. For example 21 bytes is qword (8 bytes) + xmmword (16 bytes), overlapped.
- char-copying loops like "a++ = b++ c times" with c not known at compile time are either realized as simple increase, compare, conditional jump, or, at higher optimization, a monster that has like 10 branches to use anything from xmmword to byte depending on the amount of data
- big, known size moves (> 256 bytes) just generate a call to memcpy