Live data from Hacker News

Creating a language using only assembly language

speakerdeck.com

51–60 of 67 posts

Re: Creating a language using only assembly language

#51
post #11

Step 1: Make a language high-level enough to build a parser without being a masochist. Step 2: Make a lisp. Step 3: Do it all in lisp. Step 4: Profit. Even Amber( https://github.com/nineties/amber ) and its syntax looks a lot like lisp concept of grammar. But instead of (s expressions) you've got what is very similar to M[expressions] ( https://en.wikipedia.org/wiki/M-expression ). It's amazing what you can with Lisp…

Absolutely. Two languages that anybody who wants to truly understand computing, programming languages, and compilers are Lisp and Forth.

- Lisp is a fundamental, extensible, simple, unbounded AST system (and brings in interesting lambda calculus).

- Forth is a fundamental, extensible, simple, unbounded expression evaluator.

From there, the world is yours.

(Note I use "simple" in Rich Hickey's sense of simple vs easy)

Re: Creating a language using only assembly language

#52

Earlier quoted context omitted.

I used to do some assembly on 68000, 6502 and some Z80. It's nice to have those simpler CPUs and the simpler hardware to learn an assembly language on.

I did 6502 in high school from a book and later did 6809 (EE class) and 8088 in college. The funny part was our compiler target for the CSci Compiler Course was an IBM 370 on which they taught the required assembler course. Of that group I liked the 6809 best. The 370 was ok and it sure had a lot more registers. We didn't write our compilers in assembly, but instead used the department's chosen language: Modula-2. It…

NB: Just a comment, could not (yet) watch the video. Our course was very simple with 8051 compiled with IAR, IIRC. I hated the experience: quite a lot of logic is backwards and you have to come up with something like ABI in order not to make huge mess.

Anyway, this actually skyrocketed my understanding of C: C is nothing more than (rather thin, I would say) wrappers around assembly, which in turn is wrappers around machine instructions. That's why C is fast. That's why you cannot have first-class functions, return multiple (compile time unknown) values, etc..

But nothing beats spending 10+ hours debugging simple I2C (or SPI, can't remember) baremetal ARM program, first weeding out vendor libs, then actually diving into assembly only to find out that CPU is buggy.

Re: Creating a language using only assembly language

#53

Earlier quoted context omitted.

I did 6502 in high school from a book and later did 6809 (EE class) and 8088 in college. The funny part was our compiler target for the CSci Compiler Course was an IBM 370 on which they taught the required assembler course. Of that group I liked the 6809 best. The 370 was ok and it sure had a lot more registers. We didn't write our compilers in assembly, but instead used the department's chosen language: Modula-2. It…

NB: Just a comment, could not (yet) watch the video. Our course was very simple with 8051 compiled with IAR, IIRC. I hated the experience: quite a lot of logic is backwards and you have to come up with something like ABI in order not to make huge mess. Anyway, this actually skyrocketed my understanding of C: C is nothing more than (rather thin, I would say) wrappers around assembly, which in turn is wrappers around m…

This is similar to what I experienced, sort of like an enlightenment moment. In C, I see a thin veneer over assembly, but that veneer has been designed to look thicker than it actually is. I came to appreciate the abstraction and now understand why it's so long lived.

Re: Creating a language using only assembly language

#54

Ok. I'll toot my own horn. In ~1983 I wrote the first iteration of Sensible Solutions for O'Hanlon Computer Systems using MASM. Of course, it had to run in less than 64k. Basically, it was a VM that would run pseudo code. And, the compiler was written in ASM also. In 1985 I wrote TAS, again using MASM. Again, a VM. The language it compiled (in both cases) was written to create business accounting applications, which…

Now there is a language I haven't heard about in a very long time. I actually briefly worked for a company that developed a whole system with TAS, that they then sold to optometrists.

Re: Creating a language using only assembly language

#55
post #9

Speaking of languages in assembly. Here is a Java virtual machine in assembly language: https://github.com/k1w1/javelin-stamp/blob/master/asm/javeli... Until I looked at it again now I had forgotten my favorite instruction, a nop to avoid a bug in the silicon: https://github.com/k1w1/javelin-stamp/blob/master/asm/javeli...

That's for the SX microcontroller.

Re: Creating a language using only assembly language

#57
post #11

Step 1: Make a language high-level enough to build a parser without being a masochist. Step 2: Make a lisp. Step 3: Do it all in lisp. Step 4: Profit. Even Amber( https://github.com/nineties/amber ) and its syntax looks a lot like lisp concept of grammar. But instead of (s expressions) you've got what is very similar to M[expressions] ( https://en.wikipedia.org/wiki/M-expression ). It's amazing what you can with Lisp…

Parsing LISP in assembly isn't even so hard. Without a lot of syntactic symbols, you're just finding whitespace and parens. Even syntactic sugar like ' for quote and , or @ in macros are pretty simple to parse compared to C or -- code gods forbid -- C++. What I'd dread is writing the garbage collector in assembly. Maybe you could rely on reference counting, but that could still be a mess.

That only works if you don't have self referential structures though.

(Disclaimer: I've never actually used one /or/ seen an example of one being used... (See, those AI Koans do have useful tidbits of knowledge in them!)).

Re: Creating a language using only assembly language

#58
post #11

Step 1: Make a language high-level enough to build a parser without being a masochist. Step 2: Make a lisp. Step 3: Do it all in lisp. Step 4: Profit. Even Amber( https://github.com/nineties/amber ) and its syntax looks a lot like lisp concept of grammar. But instead of (s expressions) you've got what is very similar to M[expressions] ( https://en.wikipedia.org/wiki/M-expression ). It's amazing what you can with Lisp…

Parsing LISP in assembly isn't even so hard. Without a lot of syntactic symbols, you're just finding whitespace and parens. Even syntactic sugar like ' for quote and , or @ in macros are pretty simple to parse compared to C or -- code gods forbid -- C++. What I'd dread is writing the garbage collector in assembly. Maybe you could rely on reference counting, but that could still be a mess.

> What I'd dread is writing the garbage collector in assembly.

There are very simple garbage collectors out there, GC doesn't necessarily mean generational mark-sweep-compact or whatever. Semispace collectors are pretty simple and don't even need a stack: https://en.wikipedia.org/wiki/Cheney%27s_algorithm

Re: Creating a language using only assembly language

#60
post #11

Step 1: Make a language high-level enough to build a parser without being a masochist. Step 2: Make a lisp. Step 3: Do it all in lisp. Step 4: Profit. Even Amber( https://github.com/nineties/amber ) and its syntax looks a lot like lisp concept of grammar. But instead of (s expressions) you've got what is very similar to M[expressions] ( https://en.wikipedia.org/wiki/M-expression ). It's amazing what you can with Lisp…

Yeah, it's kind of a cheat. A real language built in assembler would be headaches and I bet a HLL prototype would be hidden somewhere. HLA might be doable, though, especially if macro's are used diligently for pre-optimization work. Competition has gone down from the 70's and 80's, though. I remember reading a Scheme paper that builds a software interpreter then implements the thing in hardware too. There were also p…

It's not that hard to write a "real language" compiler in assembler, lots of people have done so. Amiga-E is a good example for which source code is available.

A simple, Wirth-type compiler is very straight-forward to write for most simple ALGOL-family languages.

By Wirth-type compiler, I mean recursive descent parser with direct code generation (no AST). Single pass if the language allows it.

You need a handful of utility functions for lexical analysis/tokenization, symbol table management, and for outputting code, and then most of the rest is calling subroutines and simple control flow.

In fact, some parts, like code generation, can be made very simple in assembler, because you can directly inline code fragments and in effect use them as "templates".

Post reply on HN