10 ms·
Learn x86-64 assembly by writing a GUI from scratch
- pkphilip 3y agoThis is cool!
- sylware 3y agoIf you write a wayland compositor in x86_64 assembly... (vulkan+drm on elf/linux), without abusing a macro processor and without obscene code generators...
- steppi 3y agoThis is a really cool little example. I've been teaching myself assembly recently and have found Learn to Program with Assembly (2021) [0] by Johnathan Bartlett to be really valuable. I had initially looked through his freely available book Programming From the Ground Up (2003) [1], which covers x86 assembly, and ended up buying the updated book after finding the old one to be well written but out of date. I've been programming in C for a long time and it's been very cool to dig a little deeper and understand better what's really going on under the hood. [0] https://www.bartlettpublishing.com/site/books/learn-to-program-with-assembly/ https://www.bartlettpublishing.com/site/books/learn-to-progr... [1] https://download-mirror.savannah.gnu.org/releases/pgubook/ProgrammingGroundUp-1-0-booksize.pdf https://download-mirror.savannah.gnu.org/releases/pgubook/Pr...
- anta40 3y agoSomeone ported PGU code to MacOS: https://github.com/lmartinho/pgubook-macos-x86-64 https://github.com/lmartinho/pgubook-macos-x86-64 Seems very handy, since most assembly tutorials nowadays are Windows/Linux-specific.
- Croftengea 3y agoTL;DR: the article explains how to open a new window in X11 and print "Hello, world" in assembly. The asm code to achieve this is 618 lines long.
- ripe 3y agoThank you for summarizing! This adds a lot of color to the headline. I was imagining a framebuffer-based GUI.
- asveikau 3y ago> I will be using the Linux system call values, but ‘porting’ this program to, say, FreeBSD, would only require to change those values Is that true? I remember ~20 years ago I was looking at the i386 syscall ABIs (since amd64 wasn't big then), and there, Linux syscalls passed arguments by register and FreeBSD passed them on the stack. Maybe for amd64, FreeBSD switched to pass by register on Intel, but I wouldn't assume a syscall ABI is such a quick and simple substitution.
- bitshiffed 3y agoFor amd64 they both use the same registers to pass arguments. But, the BSD syscalls use the carry flag to indicate error, rather than the returned value of rax being negative. If your syscalls always succeed, and never return values within what would be a negative range as a signed value, then the code would run; but that's not exactly "portable".
- freedomben 3y agoWhat sort of jobs are there these days that use assembly? Is anybody still using it directly? These are pretty non-specific, but these are area I know about already for others who may have the same question as me: 1. Compiler development 2. Security research (malware analysis/reverse engineering) - although not much if any writing assembly, just reading 3. Kernel development - again mostly just reading assembly, not writing it. Bulk of code written in C (or potentially a very recent development, rust) 4. Driver development - mostly C but some devices can involve assembly
- steppi 3y agoAnother example is writing hand optimized matrix and vector operation routines tailored to specific hardware for BLAS libraries [0]. [0] https://en.m.wikipedia.org/wiki/Basic_Linear_Algebra_Subprograms https://en.m.wikipedia.org/wiki/Basic_Linear_Algebra_Subprog...
- KeplerBoy 3y agoIs this really still a thing? Do people go further than using instrinsics for let's say AVX?
- retrac 3y agoSure. You'll see it very often in codec implementations. From rav1e, a fast AV1 encoder mostly written in Rust: https://github.com/xiph/rav1e/tree/master/src/x86 https://github.com/xiph/rav1e/tree/master/src/x86 Portions of the algorithm have been translated into assembly for ARM and x86. Shaving even a couple percent off something like motion compensation search will add up to meaningful gains. See also the current reference implementation of JPEG: https://github.com/libjpeg-turbo/libjpeg-turbo/tree/main/simd/x86_64 https://github.com/libjpeg-turbo/libjpeg-turbo/tree/main/sim...
- steppi 3y agoYeah. I'm going to be helping to work on expanding CI for OpenBLAS and have been diving into this stuff lately. See the discussion in this closed OpenBLAS issue gh-1968 [0] for instance. OpenBLAS’s Skylake kernels do rely heavily on intrinsics [1] for compilers that support them, but there's a wide range of architectures to support, and when hand-tuned assembly kernels work better, that's what are used. For example, [2]. [0] https://github.com/xianyi/OpenBLAS/issues/1968 https://github.com/xianyi/OpenBLAS/issues/1968 [1] https://github.com/xianyi/OpenBLAS/blob/develop/kernel/x86_64/cgemm_kernel_8x2_skylakex.c https://github.com/xianyi/OpenBLAS/blob/develop/kernel/x86_6... [2] https://github.com/xianyi/OpenBLAS/blob/23693f09a26ffd8b60ebdfbfd79ffa5b649d1289/kernel/arm64/cgemm_kernel_8x4_cortexa53.c#L76-L290 https://github.com/xianyi/OpenBLAS/blob/23693f09a26ffd8b60eb...
- josephcsible 3y ago> Note that Linux has a ‘fun’ difference, which is that the fourth parameter of a system call is actually passed using the register r10. Why is Linux singled out there? No OS can use rcx for that, since the syscall instruction itself overwrites rcx with the return address.
- laxd 3y agoSome use stack for syscall params.
- josephcsible 3y agoOn amd64? Which ones?
- fsckboy 3y agoare you saying "they couldn't use rcx so they use r10, just like everybody else"? Because the quote says r10 and you brought up rcx in any case, there's a good discussion of registers and syscalls here https://stackoverflow.com/questions/53290932/what-are-r10-r15-registers-used-for-in-the-windows-x64-calling-convention https://stackoverflow.com/questions/53290932/what-are-r10-r1...
- josephcsible 3y agoHere's the full quote: > Following the System V ABI, which is required on Linux and other Unices for system calls, invoking a system call requires us to put the system call code in the register rax, the parameters to the syscall (up to 6) in the registers rdi, rsi, rdx, rcx, r8, r9, and additional parameters, if any, on the stack (which will not happen in this program so we can forget about it). We then use the instruction syscall and check rax for the return value, 0 usually meaning: no error. > Note that Linux has a ‘fun’ difference, which is that the fourth parameter of a system call is actually passed using the register r10. That makes it sound like it's only Linux that uses r10 and every other OS uses rcx as the 4th parameter with syscall, but really no OS uses rcx with syscall.
- zerkten 3y agoWriting Win32 programs in assembly was a niche in the late-90s. This post inspired me to do some googling for a project I was familiar with back then and discovered the author has brought it back to life at https://github.com/ThomasJaeger/VisualMASM https://github.com/ThomasJaeger/VisualMASM.
- SeenNotHeard 3y agoOne of the great video games of the late 90s, Rollercoaster Tycoon, was coded in assembly. Even back then, that was considered a feat.
- FartyMcFarter 3y agoIf I remember correctly there were websites with tutorials naming this style of programming "win32asm". This is the one I remember: http://www.afturgurluk.net/documents/Info/Win32ASM/Iczelion%20tut/index.htm http://www.afturgurluk.net/documents/Info/Win32ASM/Iczelion%...
- anta40 3y agoThe MASM SDK project is still alive and pretty active. https://www.masm32.com/download.htm https://www.masm32.com/download.htm. Nowadays I mostly work on Linux and Mac, and wonder why there's no equivalent project exist. Perhaps those Unix coders are satisfied enough with C...
- pjmlp 3y agoNot sure why I would use that instead of the Windows SDK proper. https://learn.microsoft.com/en-us/cpp/assembler/masm/microsoft-macro-assembler-reference?view=msvc-170 https://learn.microsoft.com/en-us/cpp/assembler/masm/microso... Regarding UNIX, due to macro assembler nature of K&R C, and the spread of UNIX source tapes, there was never an Assembly culture like on the home computers where we had an integrated experience, of hardware and OS, that defined the platform. It is similar to how John Carmack describes the IDE culture from those of us that grew up with those platforms, versus UNIX.
- 3y ago
- pavlov 3y agoAs a curiosity, it's worth mentioning there have been entire GUIs written in assembly. Probably the last commercially released one was GEOS a.k.a. GeoWorks Ensemble. It was a small and efficient GUI environment for x86 PCs, briefly somewhat popular as a Windows alternative around 1990. Steve Yegge worked there and tells an interesting story. 15 million lines of hand-written x86 assembly! http://steve-yegge.blogspot.com/2008/05/dynamic-languages-strike-back.html http://steve-yegge.blogspot.com/2008/05/dynamic-languages-st... "OK: I went to the University of Washington and [then] I got hired by this company called Geoworks, doing assembly-language programming, and I did it for five years. To us, the Geoworkers, we wrote a whole operating system, the libraries, drivers, apps, you know: a desktop operating system in assembly. 8086 assembly! It wasn't even good assembly! We had four registers! [Plus the] si [register] if you counted, you know, if you counted 386, right? It was horrible. "I mean, actually we kind of liked it. It was Object-Oriented Assembly. It's amazing what you can talk yourself into liking, which is the real irony of all this. And to us, C++ was the ultimate in Roman decadence. I mean, it was equivalent to going and vomiting so you could eat more. They had IF! We had jump CX zero! Right? They had "Objects". Well we did too, but I mean they had syntax for it, right? I mean it was all just such weeniness. And we knew that we could outperform any compiler out there because at the time, we could! "So what happened? Well, they went bankrupt. Why? Now I'm probably disagreeing – I know for a fact that I'm disagreeing with every Geoworker out there. I'm the only one that holds this belief. But it's because we wrote fifteen million lines of 8086 assembly language. We had really good tools, world class tools: trust me, you need 'em. But at some point, man... "The problem is, picture an ant walking across your garage floor, trying to make a straight line of it. It ain't gonna make a straight line. And you know this because you have perspective. You can see the ant walking around, going hee hee hee, look at him locally optimize for that rock, and now he's going off this way, right? "This is what we were, when we were writing this giant assembly-language system. Because what happened was, Microsoft eventually released a platform for mobile devices that was much faster than ours. OK? And I started going in with my debugger, going, what? What is up with this? This rendering is just really slow, it's like sluggish, you know. And I went in and found out that some title bar was getting rendered 140 times every time you refreshed the screen. It wasn't just the title bar. Everything was getting called multiple times. "Because we couldn't see how the system worked anymore!" ...I have to say, the "140 redraws by accident" part sounds like an ordinary day in web UI development using 2023 frameworks. The problem of not seeing the entire picture of what's going on isn't limited to assembly programmers. You can start from the opposite end of the abstraction spectrum and end up with the same issues.
- xurukefi 3y agoThe "xor rax, rax" that I just saw at a quick glance makes me flinch. Still putting it on my reading list though. Sounds like a really interesting little toy project.
- seritools 3y agoTo explain the flinching (since I didn't catch it immediately): > In 64-bit mode, still use `xor r32, r32`, because writing a 32-bit reg zeros the upper 32. `xor r64, r64` is a waste of a byte, because it needs a REX prefix. (from https://stackoverflow.com/a/33668295/554577 https://stackoverflow.com/a/33668295/554577 )
- samsquire 3y agoThis is awesome, thanks for submitting and thanks to the author. * I would like to understand the assembly used for exception handling. Does anybody know how exceptions work at an assembly level? (I am interested in algebraic effects) * Need to create a closure in assembly. * I have some assembly ported to GNU assembly based on a blog post whose website is down that executes coroutines.
- Findecanor 3y agoBoth of those topics are rabbit holes to fall down into and discover a whole lot. There is not one way to do either, and there are different conventions for different platforms, languages and compilers. I'd suggest to start with the paper "Aspects of implementing CLU" from 1978 that covers both CLU's early type of exception handling and Iterators, which are a form of closures. To find out how modern C++ - style exception handling is done, read "Itanium C++ ABI" (yes, Itanium !), which most of the Unix world used as template for x86-64 and AArch64 later. Then look up "Zero overhead deterministic exceptions" for a proposal for C++ that didn't get picked.
- justinhj 3y agoGreat to see a CLU mention here. There are number of interesting papers and documents floating around but it's rarely mentioned, presumably because it was always a research language and only used by a handful of people in industry. The parameterized type system has features only recently rediscovered in Rust and in C++23.
- pjmlp 3y agoAnd that Go could have used all along, and they even acknowledged that. > We would have been well-served to spend more time with CLU and C++ concepts earlier. From https://go.googlesource.com/proposal/+/master/design/go2draft-generics-overview.md https://go.googlesource.com/proposal/+/master/design/go2draf... Another feature is the checked exceptions that everyone blames on Java, which were already present in Modula-2+, Modula-3 and C++, all of them inspired by CLU.
- deleted 3y ago
- voidz7 3y agodoes this tutorial work on macos?
- nmstoker 3y agoThere are some pointers on that if you skim the tutorial.
- titzer 3y agoThis is great! I'd like to write code to interface X11 without going through libx11 but I've not gotten around to reading the documentation around its binary format. This is a good start!
- eschneider 3y agoYou don't need assembly to do that. It's just another network app. :) Check out Adrian Nye's "X Protocol Reference Manual" to see how to talk X.
- titzer 3y agoSure, but any working starting point is a worthwhile read.
- toast0 3y agoIf you want to be closer to X without reading the protocol documentation, you might look into xcb; it's much less abstraction than xlib.
- maherbeg 3y agoOh man, this brings me back to writing a hot key based application launcher in assembly for windows to learn assembly and the various tools for compiling and building things. Good times!
- wudangmonk 3y agoBeing self-taugh I decided what better way to learn programming than starting with the basics?. Assembly was my first language, I could read and program in it so I considered like I knew the language. It wasn't until I created a sinple 8086 emulator where you take the raw machine code instructions and translate those into not only the assembly instructions but actually emulate what those instructions do that I finally felt like I REALLY knew assembly. My suggestion to others that want to learn assembly is to skip any assembly books. Using whatever language you want first start with a translator from machine code into assembly instructions, and then do an emulator. You only need to implement a small subset of the instructions, check out godbolt and translate some simple programs to know which instructions you need to implement. Other than that all you really need is the 8086 manual, it has all the information there. I also found this site useful when implementing the flags https://yassinebridi.github.io/asm-docs/8086_instruction_set.html https://yassinebridi.github.io/asm-docs/8086_instruction_set.... This takes less time than finishing a book and you learn a LOT more. The goal is not to program in assembly at all but to truely understand the cost of everything and what you can expect from your hardware.
- nerpderp82 3y agoBecoming skilled at GDB and knowing how to generate assembly listing from your tooling is a key skill that really helps with understanding. I learned assembly by reading the assembly listings from the C compiler. It is extremely interesting to be able internalize how high level constructs are compiled and optimized.
- BlackLotus89 3y agoThere was a reverse engineering guide that I quite liked that introduced you to assembly by first writing c examples, compiling them and then analyzing the disassembled output. It was quite a long guide, but I would recommend it to anyone starting out. I don't have it in my bookmarks it seems, but I will try to update my comment tomorrow when/if I find it. Edit: damn I guess it was https://beginners.re/ https://beginners.re/ before it became pay-walled. Web archive still has copies of the book, but if you like it you should consider buying it even if it means signing up for patreon m-( I still got a few versions of the book somewhere as well. Have to dive in again to see if it is as good as I remember
- fuzztester 3y agoTwo older assembly language programming books that I had checked out earlier, and thought were good, are ones by Randal Hyde and Paul Carter. Both were for 32 bit assembly, not 64 bit, IIRC. Paul Carter was a professor or lecturer at a US college. I think his book was available online.
- fuzztester 3y ago>I think his book was available online. http://pacman128.github.io/pcasm/# http://pacman128.github.io/pcasm/# Scroll down the page for the PDF book.
- redsaz 3y agoRandal Hyde now has an Art of Assembly edition for 64-bit.
- fuzztester 3y agoCool, good to know.
- pacman128 3y agoPaul Carter here. Yes, as someone already replied. It's online. I would have liked to update it to 64-bit, but I jumped to industry and don't have the time to do a decent job of it. I didn't realize that Randall had a 64-bit version out. I'm sure it's very good. We both used to hang out on comp.lang.asm.x86 back in the 90's.
- fuzztester 3y agoOh cool. Thanks for making it available online.
- Solvency 3y agoIn 2023, does anyone who writes a compiler inherently have to know assembly? Or even less recently...whoever wrote the first Rust, Zig, or insert <new compiled language> here? Because don't you ultimately have to know how to make your own syntax translate into efficient assembly code? Or is there someway these days for programming language designers/creators to avoid it entirely?
- dahfizz 3y agoLlvm abstracts the "backend" which generates the actual assembly for each target machine. You only have to write a "frontend" that generates an llvm intermediate representation. But in general, yes. To generate assembly you need to know assembly.
- Solvency 3y agoIs LLVM sufficiently "simpler" to learn and wield than assembly, or does it just make it easier to compile to different systems?
- jcranmer 3y agoLLVM is definitely more complex than a toy assembly you might learn in an intro computer architecture course, but it's generally somewhat less complex than working with real assembly languages. Although the complexity in LLVM is a very different kind of complexity from assembly languages; LLVM is ultimately a higher-level abstraction than machine code, and the semantics of that abstraction can be complex in its own right.
- gamache 3y agoCompiler writers can target high-level languages too; it's not uncommon to see e.g., a Blub-to-C compiler which leaves the asm parts to a different toolchain. (Lots of languages without the goal of producing native code target even higher-level languages, for example JS.) Another popular way to _sort of_ avoid assembly is to target the LLVM IR (intermediate representation), in which case LLVM takes care of optimization and producing processor-specific machine code for a bunch of CPU types. But LLVM IR is basically a fancy assembly language.
- deleted 3y ago[deleted]
- jiffygist 3y agoSome useful gui program examples for winapi https://www.davidgrantham.com/ https://www.davidgrantham.com/
- jagged-chisel 3y agoSeeing the headline, one could be scared away thinking this is bare metal from scratch. It is not. The app is an X11 client and will run under an OS, meaning you’ll learn to make system calls and other library calls to get things on the screen. Very educational, and not scary-deep.
- tengwar2 3y agoIt's also not that hard to write a simple real bare-metal GUI in assembler. I had to built some lab apparatus under OS/2 1.0, which didn't have a GUI, and most of it was in assembler. It's a while back, but from memory, the main bits I needed were lines and circle segments (using Bresenham's algorithm); mouse pointer draw/refresh; writing text (I copied bitmaps for VGA text from MS/DOS); menus; text entry; and mouse drag as a way of selecting an area or marking a circle. Apart from the mouse pointer, it was single-threaded and pretty simplistic, but good enough to be in use for several years. It had to be: it wasn't easy to move to OS/2 1.1 and get the Presentation Manage GUI because they changed the memory model and for performance reasons some of the bit-bashing I needed for a non-GUI part of the system was done with a large amount of wire-wrap. Anyway, the bare-metal GUI bit was really pretty simple to implement.
- ndesaulniers 3y agoWriting a Linux application in _Intel_ x86 assembler syntax...smh. You do not know de wey
- lost_tourist 3y agoIf you're going to learn assembly for the first time I would say start with arm-64 assembly first, the architecture is much more refined and the assembler much more pleasurable to code with less foot guns and complication unless you are doing only the most basic of programs.
- troad 3y agoFor anyone interested in x64 assembly, it's worth noting that a new edition of Jeff Duntemann's excellent and classic introductory book on assembly, now fully updated for x64, is sitting with his publishers and is likely to be out sometime around the summer. Source: http://www.contrapositivediary.com/?m=20230222 http://www.contrapositivediary.com/?m=20230222
- satiric 3y agoIs there a practical reason to do this? I don't mean that disparagingly; it's a cool project and I can see its value. I'm just wondering if there's also a practical reason you might do something like this rather than just using Qt or HTML/CSS or whatever.
- gigel82 3y agoThe title is confusing. What is a GUI from scratch? A bootloader / mini kernel with framebuffer? A win32 application? Should probably be something like "Writing a Linux X11 application in assembly".
- felcro 3y agoPlease add a C implementation too to make it easier to understand what's going on by reading less code.
- Havoc 3y agoI respect people doing this sort of thing but hard pass from me. Even my venturing into rust is plagued by doubts as to whether it’s too low level for productivity
- bschwindHN 3y agoI would say Rust is quite high level. You can write entire, useful programs without once having to think about memory, pointers, allocation, etc. Of course that won't be true for every program, but it's worlds away from asm.
- Havoc 3y agoAgreed. Rust is already pretty comfortable. Meant it more in the same that asm to rust is a jump and then there is another jump from say rust to python. I’m sitting on the rust rung and wondering whether that was the right choice so surprised people go one step further. (To each their own ofc)
- userbinator 3y agoAlways use the standard function prologs and epilogs This sounds like another one of those common "learn Asm by acting like a compiler" articles, which IMHO completely misses one of the best reasons to learn Asm: you can beat the compiler on size (relatively easy), speed (often harder), or both, precisely by not acting like one. I suspect the author, like so many others, also learned from only reading (some) compiler output. The complete lack of any use of static initialised data is shocking. mov rdi, rdi lea rsi, [rsp] Please don't do this. Even a compiler can do better at O0. Stripped and OMAGIC (--omagic linker flag, from the man page: Set the text and data sections to be readable and writable. Also, do not page-align the data segment): 1776 bytes (1 KiB) Besides being a very notable date (was that deliberate?), 1776 is closer to 2k than 1k. I suspect if you wrote it in C with inline Asm for the syscalls, it wouldn't be much bigger (and may even be a little smaller.) If you want to see what Asm can really do, the sub-1k categories in the demoscene are well worth looking at.
- jart 3y agoLike the Lambda Calculus in 383 bytes. https://justine.lol/lambda/ https://justine.lol/lambda/
- matheusmoreira 3y ago> ‘porting’ this program to, say, FreeBSD, would only require to change those [system call numbers] This is not correct. Only Linux has a stable kernel-userspace interface which allows you to depend on these numbers. In pretty much every other operating system, you are required to use the system libraries they provide. Compiling a program with these numbers hardcoded into them will cause them to break when the OS developers change the syscalls. I wrote an article about this with more details and lots of citations: https://www.matheusmoreira.com/linux/system-calls https://www.matheusmoreira.com/linux/system-calls
- toast0 3y agoFreeBSD may not promise stable system calls, but it usually delivers. The documented procedure for upgrades is update the kernel, reboot, update userland, restart deamons as needed. That only works if the old userland can run on the new kernel, so usually it can. Certainly there have been exceptions, but in my experience, they've all been fixed. FreeBSD documentation includes a guide on assembly programming [1], which means libc isn't the only acceptable interface to the kernel. Staticly linked executables are supported by the included compiler, those are expected to continue to work after kernel updates, which requires syscall stability. Syscalls that take structures at risk of changing sizes generally include size as a parameter or within the structure, and the transitions to different sizes haven't always been easy (cpuset increases were rough in the past, but the next one will be better), but the syscall interface didn't change really. [1] https://docs.freebsd.org/en/books/developers-handbook/x86/ https://docs.freebsd.org/en/books/developers-handbook/x86/
- matheusmoreira 3y agoThay's just the thing... As far as I know Linux is the only one which does make that promise. If user space programs break it's a bug in the kernel. https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/ABI/stable/syscalls https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/lin... Other systems have the same "usually works but no promises" disposition which means they can't be relied upon. Things work fine until they don't. When breakage occurs they take no responsibility for them because they didn't promise anything to begin with. Torvalds says today's Linux can run binaries from the 90s, that's how serious this guy is about ABIs. I've never seen other operating system developers claim anything of the sort. Not even Microsoft.
- cubefox 3y agoAssembly "Hello World": > just 600 lines of code
- deleted 3y ago[deleted]
- herewulf 3y agoExcellent. Now how do I blit some pixels? This takes me back about 30 years as a youngster discovering the magic ASM incantation to efficiently draw to the screen in DOS mode 0x13.
- deleted 3y ago[deleted]
- mrlonglong 3y agoI just tried this out, it always dies after calling x11_send_handshake, at the point where it reads back 8 bytes. It seems it expects the first byte NOT to be zero. ... cmp BYTE [rsp], 1 jnz die How can I diagnose the issue? The article didn't dive into the matter of reading error codes.
- mrlonglong 3y agoTo follow-up, the author really should have mentioned that access to X is controlled using cookies. In order for this program to work, one needs to temporarily allow any client access to X using 'xhost +'. To put back access control, use 'xhost -' to re-enable.