Programming language implementation ...

Basic and Machine Language

Moderator: Moderators

Bobbi
Vic 20 Afficionado
Posts: 355
Joined: Thu Oct 13, 2016 11:35 am
Location: Toronto
Occupation: Programmer

Programming language implementation ...

Post by Bobbi »

Just supposing I was crazy enough to write a new programming language in 6502 assembler ... what should I have a go at?

Another BASIC is the obvious choice. I like the idea of a simple integer BASIC (like Woz's Apple Integer BASIC but coded by someone with 1/10 the talent!) CBM BASIC does everything as floating point, which must contribute to its slowness! I also like the idea of a BASIC that could allow at least some modularity - procedures with local variables perhaps? It would be nice not to have line numbers and to use labels instead.

Alternatively, what about FORTH? FORTH code runs fast on small machines. There is already VIC-FORTH of course, and I expect it is way better than what I would do!

Interpreters are obviously feasible, but how about some sort of compiler. That may be too ambitious - perhaps interpret first and then see if I learn enough to write a compiler for version 2!!

What other choices are there? Implementing this is definitely a long term project (months of spare time!) It needs to be a language that is reasonably simple given I will be writing it all in asm!
User avatar
Mike
Herr VC
Posts: 4816
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Programming language implementation ...

Post by Mike »

Implementing this is definitely a long term project (months of spare time!) It needs to be a language that is reasonably simple given I will be writing it all in asm!
Brainfuck maybe? Should be easy enough to implement in a reasonable amount of time. :)

And then there's CAL. A "toy" language I first implemented in 1985, in BASIC, and then reconstructed from my memories and some notes I took at that time, in 2012. :mrgreen:
Bobbi
Vic 20 Afficionado
Posts: 355
Joined: Thu Oct 13, 2016 11:35 am
Location: Toronto
Occupation: Programmer

Re: Programming language implementation ...

Post by Bobbi »

Brainfuck would be easy to implement. I am looking for something a bit more challenging :)

I guess the background to this project is that I have long had the idea to build my own software environment 'from scratch' without building on anyone else's code. For this reason I will probably re-invent low level functionality (such as the screen driver) rather than using CBM functions like STROUT. So far I have implemented double-precision integers, screen driver for text mode and a hi-res mode, but I haven't started on the language interpreter itself. I need to form a better idea of where I am going in my head before I start pounding out more code (fun though that is!)
User avatar
srowe
Vic 20 Scientist
Posts: 1325
Joined: Mon Jun 16, 2014 3:19 pm

Re: Programming language implementation ...

Post by srowe »

I found implementing FORTH a very good learning exercise. I discovered a lot about the language but also improved my understanding of assembler.
User avatar
Mike
Herr VC
Posts: 4816
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Programming language implementation ...

Post by Mike »

I have entertained suchalike ideas often enough, and my current point of view is that writing a complete new programming environment for the VIC-20 simply isn't worth the time and hassle.

As I wrote in another thread, a file-based [1] symbolic assembler and a (40 column) ASCII text editor, both being able to build themselves would constitute a minimum environment which could be built upon. I wouldn't simply disregard the huge library of code available in both BASIC interpreter and KERNAL. Especially when you're going to do file I/O.

Using that library of course requires one deep look into the ROM listings, inferring from those the routines eligible for use in own programs, and how registers, flags and memory need to be set up to properly use them. That knowledge also comes handy for designing language extensions for BASIC, tools to improve upon the standard IEC protocol, and so on.

We had similar discussions here in Denial every now and then, when some people thought they could somehow transcend the limits of the VIC-20 hardware by first designing and then writing their own OS+language.

Without exception, all those projects ended up as non-starters. :(


[1] i.e. source and target are files on the drive, the RAM is only used to hold the symbol table
Bobbi
Vic 20 Afficionado
Posts: 355
Joined: Thu Oct 13, 2016 11:35 am
Location: Toronto
Occupation: Programmer

Re: Programming language implementation ...

Post by Bobbi »

I am under no illusions - it would be a huge amount of work to replace the system ROMs.

I agree with you in general that it is better to use the KERNAL and BASIC routines - not least because they save precious RAM. However part of my motivation is to learn how to build the low level routines, which means making the hard decisions about how much of page zero to use up etc. ;)

Even if I do my own math routines etc., I would probably still use KERNAL for any file I/O because that would be very tedious to reimplement! (That means I can't trash zero page with abandon of course!)

I have considered your previous suggestion of editor + assembler. It is still a candidate, if I don't settle on some sort of interpreted language. One thing that put me off the idea of a native assembler on the VIC was a quick look at just how many labels I define in typical code. There is no way I could assemble my own source files on the VIC without blowing up memory with the symbol table (even if many of them are just handy defines for things that I don't actually end up using.) A 'simple assembler' is doable, but I can't see myself using it when I have xa65 on Linux!

I also agree that "it's not worth the effort" in any objective sense. However I am looking for a 6502 assembly project to tinker with and I think I would learn a lot from trying to accomplish some of these things.
Bobbi
Vic 20 Afficionado
Posts: 355
Joined: Thu Oct 13, 2016 11:35 am
Location: Toronto
Occupation: Programmer

Re: Programming language implementation ...

Post by Bobbi »

Just looking at VIC-FORTH and I have to say it looks very impressive!
User avatar
Mike
Herr VC
Posts: 4816
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Programming language implementation ...

Post by Mike »

Bobbi wrote:I have considered your previous suggestion of editor + assembler. It is still a candidate, if I don't settle on some sort of interpreted language. One thing that put me off the idea of a native assembler on the VIC was a quick look at just how many labels I define in typical code.
In typical code intended to run on the VIC-20 or for other machines?

