Monday, 3 September 2018

To Swap or not to Swap that is the question.....

On changing a language which already has code written for it

Having written a few bits of code in M8, I'm beginning to wonder if I've made a mistake.  I've written a Console I/O module, a Text-loading module for debugging to easily load in Color text* and most of a colourising screen editor.

I originally changed the language design so that the "stack pushing" so to speak was manual ; so to calculate 2 + 3 you would do 2 , 3 +

I think perhaps for readability the old system worked better. This was based around the concept of a loader ; constants, load variables and load addresses (and nothing else) all did a A<->B swap prior to loading their value into A.

So 4, rather than being simply ld hl,4 , was ex de,hl ; ld hl,4.  This had a slight problem in that there was a tendency to produce too many ex de,hl instructions, but it did actually make the code look clearer. Current code seems to be full of commas and you can't really read it.

M8 , especially with this, really does look like FORTH. The only time it matters is when you get loops where you are manhandling two values, and you need to track what is in A and what is in B.

But even then, this is much simpler than remembering what is where on the stack.

Colouring

One thing I didn't mention ; this system has two compilers. The first is a cross compiler in Python, which already exists, which generates code from ASCII text that looks like FORTH : increment 1 + ; ; the other is an interactive one that will run on the actual target uses the color format increment 1 + ;

The interactive one is extensible in the way FORTH is, in that you can add new loops and so on ; to do this in the Python one you have to write them in Python

Platform

I can finally switch back to Linux :) Zesarux has now reached a stage where it is useable as a development target viz you can run Spectrum SNA files from the command line without going through 27 button presses each time.

The windows only CSpect emulator (these are the only two working Spectrum Next emulators) sort of works under Wine (the cursor keys don't seem to work ....) but it is the only one with a decent debugger.

Sunday, 2 September 2018

A little bit of M8

So, some actual code from the console library I wrote yesterday.

: con.print.digit private
    , 15 and , 10 >= if 7 + swap then 48 + con.emit    
;


What this bit of code does is to take the value passed in A and print out 0-9a-f based on the lower 4 bits of A.

, 15 and

, is a bit like Enter in RPN, it swaps A and B. This puts A into B, 15 into A and then ands B into A.

, 10 >=

is very similar, it puts the anded value into B, and 10 into A. >= sets A to -1 if B >= A and 0 otherwise. This is deliberately the wrong way round so it is FORTH like, so to calculate 3 - 2 you do 3 , 2 - (M7 allowed you to just write 3 2 - which made it even more Forth like but it was a bit inefficient)

if  7 + swap then

If tests the current value of A, so if true (e.g. >= 10) then it puts 7 in A, adds B into A and puts the result back in B - 7 is the offset to display hex in ASCII correctly.

Note that neither >= or if is destructive of the value in B.

48 + con emit 

The value is still in B, possibly modified by the previous instructions. 48 is put into A, B is added into A, giving the ASCII value in A, which is then printed using con.emit

;

Compiles ret.

It looks a bit horrible but it works quite well once  you are used to it. Rather than remembering what's on the stack, you have to remember what is in A and B (and sometimes, the top of the return stack, which is used as an interim store). 

When I first wrote an early version I thought this would lead to an explosion in variables (replacing stack based elements) but this doesn't seem to have happened.

It gains its efficiency from not having a data stack which is changed all the time , even when you aren't really doing anything. The call stack stays as the call stack.  Nothing changes unless you ask it to, so you can use all kinds of operations on A and B just stays as it is, as in this example.

Saturday, 1 September 2018

Retrochallenge September 2018

This is my blog for Retrochallenge for September 2018. I plan to develop and implement code for my M8 language.

M8 is a language I devised with this machine in mind, the Spectrum Next, though it should be convertible to any Z80 based machine or indeed pretty much any 8 or 16 bit machine.

It is neither a high level language, or a low level one ; it belongs in the middle somewhere. It's closest ancestors are Forth and Colour Forth, and it does look very much like Forth ; but it's much closer to the Z80 hardware than Forth is.

It has a github at https://github.com/paulscottrobson/m8 ; which already contains a runtime and a simple cross compiler.

This was partly done yesterday (tut tut....) but mainly exists because a fair chunk of the code base has been borrowed from its predecessor, unsurprisngly called M7.