Learning X86_64 Assembly By Building A GUI From Scratch

Learning X86_64 Assembly By Building A GUI From Scratch

Source Node: 2137960

Some professional coders are absolutely adamant that learning to program in assembly language in these modern times is simply a waste of time, and this post is not for them. This is for the rest of us, who still think there is value in knowing at a low level what is going on, a deeper appreciation can be developed. [Philippe Gaultier] was certainly in this latter camp and figured the best way to learn was to work on a substantial project.

Now, there are some valid reasons to write directly in assembler; for example hand-crafting unusual code sequences for creating software exploits would be hindered by an optimising compiler. Creating code optimised for speed and size is unlikely to be among those reasons, as doing a better job than a modern compiler would be a considerable challenge. Some folks would follow the tried and trusted route and work towards getting a “hello world!” output to the console or a serial port, but not [Philippe]. This project aimed to get a full-custom GUI application running as a client to the X11 server running atop Linux, but the theory should be good for any *nix OS.

<img data-attachment-id="600772" data-permalink="https://hackaday.com/2023/06/18/learning-x86_64-assembly-by-building-a-gui-from-scratch/x11_x64_final/" data-orig-file="https://platoaistream.net/wp-content/uploads/2023/06/learning-x86_64-assembly-by-building-a-gui-from-scratch-1.png" data-orig-size="1055,471" data-comments-opened="1" data-image-meta="{"aperture":"0","credit":"","camera":"","caption":"","created_timestamp":"0","copyright":"","focal_length":"0","iso":"0","shutter_speed":"0","title":"","orientation":"0"}" data-image-title="x11_x64_final" data-image-description="

https://gaultier.github.io/blog/x11_x64.html

” data-image-caption data-medium-file=”https://platoaistream.net/wp-content/uploads/2023/06/learning-x86_64-assembly-by-building-a-gui-from-scratch.png” data-large-file=”https://platoaistream.net/wp-content/uploads/2023/06/learning-x86_64-assembly-by-building-a-gui-from-scratch-1.png?w=800″ decoding=”async” loading=”lazy” class=”wp-image-600772 size-medium” src=”https://platoaistream.net/wp-content/uploads/2023/06/learning-x86_64-assembly-by-building-a-gui-from-scratch.png” alt width=”400″ height=”179″ srcset=”https://platoaistream.net/wp-content/uploads/2023/06/learning-x86_64-assembly-by-building-a-gui-from-scratch-1.png 1055w, https://platoaistream.net/wp-content/uploads/2023/06/learning-x86_64-assembly-by-building-a-gui-from-scratch-1.png?resize=250,112 250w, https://platoaistream.net/wp-content/uploads/2023/06/learning-x86_64-assembly-by-building-a-gui-from-scratch-1.png?resize=400,179 400w, https://platoaistream.net/wp-content/uploads/2023/06/learning-x86_64-assembly-by-building-a-gui-from-scratch-1.png?resize=800,357 800w” sizes=”(max-width: 400px) 100vw, 400px”>

Hello World! In X11!

The first part of the process was to create a valid ELF executable that Linux would work with. Using nasm to assemble and the standard linker, only a few X86_64 instructions are needed to create a tiny executable that just exits cleanly. Next, we learn how to manipulate the stack in order to set up a non-trivial system call that sends some text to the system STDOUT.

To perform any GUI actions, we must remember that X11 is a network-orientated system, where our executable will be a client connected via a socket. In the simple case, we just connect the locally created socket to the server path for the local X server, which is just a matter of populating the sockaddr_un data structure on the stack and calling the connect() system call.

Now the connection is made, we can follow the usual X11 route of creating client ids, then allocating resources using them. Next, fonts are opened, and a graphical context is created to use it to create a window. Finally, after mapping the window, something will be visible to draw into with subsequent commands. X11 is a low-level GUI system, so there are quite a few steps to create even the most simple drawable object, but this also makes it quite simple to understand and thus quite suited to such a project.

We applaud [Phillip] for the fabulous documentation of this learning hack and can’t wait to see what’s next in store!

Not too long ago, we covered Snowdrop OS, which is written entirely in X86 assembly, and we also found out a thing or two about some oddball X86 instructions. We’ve also done our own Linux assembly primer.

Time Stamp:

More from Hack A Day