← Back to context

Comment by rep_lodsb

3 months ago

Actually more readable than the AT&T syntax :)

But does this work on both GCC and Clang, and is safe from being optimized away? edit: the answer is no

Turbo Pascal had an integrated assembler that could use symbols (and even complex types) defined anywhere in the program, like this:

    procedure HelloWorld; assembler;
    const Message: String = 'Hello, world!'^M^J;  {Msg+CR+LF}
    asm
        mov  ah,$40  {DOS system call number for write}
        mov  bx,1    {standard output}
        xor  ch,ch   {clear high byte of length}
        mov  cl,Message.byte[0]
        mov  dx,offset Message+1
        int  $21
    end;

Not only Turbo Pascal, this more sane approach to inline Assembly was quite common in the PC world compilers, regardless of the programming language.

Thanks for making me extremely sentimental for the hundreds of Turbo Pascal projects I did back in the day - this particular example highlights the elegance and clarity of the language, which we still seem to resist in our modern tooling.

  • I don't really see what's "elegant" about the code, could you elaborate? (This isn't a jab at GP. I'm just curious about what I'm not seeing.)

    • You might want to compare it to the "proper" version of the inline asm code, from this comment: https://news.ycombinator.com/item?id=40703314

      Modern C is neither "low-level" or "high-level". It's defined for an abstract machine where integers can't overflow, null pointers can't be referenced, etc. And unless you follow all the rules, and add proper annotations for things like inline assembly, the compiler is free to do anything to your code.

      The one advantage to this approach is that modern compilers can turn megabytes of auto-generated crap produced by string substitution macros into halfway decent machine language.

      (And I freely admit that specifically Turbo Pascal produced really bad code, worse even than C compilers at the time, but the syntax is oh so much nicer IMHO)

      2 replies →

    • I think its elegant because the distinction between Pascal and Assembly is made using the Pascal asm .. end; keywords, and in that block one can also access the Pascal variables without much fuss involving the assembler.

      I find that really nice to read and to look at, whereas the examples given in the original article are prone to syntax overload, what with all the intermixing - for example, the variable declarations having what 'look' like attributes - but are really assembly instructions, emitted.

      I guess one would have had to have enjoyed writing Turbo Pascal code, though, to see this particular aesthetic. A lot of folks do, some don't ..