News for hackers. Stuff that matters.
Re:Learning to Read X86 Assembly Language by Annatar on Monday November 28, 2016 @02:59AM (#13054065)
And, of course, modern compilers will usually produce faster, more optimized code than you ever could, without making any mistakes.

This assertion comes up over and over again in the last 30 years. Every time I've had it asserted to me, it always came from non-assembler programmers, who always wrote in a high level language. I have yet to see evidence of optimizing compilers generating code even remotely close in efficiency to what we would code directly in assembler.

A coder would never write all those extra frame pointer setup code, nor would they waste encoding and tact cycles shuffling values from one register to another. For example, a human might write the code from the article thus:

  Add42: addb $42, %al
         ret
and that's it. No frame pointer or stack setup, that's all unnecessary overhead because compiler algorithms can't reliably make such contextual decisions.
Re:Learning to Read X86 Assembly Language by xxs on Monday November 28, 2016 @07:41AM (#13055018)
>>because compiler algorithms can't reliably make such contextual decisions

They can actually. Compiler optimizations have come long way, even Java's JIT should be able to optimize that. (ok, not using the AL register)

My personal story - I used to use exclusively assembler for 6502 and 8086 as it actually ran fast enough. In the mid 90s I saw Delphi's code (and Delphi was not known for its optimizations) but it was able to use the Pentium instruction pairing which takes quite an effort to accomplish by hand.

While beating an old compiler was easy it was the time the compilers began making strides rivaling humans.

Still, hand written inner loops in Assembly might yield some performance (iirc, grep still relies on) but overall there are very limited amount of settings where there would be significant difference... to warrant the effort (incl. correctness and [micro]benchmarks)

Re:Learning to Read X86 Assembly Language by Annatar on Monday November 28, 2016 @10:09AM (#13055844)
My personal story - I used to use exclusively assembler for 6502 and 8086 as it actually ran fast enough. In the mid 90s I saw Delphi's code (and Delphi was not known for its optimizations) but it was able to use the Pentium instruction pairing which takes quite an effort to accomplish by hand.

But a human would almost never use some of those more complex instructions, for a very simple reason: they eat too many clock cycles. When one is coding in assembler, one usually targets two constraints:

1. the least amount of clock cycles needed to pull off an operation;

2. the least amount of bytes to encode the operation.

Where those two meet is where the best coders get unbelievable performance out of the hardware. At least that's the case in the demo scene, although many nowadays cheat by banging the GPU's in CUDA or OpenGL.

Re:Learning to Read X86 Assembly Language by aap_ on Monday November 28, 2016 @04:25AM (#13054345)
Why couldn't they though? Doesn't sound very hard to only generate the wordy prologues and epilogues when necessary (i.e. when you have to save any registers). Why they apparently don't do this is another question then.
Re:Learning to Read X86 Assembly Language by Annatar on Monday November 28, 2016 @12:19PM (#13056831)
I'm not aware of a general algorithm which is capable of deciding whether and how many processor's registers to use instead of setting up frame and stack pointers, and pushing a variable number of arguments on the stack, are you?

A human will know while coding in assembler, at any given time, how many variables are in the game; and will almost always manage to fit them all within processor's registers; There were only two times in my life where I actually had more than eight variables within a subroutine and had to use the stack, and even then, I didn't push everything, but only as many registers as I was actually coming up short, and the rest I still stuffed in the available registers. The other time, I figured out a more efficient algorithm where I could fit everything within the seven general purpose address registers (a0 - a6, since a7 is the stack pointer). A human will also know whether the expected result is within a byte, word, longword, or quadword range, and will only use those instruction and register sizes; a compiler has no chance to figure that out. It's trivial for humans, but as far as I'm aware, impossible for a general compiler algorithm.

In fact, even the best optimizing compilers are so dumb, that one is not allowed to mix and match 8-, 16-, 32- or 64-bit code; one must either compile everything 32- or 64-bit (the linker won't let one link 32- and 64-bit object code together). A human could easily write correct assembler code using all of those instruction / register sizes at once, and we often do.

I have yet to see a compiler capable of inferring that. If you know of one, please show me the generated code. I'd love to use such a compiler.

Re:Learning to Read X86 Assembly Language by RX14 on Monday November 28, 2016 @07:17AM (#13054920)
Bear in mind that this isn't a release build, so the generated assembly will not be optimized at all.
Re:Learning to Read X86 Assembly Language by Annatar on Monday November 28, 2016 @12:27PM (#13056893)
There isn't really much more to optimise: at higher optimization levels, the compiler will figure out whether that was a one time operation or not, and if he determines that it was, it will simply hardcode:

  moveq $52, %eax
but all the extra cruft with stack and frame pointer setup will remain unchanged, and will still be there, if only to comply with the ABI calling conventions. I guesstimate that there are up to 50 clock cycles used for each setup and teardown of the stack and frame pointers; now multiply that with the number of times a function is called, and you can easily waste hundreds of thousands, or even millions of clock cycles pretty much doing no useful work, just housekeeping.
Re:Learning to Read X86 Assembly Language by RX14 on Tuesday November 29, 2016 @08:35AM (#13062912)
Most crystal programs make heavy use of inlining, at least from my experience profiling crystal programs, which removes the whole overhead entirely.
Re:Learning to Read X86 Assembly Language by Annatar on Tuesday November 29, 2016 @01:41PM (#13065165)
Then you end up with huge code sizes, where calls like

  jsr ScrollRaster(pc)
end up repeating the ScrollRaster code over and over and over again, basically leading to macro expansion. And depending on the processor, your code might end up being too large to fit into the instruction cache... and you just kissed instruction burst mode bye-bye. This is unlikely to be the case on modern 80x86 processors as they have staggering amounts of cache, but who knows what embedded platforms and targets people are targeting this very moment as I write this, and who knows how small the instruction and data caches might be on those.
Re:Learning to Read X86 Assembly Language by RX14 on Tuesday November 29, 2016 @05:43PM (#13066844)
It's LLVM's choice when it inlines, I'm sure it has some fancy heuristics.
Re:Learning to Read X86 Assembly Language by rasz_pl on Monday November 28, 2016 @10:36PM (#13060572)
-O2 will remove stack pointer setup
Re:Learning to Read X86 Assembly Language by Annatar on Tuesday November 29, 2016 @01:43PM (#13065182)
You have to have stack and frame setup if the code is to be ABI / target platform's calling convention compliant. Which compiler are you referring to, because GCC won't remove it, as far as I could tell looking at the generated assembler code?

You could purposely tell GCC to omit the frame pointer -fomit-frame-pointer, but then you just made the code extremely difficult to debug (and stack setup code will still be generated, as the compiler can't generate useful / functional code without it), so that's no solution either.

Re:Learning to Read X86 Assembly Language by rasz_pl on Tuesday November 29, 2016 @11:21PM (#13068470)
you dont need to setup local stack if your code doesnt use stack
Re:Learning to Read X86 Assembly Language by Annatar on Thursday December 01, 2016 @04:34AM (#13078262)
Please provide instructions on how to reproduce / verify the assertion that GCC doesn't generate stack / frame pointer setup. I want to see this for myself.
Re:Learning to Read X86 Assembly Language by olegolegovich on Friday December 02, 2016 @11:28AM (#13089918)
The x86-64 ABI does not require frame pointer setup: https://godbolt.org/g/10URYW
Re:Learning to Read X86 Assembly Language by Annatar on Saturday December 10, 2016 @08:45AM (#13145605)
That's a contrived example of inlining; now watch what happens when you provide more than just a trivial function which can be inlined; look at all the futzing it does with the stack:

https://godbolt.org/g/tjrKNO

without explicit direction from humans, compilers can't generate code which calls subroutines or functions without using the stack at the very minimum, and that was my point.

And that's just GCC on intel; check out intel's compiler, it's even worse (click on "turn off intel syntax", and "compile to binary and disassemble the output"):

https://godbolt.org/g/IjGdAP

...and if you try other processors as well, it just gets worse and worse:

https://godbolt.org/g/DZDLju

https://godbolt.org/g/s47tlR