News for hackers. Stuff that matters.
Re:KolibriOS – operating system written entirely in assembly... by ajxs on Tuesday April 14, 2020 @10:48PM (#22873962)
Why is this getting downvoted? Writing programs in pure assembly does offer the possibility of implementing very fine-grained optimisations. However, assembly is not a magic bullet. Modern optimising compilers are extremely efficient. The most efficient assembly is quite often not the most legible or maintainable assembly. I've written lots of bare-metal assembly and if I'm given the choice between writing more efficient assembly and writing straightforward assembly, I'll pick the latter every time.

Edit: I have a little bit of experience in the area of operating-system development in assembly. This is purely a hobby project that I work on half-heartedly. Nevertheless, it does demonstrate what I know about bare-metal x86 assembler.

https://github.com/ajxs/hxos

Re:KolibriOS – operating system written entirely in assembly... by projektfu on Wednesday April 15, 2020 @12:28AM (#22874506)
The biggest benefit is usually that you can forgo calling conventions in your own code because everything is visible to you. Of course the code starts looking like spaghetti.
Re:KolibriOS – operating system written entirely in assembly... by ajxs on Wednesday April 15, 2020 @01:02AM (#22874694)
This is correct. You can forego all aspects of your platform's calling conventions if you so desire. Omitting setting up stack frames is a really simple optimisation that can be done in hand-written assembly. You've already touched on what the stakes of doing such things are.
Re:KolibriOS – operating system written entirely in assembly... by jfkebwjsbx on Wednesday April 15, 2020 @06:10AM (#22876130)
Compilers are able to do the same since a few years.
Re:KolibriOS – operating system written entirely in assembly... by projektfu on Wednesday April 15, 2020 @08:06AM (#22876749)
Yes, but they rarely take a whole-program approach. Reducing a complicated sequence of function calls to a few goto’s is not an easy task. Probably NP-hard?
Re:KolibriOS – operating system written entirely in assembly... by BubRoss on Wednesday April 15, 2020 @01:11PM (#22880278)
Doing it on a whole program basis is also unlikely to give much benefit. Function calls are extremely fast, they only show their overhead in tight loops.
Re:KolibriOS – operating system written entirely in assembly... by projektfu on Wednesday April 15, 2020 @06:54PM (#22884120)
Yeah, reviewing the repository there’s a lot of high level language code now, so that probably removes most of the benefit of assembly. You’re still calling into the OS a lot.

Some of the other low level systems (like the Mac) had a trap system that wasn’t so far away in cycle count from user code. But in these days of needing 10,000 cycles to bridge a system call it’s best to do whatever you can to avoid calling the OS.

Re:KolibriOS – operating system written entirely in assembly... by BubRoss on Wednesday April 15, 2020 @09:48PM (#22885269)
Function inlining can't avoid system calls
Re:KolibriOS – operating system written entirely in assembly... by projektfu on Wednesday April 15, 2020 @10:23PM (#22885441)
Exactly my point
Re:KolibriOS – operating system written entirely in assembly... by crispinb on Wednesday April 15, 2020 @03:18AM (#22875358)
> Why is this getting downvoted?

You might as well ask why dogs bark or puzzle with furrowed brow over the croaking of frogs.

Re:KolibriOS – operating system written entirely in assembly... by ludditetech on Wednesday April 15, 2020 @07:18AM (#22876478)
haha
Re:KolibriOS – operating system written entirely in assembly... by m463 on Wednesday April 15, 2020 @08:23AM (#22876865)
I was thinking the same thing.

Then I sort of turned the problem around in my mind.

When is a compiler prevented from optimizing? Maybe pointer aliasing?

Another thing I wondered. I've written hello.c and it's about 84 bytes of C and 8.3k as an executable. Would hand-coded assembler be that large?

maybe it's that compilers CAN do well, but because of requirements, they can't do some things well and necessarily create a lot of "boilerplate infrastructure".

Re:KolibriOS – operating system written entirely in assembly... by jsjohnst on Wednesday April 15, 2020 @09:26AM (#22877421)
> I've written hello.c and it's about 84 bytes of C and 8.3k as an executable. Would hand-coded assembler be that large?

The answer is generally no. A lot of that overhead is due to the C std library. If memory serves from a blog post I read long ago, with very very aggressive tuning, you can get a hello world binary down under 50 bytes (which is smaller than the ELF header). A “normal” ASM coded “hello world” binary could easily be in the range of a few hundred bytes without anything special.

Re:KolibriOS – operating system written entirely in assembly... by ajxs on Wednesday April 15, 2020 @08:18PM (#22884740)
You can use the `-S` switch with gcc to produce the intermediate assembler representation of your C program instead of binary output. From there you can see for yourself what optimisation has been performed and what hand optimisations are possible.

I just compiled my own 'hello.c' on Linux, with the optimisation flag set for 'code size', and the binary weighed in around 8.3k. I then removed the `printf` call and the inclusion of `stdio.h`. The resulting binary was 8.1k. This is exactly as expected. Why would the inclusion of a call to a dynamic library bloat the final binary size?

Re:KolibriOS – operating system written entirely in assembly... by jsjohnst on Wednesday April 15, 2020 @10:37PM (#22885500)
> Why would the inclusion of a call to a dynamic library bloat the final binary size?

Here’s two good links to read on it:

https://www.muppetlabs.com/~breadbox/software/tiny/teensy.ht...

http://timelessname.com/elfbin/

Re:KolibriOS – operating system written entirely in assembly... by m463 on Wednesday April 15, 2020 @02:53PM (#22881548)
I looked a little further.

hello.c: 4 loc, 84 bytes

hello.s: 30 loc, 517 bytes

hello: 8.3k

so it must be data structures for linking.

oh, and if I use -static, hello becomes 844704 bytes. :)

Re:KolibriOS – operating system written entirely in assembly... by wenc on Wednesday April 15, 2020 @12:32AM (#22874524)
Just curious, is pure assembly harder to test for correctness? Are there higher level abstractions like functions?
Re:KolibriOS – operating system written entirely in assembly... by ajxs on Wednesday April 15, 2020 @12:59AM (#22874674)
Programs don't have to be particularly large complex to become relatively unweildy and difficult to understand in assembly. I find it takes me much more time to comprehend large portions of written assembly than higher-level languages. Your mileage may vary. Other engineers might be more talented than I am in this regard, however I think this is more or less everyone's experience.

Abstractions like functions are still present, just not in the way that you might think of them in a higher-level language. You use 'branching' instructions to jump from one part of the code to another. Either by referencing labeled sections of the code to perform 'absolute' jumps, or by jumping 'relative' to the current instruction. This forms the building blocks that can be used to implement more abstract constructs such as loops, functions and conditionals. This is a gross simplification, I hope it helps answer your question though.

Re:KolibriOS – operating system written entirely in assembly... by pjmlp on Wednesday April 15, 2020 @01:58AM (#22874994)
Yes and no.

You can write tests like in any language, they just happen to be more tedious.

If you make use of powerful macro assemblers, like MASM and TASM, you can write what looks like high level functions.

You can have a look here for a couple of MASM examples,

https://de.wikipedia.org/wiki/Microsoft_Macro_Assembler

Re:KolibriOS – operating system written entirely in assembly... by misja111 on Wednesday April 15, 2020 @04:41AM (#22875714)
You can code anything you want in ASM, so higher level abstractions as well. However while doing that you will lose some of the performance gain that ASM gives you.

Optimised ASM can still be tested but one problem is that when the code doesn't work, it can simply crash. This makes it a pain to debug.