Live data from Hacker News

Ask HN: What are some examples of beautiful x86 assembly code?

news.ycombinator.com

71–80 of 93 posts

Re: Ask HN: What are some examples of beautiful x86 assembly code?

#73
post #12

I bought a nice little booklet of small x86 gems a few years back[1][2] and this one is my favorite: .loop: xadd rax,rdx loop .loop Fibonacci with two instructions. [1] https://www.amazon.com/dp/1502958082 [2] https://www.xorpd.net/pages/xchg_rax/snip_00.html

Can someone more versed in x86 explain to me how this works? It's been a long time since I've done any assembly, and that was MIPS, but I don't see how there's any sort of exit condition or anything. I'm guessing there's more to xadd than `x + y = z`?

"xadd r1,r2" sets r2=r1 and r1=r1+r2 at the same time.

Re: Ask HN: What are some examples of beautiful x86 assembly code?

#74
post #31

Earlier quoted context omitted.

If all functions took (rax, rbx, rcx, ...) and you had loop statement that defrements rcx and jumps unless zero, then how would you write fib-function’s body?

You raise a good question. At one time you would have only one loop running through the system as per my understanding. I am a little lost here.

I think this is the biggest problem with learning x86 assembly (or ARM or anything else) on modern systems (or more specifically modern operating systems).

It’s sometimes difficult to think about the assembly code in situ when you start to think about the operating system doing a ton of context switching and paging etc. in the background, which can distract your thought process from what’s right in front of you (as well as the operating system’s software interrupts / system calls on top of the basic ISA, which is another abstraction!)

Older systems had the currently running program as the entire context of the system at that point in time - in a similar way to embedded programming, which is imho a much easier realm to learn assembly in once you’ve got a bit of basic electronics under your belt!

Re: Ask HN: What are some examples of beautiful x86 assembly code?

#75
post #26

Earlier quoted context omitted.

No, loop decrements rcx until it reaches zero and then stops

But rcx has not been initialised.

It's a hardware register, so it has always has a value (in 64-bit mode, at least), whether or not the program explicitly sets it.

Re: Ask HN: What are some examples of beautiful x86 assembly code?

#76
post #31

Earlier quoted context omitted.

If all functions took (rax, rbx, rcx, ...) and you had loop statement that defrements rcx and jumps unless zero, then how would you write fib-function’s body?

You raise a good question. At one time you would have only one loop running through the system as per my understanding. I am a little lost here.

In a single-core cpu, the operating system's scheduler manages the register state for each process: basically, when switching from one context to another, it dumps the old process' registers to memory, and loads the register state to the new one. From the user's point of view, the register state will appear unaffected by different processes: your loop register will not changed by other processes and threads. There is no parallelism, so only one program and register state is active at once, but there is concurrency (if the OS supports it). On a multi-core processor, each core has its own set of registers, so the scheduler could theoretically run a multiple processes uninterupted on one core per process.

Re: Ask HN: What are some examples of beautiful x86 assembly code?

#77
post #12

I bought a nice little booklet of small x86 gems a few years back[1][2] and this one is my favorite: .loop: xadd rax,rdx loop .loop Fibonacci with two instructions. [1] https://www.amazon.com/dp/1502958082 [2] https://www.xorpd.net/pages/xchg_rax/snip_00.html

interesting but i prefer Binet's formula

Re: Ask HN: What are some examples of beautiful x86 assembly code?

#78
post #58

Come on, the movfuscator has to be the best x86 I've seen: https://github.com/xoreaxeaxeax/movfuscator

I was going to ask why? But then I read the single faq at the end: because I thought it would be funny...

In addition to being funny, it's appealing to me to have the option of generating assembly to perform a task in such a way that a human looking at the assembly would have enormous difficulty in determining what is performed. As suggested by its name, it serves as a nice obfuscator.

It also fully avoids all branches.

Post reply on HN