I just can't imagine how one would go to fill up the RAM with a table of _thousands_ of symbols for a 'typical' VIC-20 assembly project. The EDITOR component of MINIPAINT, for example, consists of roughly 50 procedures, each with around 10..15 Labels (the biggest fn having 27 labels including the entry point), also some other labels designating data areas and state information blocks.

That gives a rough estimate of 500..700 symbols, each with a 16 bit value (2 bytes) and maybe 20 letters for each symbol + maybe 2 bytes for a link pointer, etc. and we'd end up at ~16K symbol information. Roughly twice the size of the resulting object code (which is 7036 bytes, not including the 2 bytes for the load address). For a bigger project on this proposed environment, one would possibly go and shorten the symbol length a bit and/or assemble the project in parts, implying a linker.

Well...
However I am looking for a 6502 assembly project to tinker with and I think I would learn a lot from trying to accomplish some of these things.
If you start out with the combo OS+language implementation, that would be like "want to run" before "learn to walk".

I suppose the combo assembler+editor - given BASIC and KERNAL - has the charme of a self-hosted environment. If you follow through with this one.

Or some graphic tool, something for the sound, some file manipulation tool, data acquisition over the user port - enough to try out and explore. Directly calling the arithmetic routines in the BASIC interpreter from machine code is good for a 3x speed-up when all the token dispatch, constant evaluation, operator priority stack handling and variable search can be eliminated! Etc.

It's not even necessary to write the whole program in machine code. Most often, identifying the time critical, compute bound parts and rewriting them in assembly, leaving the I/O bound parts in BASIC is entirely sufficient. Using that method of course requires some knowledge how to interwork BASIC and machine code efficiently (and without the ML getting into the gears of BASIC). :)

...

And really, all the thinking can't beat a working, testable prototype. Strangely enough, those people I mentioned earlier, with all their great plans, were not able or willing to put out a single _working_ program that others could try out and comment upon...
Bobbi
Vic 20 Afficionado
Posts: 355
Joined: Thu Oct 13, 2016 11:35 am
Location: Toronto
Occupation: Programmer

Re: Programming language implementation ...

Post by Bobbi »

I guess if one assembles from reading the source from disk file to writing generated code to disk file then there is plenty of RAM for just the symbols, you are right.

I am currently on a tangent playing with VIC-Forth. Maybe that will give me some ideas about the direction to take!

I am also reading the KERNAL and BASIC disassembly. It is very illuminating!
User avatar
Stormcrow
Vic 20 Enthusiast
Posts: 177
Joined: Mon Dec 24, 2012 9:46 pm
Website: http://trimboli.name
Location: Ronkonkoma, NY

Re: Programming language implementation ...

Post by Stormcrow »

Bobbi wrote:Brainfuck would be easy to implement. I am looking for something a bit more challenging :)
Pleaseohpleaseohplease implement it as Ook!

I'd also like to see Whenever.
Bobbi
Vic 20 Afficionado
Posts: 355
Joined: Thu Oct 13, 2016 11:35 am
Location: Toronto
Occupation: Programmer

Re: Programming language implementation ...

Post by Bobbi »

Then reimplement the ROMs in Ook :)

I have been learning VIC-Forth and I have to say it is not far away from some of those esoteric languages. It starts out pretty reasonable (for an RPN stack language) but gets ugly / incomprehensible really fast. Plus it is very easy to crash the interpreter / compiler with one bad 'word.'
User avatar
srowe
Vic 20 Scientist
Posts: 1325
Joined: Mon Jun 16, 2014 3:19 pm

Re: Programming language implementation ...

Post by srowe »

The important thing with FORTH is to keep each word to a reasonable size. If you break your task up into steps then each step becomes a series of other steps etc. And factoring out common code helps too.

But you're right, it's very much like assembler, you can crash with a simple error like getting two parameters the wrong way around. There's no safety net.
Bobbi
Vic 20 Afficionado
Posts: 355
Joined: Thu Oct 13, 2016 11:35 am
Location: Toronto
Occupation: Programmer

Re: Programming language implementation ...

Post by Bobbi »

It is very easy to crash FORTH messing around with the 'return stack.' Operations have to be balanced or ... bang!!
Bobbi
Vic 20 Afficionado
Posts: 355
Joined: Thu Oct 13, 2016 11:35 am
Location: Toronto
Occupation: Programmer

Re: Programming language implementation ...

Post by Bobbi »

On a slightly different tack, I have been thinking about expression parsing.

First I tried to understand the expression parser in CBM BASIC. Having failed miserably to make much sense of it, I thought I should implement something in C first. I now have a working expression parser using the shunting yard algorithm. It can handle basic math expressions, including parenthesis and it gets precedence correct. I may try and extend it (still in C) to be the core of mini-BASIC of sorts, at least allowing me to define variables and evaluate them in expressions (LET and PRINT basically.)

If my prototyping goes well, I will port the C to 6502 so I can play with it on the VIC. It is much quicker to prototype in C first though for something with a fiddly algorithm like this.

I was going to post the C code here in case anyone is interested, but I am not sure what the correct etiquette is for posting source in this forum (newbie here!) It is 5K of C if anyone is interested.
User avatar
Mike
Herr VC
Posts: 4816
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Programming language implementation ...

Post by Mike »

Just apply the golden rule. Would you like to scroll several pages of a post to see the continuation of a discussion?

At least if you embed the source into code-tags, the resulting text block will use a mono-spaced font, the indentation is honoured, and the block automatically shows only 10 lines or so first - before it is expanded by the viewer who wants to read the code block in full.

And I can't imagine anyone'd complain about a *.zip file containing the source as attachment. The forum software allows for that. :)
Post Reply