3 ms·
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 P
by rep_lodsb 2y 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;
- boffinAudio 2y agoThanks 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.
- g15jv2dp 2y agoI 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.)
- rep_lodsb 2y agoYou might want to compare it to the "proper" version of the inline asm code, from this comment: https://news.ycombinator.com/item?id=40703314 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)
- gpderetta 2y agoI believe that MSVC inline asm allows referencing variables in the asm as it can parse and understand the asm (at least before they got rid of inline asm completely for 64 bit code). AFAIK GCC does not attempt to parse the asm by design, as it is meant to be used for code that the compiler might not understand, so you have to describe input, outputs and side effects with annotations.
- pjmlp 2y agoThat isn't proper C, rather GCC and clang dialects of inline Assembly.
- boffinAudio 2y agoI 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 ..
- pjmlp 2y agoNot only Turbo Pascal, this more sane approach to inline Assembly was quite common in the PC world compilers, regardless of the programming language.
- pajko 2y agoInline assembly also has support for symbol names, although the native symbols could not be accessed directly but in a bit awkward way. https://stackoverflow.com/questions/32131950/assembler-templates-asmsymbolicname-and-error-expected-string-literal https://stackoverflow.com/questions/32131950/assembler-templ...