Live data from Hacker News

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

news.ycombinator.com

11–20 of 93 posts

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

#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

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

#14
post #6

The source code of AGC (Apollo Guidance Computer) written by Margaret Hamilton and her team. Clean and obviously it worked well. Comments are also interesting to read. https://github.com/chrislgarry/Apollo-11

That's certainly not x86 code, but loved the link anyway, thanks.

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

#16
post #8

My interest, save for occasional tweak in ARM boot code, is mostly historical. Grew up on Z-80 assembly but really enjoy PDP-11 code for didactic purposes. You can easily see that the rise of optimising C compilers was helped by divergence from PDP instruction set. Not ever since C idioms map down so nicely. strlen(): LEN: MOV #-1, R0 1⊙ INC R0 TSTB (R1)+ BNE 1⊙ or take strcpy(): COPY: MOVB (R1)+, (R0)+ BNE COPY vers…

That's remarkably close to 68k str-functions.

Page 133 http://www.atarimania.com/documents/Asm_Lang_Prog_68K_Family...

  * STRLEN - RETURNS LENGTH OP NULL TERMINATED STRING IN D0
  * A0 -> STRING
  STRLEN: MOVE.L A0,-(SP) SAVE REG
          CLR.L  D0       INITIALIZE
  STRLENI:TST.B  (A0)+    NULL?
          BEQ    STRLENR  YES, RETURN
          ADDQ.L #1, D0   BUMB COUNT
          BRA    STRLENI  LOOP
  STRLENR:MOVE.L (SP)+,A0 RESTORE REG
          RTS

  We might also want to copy a string:
  
  * STRCPY - COPY A NULL TERMINATED STRING
  * A0 -> SOURCE STRING
  * A1 -> DESTINATION STRING
  STRCPY: MOVEM.L A0-A1,-(SP) SAVE REGS
  STRCPY1:MOVE.B  (A0)+,(A1)+ MOVE A BYTE
          BNE     STRCPY1     GET ANOTHER IF NOT NULL
          MOVEM.L (SP)+/A0-A1 RESTORE REGS
          RTS
  
  Next, we will want to compare two strings:
  
  * STRCMP - COMPARE TWO NULL TERMINATED STRINGS
  * A0 -> STRING 1
  * A1 -> STRING 2
  STRCMP: MOVEM.L A0-A1,-(SP)  SAVE REGS
  STRCMP1:CMPM.B  (A0)+,(A1)+  COMPARE BYTES
          BNE     STRRET       RETURN IF DIFFERENT
          TST.B   -1(A0)       HAVE WE HIT A NULL?
          BNE     STRCMP1      NOW MORE BYTES LEFT
  STRRET: MOVEM.L (SP)+,A0-A1  RESTORE REGS
          RTS
Although I guess I'd implement strlen more like this (not sure if it works, been a long time since I last wrote anything for 68k):

  strlen:   movem.l a0-a1,-(sp)
            move.l  a0, a1   ; copy a0 to a1
  slenloop: tst.b   (a0)+
            bne     slenloop
            addq.l  #1, a1
            sub.l   a0, a1
            move.l  a1, d0
            movem.l (sp)+,a0-a1
            rts
Just two instructions in the inner loop. But not sure whether address registers supported addq etc.

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

#17

This is not x86 but certainly an optimised code https://www.pagetable.com/?p=774

Pretty incredible that such world changing code fits on one page.

It’s ineresting that when tools are new, like ASM in 1978, they give high leverage to the first to use them. Microsoft was able to leverage a small amount of code into a world changing platform. Now it would be nearly impossible to do the same with a team the same size.

But in 2018, the nascent state of ML tools looks similar to the nascent state of programming tools in 1978. And indeed we are seeing entire companies built around relatively basic AI in the scheme of things. As first movers these companies have the same kind of leverage with respect to AI that Microsoft did to Software in the 1980s.

Perhaps in 2058 someone will share a link to a Tensorflow script and we will all marvel at its terseness and apparent simplicity.

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

#18
post #16
post #8

My interest, save for occasional tweak in ARM boot code, is mostly historical. Grew up on Z-80 assembly but really enjoy PDP-11 code for didactic purposes. You can easily see that the rise of optimising C compilers was helped by divergence from PDP instruction set. Not ever since C idioms map down so nicely. strlen(): LEN: MOV #-1, R0 1⊙ INC R0 TSTB (R1)+ BNE 1⊙ or take strcpy(): COPY: MOVB (R1)+, (R0)+ BNE COPY vers…

That's remarkably close to 68k str-functions. Page 133 http://www.atarimania.com/documents/Asm_Lang_Prog_68K_Family... * STRLEN - RETURNS LENGTH OP NULL TERMINATED STRING IN D0 * A0 -> STRING STRLEN: MOVE.L A0,-(SP) SAVE REG CLR.L D0 INITIALIZE STRLENI:TST.B (A0)+ NULL? BEQ STRLENR YES, RETURN ADDQ.L #1, D0 BUMB COUNT BRA STRLENI LOOP STRLENR:MOVE.L (SP)+,A0 RESTORE REG RTS We might also want to copy a string: * STRC…

Wow it's close indeed, even stack manipulation looks the same. Would be

  MOV  R0, -(SP)
  ....
  MOV  (SP)+, R0
for the PDP code.

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

#19
post #7

IBM PC x86 BIOS code: https://github.com/kaneton/appendix-bios OT: This brings back memories of tinkering with the MS-DOS boot process. Back then, the BIOS would read the MBR and copy its contents to 0x7C00 and start execution from there. So you could assemble your own code (using no less than MS-DOS debug) and plonk it into the MBR. I remember doing things like fooling the boot loader into thinking there's less ram…

What was the advantage of this over 'Terminate and Stay Resident'?

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

#20
Well, not written in assembly but uses x86 intrinsics:

https://github.com/leni536/fast_hilbert_curve

I'm pretty proud of it. It calculates the coordinates of the nth point on the hilbert curve. No loops, no branches. It uses the pext, pdep and popcount intrinsics creatively.

Post reply on HN