Live data from Hacker News

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

news.ycombinator.com

21–30 of 93 posts

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

#21

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 pr…

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

What code are you looking at? I see "Microsoft BASIC for 6502 Original Source Code" which runs for 6955 lines. By my reasoning that is more than a hundred pages.

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

#22
post #21

Earlier quoted context omitted.

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 pr…

> Pretty incredible that such world changing code fits on one page. What code are you looking at? I see "Microsoft BASIC for 6502 Original Source Code" which runs for 6955 lines. By my reasoning that is more than a hundred pages.

Ok, “one page” may not be a technically fair description (though it does fit on a web page). Still, I think most programmers would agree that nowadays, few world changing technologies can be expressed in 6955 lines of code. That’s what I mean by high leverage.

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

#23
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`?

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

#24
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`?

I don't know much Assembly, but at what point does Fibonacci end? Shouldn't it just keep going?

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

#25
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

Having barely any assembly experience, and looking up what xadd means[0] it really is quite a nice little snippet of code.

[0] https://stackoverflow.com/a/30131285

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

#26

Earlier quoted context omitted.

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`?

I don't know much Assembly, but at what point does Fibonacci end? Shouldn't it just keep going?

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

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

#27
post #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'?

Maybe stealthiness? TSRs could be viewed with the right tools, IIRC. This seems like a technique to hide code?

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

#28
post #26

Earlier quoted context omitted.

I don't know much Assembly, but at what point does Fibonacci end? Shouldn't it just keep going?

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

But rcx has not been initialised.

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

#29
post #18
post #16

Earlier quoted context omitted.

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.

My faster (?, see [0]) strlen seems to compile (using MIT 68k syntax):

  > cat > test.asm
  strlen:   movem.l %a0-%a1,-(%sp)
            movl    %a0, %a1     ;# copy a0 to a1
  slenloop: tst.b   (%a0)+
            bne.b   slenloop
            addq.l  #1, %a1
            sub.l   %a0, %a1
            move.l  %a1, %d0
            movem.l (%sp)+,%a0-%a1
            rts
  ^D

  > m68k-linux-gnu-as test.asm && m68k-linux-gnu-objdump -d a.out
  a.out:     file format elf32-m68k
  
  Disassembly of section .text:
  
  00000000 :
     0:	48e7 00c0      	moveml %a0-%a1,%sp@-
     4:	2248           	moveal %a0,%a1
  
  00000006 :
     6:	4a18           	tstb %a0@+
     8:	66fc           	bnes 6 
     a:	5289           	addql #1,%a1
     c:	93c8           	subal %a0,%a1
     e:	2009           	movel %a1,%d0
    10:	4cdf 0300      	moveml %sp@+,%a0-%a1
    14:	4e75           	rts
I wonder whether it actually works... I think same idea should be translatable to PDP-11 as well.

[0]: It runs slower for short strings, though. Not sure where the break even point is.

Post reply on HN