The Art of 64-bit Assembly

(nostarch.com)

64 points | by 0x54MUR41 2 hours ago

8 comments

  • norir 2 minutes ago
    If you need better performance than a higher level language affords you, I would not recommend programming directly in asm. Instead, I would write a compiler. Raw asm is seductive since the start up cost is relatively low. You can get started in an afternoon. The trouble is that writing correct assembly is much harder than high level code. You have to hold in your head the register state at all times. You have to know if the function you are calling will clobber registers that you need after the call and manually save/restore them. This will slow down your velocity and the resulting code will be long and difficult to read. It will also rely on a lot of undocumented information that only resided in your head while writing and has since been evicted. Good luck debugging a program written in assembly that no one has looked at for two months.

    On the other hand, if you write your own non optimizing compiler, you can avoid a lot of these problems by, for example, tracking what registers a function writes and ensuring they are saved before a call and restored after. Then you can actually get the raw performance of handwritten asm without the pitfalls (the resulting code would still he harder to read and maintain than equivalent high level code, but at least it would be tractable). Even better, you can write your own high level assembler that is actually portable to other architectures. For example, instead of directly modeling x86_64, your compiler can model a cpu with 16 general purpose registers and a set of instructions that map to x86_64 instructions. An arm port would be straightforward since arm also has 16 general purpose registers and you can model x86_64 instructions as one or more arm instructions (and you have extra registers for x86_64 instructions that must be modeled as multiple arm instructions). Or you could do the reverse and model 32 general purpose registers using arm instructions and use predefined memory slots as virtual registers on x86_64.

  • rajeevk 13 minutes ago
    >> You can ask an AI to explain how vtables work in x86. It will give you something that sounds right. What it won’t give you is what Windows actually expects the vtable to look like, why method dispatch behaves the way it does at the instruction level, or what breaks when you deviate from convention. This volume of The Art of 64-Bit Assembly closes the gap between a plausible explanation and genuine understanding.

    Once LLMs are trained with the content of this book, then this statement will no longer be true.

    If this book becomes even a bit popular, these LLMs will get access for sure to the content of this book in their next training.

    • mdp2021 7 minutes ago
      > Once LLMs are trained with the content of this book

      I strongly believe that the "future" should be LLM+Corpus - something beyond LoRA or RAG or context, but what we need and will have will be a generally strong LLM augmented with the most proper learning from the "selected library of relevant material".

      And in parallel, also the reasoner over the corpus will be implemented (something that properly studies, not just reads, and achieves the pinnacle of reflection over the corpus).

      • f3abfeca54812 4 minutes ago
        Are you saying that people should then write books for LLMs only so that you can query them to get the summarized answer and not pay for the book?
    • oinoom 4 minutes ago
      You also don’t need it to be trained on it specifically, just give it some docs or links to docs or source code as reference
    • jagged-chisel 8 minutes ago
      But will they prioritize this information over what they have currently, or over their own hallucinations?
    • f3abfeca54812 5 minutes ago
      But even then your prompt has to be explicit enough to get that answer and not the 'wrong one'. Of course they will prioritize stuff that is more often in the training data. That's why AI will be a dead end long term
  • tensegrist 21 minutes ago
    i'm sorry, starting with "you can ask AI to … [but it'll do an incomplete and bad job]" and then launching forth into a hundred words of AI-produced text is not very appetizing

    i hope this is the publisher's fault and the author replaces it with something decent of their own. contrary to the opinions i've heard around (including elsewhere on this thread) learning asm is still meaningful today and it's something i'd like to get better at so i will consider getting this book or one like it once $work is a bit less hectic

  • Someone 11 minutes ago
    Weird title for a book on assembly

    - for x64

    - on Windows

    - using MASM

    There are other 64-bit OSes, CPUs and assemblers for them, and people do use them.

  • amelius 31 minutes ago
    Is anyone still writing assembly in the age of LLMs? Asking seriously because most assembly is just doing one very simple thing very fast and because it's so simple conceptually, an LLM can easily code it without errors.
    • sousousou 26 minutes ago
      Yep. In my experience, LLMs easily go in circles with even simple assembly, creating fixes that result in 2x slower code and then fixing those with even slower code. They are pretty great as a reference or finding needle in the haystack bugs though!
    • emptybits 18 minutes ago
      I'm not being flippant but I think part of your answer is in the first two words of the title.

      IME while LLMs may help delivery utility in a finished work, humans often value the absorption, mastery, style, or constraint of performing a mundane activity hands-on and brains-on.

    • WalterBright 13 minutes ago
      I do now and then, for bits of code that is not expressible in the language. Things like special instructions, or special fixups.
    • mdp2021 21 minutes ago
      > Is anyone still writing assembly in the age of LLMs ...

      Yes. Its "simplicity" is exactly why we pick and assemble piece by hand - we imagine the leanest way.

      LLMs are (not just in Assembly, but especially) very precious as a natural language manual.

      > an LLM can easily code it without errors

      One day it will probably also be able to have sex, and yet we think we will not pass on the experience - unless, like some kind of coding, it will be a "strictly professional only for money" operation (like in Monty Python's Argument sketch).

      Edit: as esteemed emptybits wrote above, rephrasing: it's /the Art of/ Assembly.

    • eat 18 minutes ago
      You could've also asked if people still wrote assembly in the age of compilers, and the answer would be the same. Sometimes we want/need to control or understand what is emitted by the tool.

      However, I want to point out the obvious fact that people are still playing the piano in the age of the self-playing piano. Humans are experiential creatures, and we enjoy a wide range of activities for their own sake that may have nothing to do with efficiency of output.

    • Keyframe 16 minutes ago
      an LLM can easily code it without errors

      can't tell if sarcasm or not. Just in case it's not.. in that case why don't we all vibe everything in assembly? No need for abstractions anymore since that's a human concept. LLM can do it without errors, as you say, and we'll reap the benefits of speed!

      • mdp2021 11 minutes ago
        > why don't we all vibe everything in assembly

        I really loved this retort, but we have to reply: from that cone of perspective, it would be probably better to "vibecode" in C and not hope that the LLM does a better job than the compiler (for efficiency, not for absence of bugs as per the original).

      • fragmede 9 minutes ago
        Because it's not portable. The world we find ourselves in is at least x86 and ARM, and assembly for one isn't good for the other. LLVM has a hardware agnostic intermediate representation (IR) that one could use though.
  • csense 15 minutes ago
    I'm sorry, MASM? All the cool kids use NASM or YASM.
    • mdp2021 14 minutes ago
      Some still like FASM. Why NASM or YASM?
  • Retr0id 1 hour ago
    > what Windows actually expects the vtable to look like

    I'm not a Windows-knower, are vtable layouts part of the user<->kernel ABI?

    • invokestatic 44 minutes ago
      It’s part of the MSVC x64 ABI, not a part of the kernel ABI. The kernel and user mode (Win32) APIs all use C linkage so vtables are not relevant and the ABI is a convention of the compiler not the OS. Practically speaking though if your software runs on Windows it will probably use the MSVC ABI.
      • Joker_vD 25 minutes ago
        Last time I checked, software compiled with both Cygwin and MSYS2 internally uses SysV calling convention, only switching to WINAPI when calling, well, Win32 API.
    • boguscoder 53 minutes ago
      Yes, you either follow Itanium ABI (non Win) or MSVC ABI. Technically nothing stops _your_ compiler from implementing totally own conventions but it will only be compatible with itself
      • Joker_vD 27 minutes ago
        GHC uses its own calling convention yet it's compatible with Win API just fine. The secret, of course, is to implement not only your bespoke calling convention, but also the other ones that you care to interoperate with.
  • EtienneDeLyon 1 hour ago
    [dead]