Top
Best
New

Posted by 0x54MUR41 7 hours ago

The Art of 64-bit Assembly(nostarch.com)
143 points | 68 commentspage 2
EtienneDeLyon 6 hours ago|
[dead]
norir 5 hours 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.

amelius 5 hours 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 5 hours 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!
derefr 3 hours ago|||
Is that with or without the LLM being able to assemble + profile its hypotheses itself? Because “optimize this code to execute in fewer CPU cycles under the test harness” seems like one of those perfect self-contained problems for LLMs (their equivalent of an “embarrassingly parallel problem”: an “embarrassingly-easily-explored solution space with embarrassingly-easily-measured objective success criteria.”)

Sure, they might make all sorts of dumb hypotheses at first, but as long as the results of those stay in their context, they do seem to eventually “run out of ways to be stupid.” (Which is to say, LLMs seem to experience in-context learning even via self-directed trial-and-error, if given a sufficiently-large number of iterations and no way to cheat.)

ndesaulniers 4 hours ago||||
I was able to use Claude to translate a few thousand lines of assembler from one syntax+toolchain to another. Process was definitely iterative; had to keep adding a few rules along the lines of "don't do this...here's the equivalent pattern." But in the end it did a good job and saved me a ton of time. Allowed me to move a ton of unit tests off a hand rolled broken+bad assembler to a modern production toolchain.
rescbr 1 hour ago|||
This is definitely not my experience with GLM-5.2. It is writing pretty good ARM SIMD code.

It also is an excellent radare2/rizin/ghidra driver as well. Maybe it's because its ~cyber~ capabilities (pretty much linked with assembly-level knowledge) aren't guardrailed off?

stevekemp 3 hours ago|||
I wrote a lisp compiler recently, and that involved generating assembly language.

My output is linux/amd64 assembly language which is compiled by nasm. I don't link to glibc, so I had to implement my "print int", "print string", and similar primitives in raw assembly.

I successfully implemented a stop&copy garbage collector, and other interfaces to the OS such as reading command-line arguments, environmental variables, and so on. All in assembly. Though my compiler is written in golang the runtime, and all supporting functions have to be in assembly to make sure that the binaries it produced are static.

(I wrote a lisp interpreter which can be compiled, and which can be used to run itself, so I got an indirect REPL.)

amelius 1 hour ago||
An efficient concurrent garbage collector would be something I would buy a book for. (In fact I have one GC book on my bookshelf).

But assembly? I'm not sure, honestly. Architectures change, and you can easily converge to good code by talking to an LLM.

May I ask why you didn't use LLVM as a target?

emptybits 5 hours 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.

sureglymop 4 hours ago|||
Yes. Mostly for learning purposes and for fun. Also, there are things you must drop down to assembly level in order to achieve. Implementing coroutines, a JIT compiler, etc. It's fun to learn about these.
mdp2021 5 hours 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.

alain94040 4 hours ago||
> Yes. Its "simplicity" is exactly why we pick and assemble piece by hand - we imagine the leanest way

Professionally, assembly is the last thing I would let an LLM generate for me. That's because if you need to write something in assembly, it's because it's critical: operating system context switch, interrupt handler, that kind of thing. You don't want to be 99% correct, you need that code to be 100% good.

I can imagine some other professional scenarios that I'm less familiar with, where you want to write a tight numeric loop of NEON64 assembly and let the LLM do it, because it's frankly a pain to do by hand, and you can test it to some degree of confidence.

breckinloggins 5 hours ago|||
Yes. And for the same reason I still write in C or Common Lisp - by hand - in the age of LLMs.

It’s a hobby.

eat 5 hours 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.

WalterBright 5 hours ago|||
I do now and then, for bits of code that is not expressible in the language. Things like special instructions, or special fixups.
sitzkrieg 4 hours ago|||
yes, i write c and asm professionally for small hard real-time mcus. llms give me wrong code nonstop in what little i have tried in my field. not a force multiplier
root-parent 3 hours ago|||
>> an LLM can easily code it without errors.

An LLM knowns nothing of the taste of ginger.

Keyframe 5 hours 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 5 hours 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). (Edit: for clarity: some compilers of low level languages are specialized in ASM optimization - the LLM will hardly beat them.)

fragmede 5 hours 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.
Keyframe 3 hours ago||
you're not thinking vibe enough. Your prompts are now the program, that's your abstraction. Ideally, if it would work, LLM would be the one then producing each platform's output. Maybe even compiling from higher languages to gather some compiler optimization hints and then doing full salvo of asm. Ideally.
slashdave 5 hours ago|||
Except for some odd corner cases, it is very hard for a good engineer to beat compilers these days (they are very good now). Not to mention, tuning depends on the platform, and often the exact model of chip.
Keyframe 3 hours ago|||
* it is very hard for a good engineer to beat compilers these days (they are very good now).*

I keep hearing this for the last 20 odd years, yet I see evidence to the contrary each and every day. It is hard and honest work though.

zero-sharp 4 hours ago|||
there were two lead developers from FFMPEG on Lex Fridman's youtube channel who talked about performance gain from assembly. They claimed the opposite

https://www.youtube.com/watch?v=IUo0UwZOaRw

slashdave 4 hours ago||
Yeah, a corner case (streaming) and good developers working very hard.

The original SSE (SIMD) extensions were actually written with this use case in mind. When they first came out, the compilers were awful. They've since caught up.