How To

An interpreter in nx

1

LowRes Oranges are good 2019-11-20 02:22

Ok a while ago I had an idea to make some kind of interpreter in nx. I wanted to make a BASIC interpreter, but that is WAY harder than you would think! BASIC has a pretty complex syntax. Any programming languages that are easy to make an interpreter for? If not, how could I make one for BASIC?


was8bit 2019-11-20 05:07

I started a machine language emulator...
https://lowresnx.inutilis.com/topic.php?id=281

I haven't finished it because i couldn't find a conclusion i was happy with...


was8bit 2019-11-20 05:13 (Edited)

The commands are one hex digit...

The memory core (16x16) hold both code and data... and in fact it is possible to write code that rewrites itself... as code and data all share the same memory core space... the only thing that determines what is data and what is command is the sequence the processor take in the digits.. all commands and all data use the same numbers (0 to F) then it all depends on when and where in the memory core that the processor is currently at...

Here are the commands as of the last update...

REM SINGLE BIT COMMANDS
REM
REM 0 READ X,Y TO A
REM 1 WRITE A TO X,Y
REM 2 PUSH A TO STACK
REM 3 POP STACK TO A
REM 4 INC A
REM 5 DEC A
REM 6 GOTO X,Y
REM 7 GOSUB X,Y
REM 8 RETURN TO GOSUB
REM 9 OUTPUT: PLAY STACK


REM DOUBLE BIT COMMANDS
REM 1STBIT=COMMAND
REM 2NDBIT = N
REM
REM A WRITE N TO A
REM B WRITE N TO X
REM C WRITE N TO Y
REM D BRANCH TO X,Y IF A>=N
REM E BRANCH TO X,Y IF A<N
REM F BRANCH TO X,Y IF A<>N


was8bit 2019-11-20 05:21 (Edited)

My NX machine code is a much simplified version of real stuff...

Here is actual machine language i worked with in app2e computers...

Exerpt from...

http://www.appleoldies.ca/anix/Using-6502-Assembly-Language-by-Randy-Hyde.pdf

The opcodes (computer instructions) are stored in memory
in a manner identical to data. How then does the computer dif-
ferentiate between data and instructions? Clearly, the meaning
of a byte in memory is very context-dependant. A byte in memory
is assumed to be a computer instruction if the program counter
is ever allowed to "point" at (i.e., contain the address of) that
particular byte in memory. Also, programs are assumed to be
stored sequentially in memory (with some exceptions). That is,
the second instruction immediately follows the first instruction,
the third instruction follows the second, etc.
EXAMPLE:
MEMORY
1st INSTRUCTION
2nd INSTRUCTION
3rd INSTRUCTION
4th INSTRUCTION
5th INSTRUCTION
<- PROGRAM COUNTER
The program counter is loaded with the address of the first
instruction. The processor loads and then executes this instruc-
3-5
*****************************************************************
tion. The program counter is then incremented by one so that it
points to the second instruction. This instruction is fetched and
the cycle is repeated.
Always remember that the computer cannot tell the differ-
ence between data and instructions. Whatever the program
pointer points to will be interpreted as an instruction.
TWO AND 3-BYTE INSTRUCTIONS.
Many instructions require more than one byte. For instance,
suppose we want to load the accumulator with the 8-bit constant
$FF. The 6502 has an instruction which will load the accumulator
with an 8-bit constant. The only problem is how do you specify
the constant? Why not immediately follow the instruction with the
constant! Well, this is exactly what's done. The hex code $A9,
when executed, tells the 6502 to load the accumulator with the
8-bit constant located in the next byte, so the two bytes ($A9,
$FF) instruct the 6502 to load the accumulator with the constant
$FF. Loading the accumulator with a constant (or load the accu-
mulator immediate, as it's often called) is an example of a 2-byte
instruction. Rather than using just one byte to perform the oper-
ation, we need two. Naturally, the program counter is incremented
by two instead of one so that the constant does not get executed
as the next 6502 instruction.
3-6
************************************************
In addition to the 2-byte instructions, there are also 3-byte
instructions. One good example is the "store the accumulator in
an absolute memory location" instruction. This instruction (which
consists of $8D followed by a 16-bit address) will store the con-
tents of the accumulator at any of the 65,536 different memory
locations available in the 6502 memory space. For example,
($8D, $00, $10) will store the accumulator at location $1000, and
($8D, $C3, $48) will store the accumulator at location $48C3.
Remember, whenever a multibyte instruction is encoun-
tered, the program counter is automatically incremented past the
additional data.
EXAMPLE:
A9 INSTRUCTION #1 LOAD ACC WIUH $FF
FF
8D INSTRUCTION #2 STORE ACC AT LOCATION $1234
34
12
-- ETC.
--
--
WARNING
Remember, there is nothing sacred about the location of your
program instructions. The computer cannot differentiate between
data and valid instructions. In the previous example, if the pro-
gram began at location $1234 we would have loaded the accu-
3-7
*****************************************************************
mulator with $FF and then proceeded to destroy the first instruc-
tion ($A9 stored at location $1234) by storing a $FF over the top
of the $A9, leaving you with the following code:
LOC DATA/CODE
1234 FF
1235 FF
1236 8D
1237 34
1238 12
1239 --
ETC. ETC.
With this in mind, be very careful where you store data since
you can easily wipe out your program if you are not careful.


Timo 2019-11-20 07:40 (Edited)

Maybe a stack based language like Forth? I don’t really know it, but someone made this:

https://lowresnx.inutilis.com/topic.php?id=79

The interpreter has a lot of code but is still kind of readable... buuuut also complicated :O

Or you invent you own language, I made some very simple ones. Try a format like:

COMMAND P1,P2,P3,...

Use the string functions to separate the command from the parameters and then store all parameters in separate variables. Then you can “easily” create your own commands. If you reach this point I can tell you some ideas how to create a simple variable system.


LowRes Oranges are good 2019-11-20 16:38

Um idk, maybe I should just make a BASIC interpreter, because it's the most straightforward. I'm already familiar with it! I have already made a brain**** interpreter, so I kinda know what to do. Assembly would be pretty simple, since it's very close to the hardware. Registers and RAM and all that stuff. Making my own language would be pretty cool! What if I made a python or JavaScript interpreter. These are really big projects, but I'm up for it!


was8bit 2019-11-20 17:11

Its fun to at least try :)


LowRes Oranges are good 2019-11-25 20:19

Alright I will do python. See you when I upload! :--)


LowRes Oranges are good 2019-11-25 20:58

This is a dumb question. On assembly, If you had simpler instructions. It's still assembly, just your simpler form. Right?


was8bit 2019-11-26 02:24

Assembly is sorta an english subtitution for the numeric commands... so

A9 INSTRUCTION #1 LOAD ACC WIUH $FF
FF
8D INSTRUCTION #2 STORE ACC AT LOCATION $1234
34
12

So in machine code you simply type

A9FF8D3412

With an assembly program you use

LOAD ACC $FF
STORE ACC $1234

Then the assembly writes this for you

A9FF8D3412

MOST people used an assembly program so they could use the english equivalent and then it would write the machine code for you

I didn't have an assembly program, just a raw data editor, so i had to code my programs raw with straight hex numbers... it was time consuming as if i missed one number it would mess up my program and i would have to carefully check my code number by number by number ...


was8bit 2019-11-26 02:29 (Edited)

A9 is the command for LOAD ACC, the 2 hex numbers following it are the value you want to load the ACC with

8D is the command of where in memory do you want to put the ACC value... this needs the 4 hex numbers following the command...

In machine code, you dont have variable names, you simply have memory adress numbers..

So basically you are storing the value $FF into memory adress $1234.. you just cant do it directly

In basic it would look like...

PLAYERSTRENGTH=255


was8bit 2019-11-26 02:35

ACC = accunulator..

An accumulator is a register for short-term, intermediate storage of arithmetic and logic data in a computer's CPU (central processing unit)

You couldnt directly transfer data from one memory address to another, you had to use the ACC ... you put data from code or memory to the ACC, and you could then put data from the ACC to memory... i think this was how the wiring of the computer actually physically worked....


LowRes Oranges are good 2019-11-28 21:48

What about registers? What's the difference between a Accumulator and a Register?


was8bit 2019-11-28 23:22 (Edited)

Memory alone cannot directly connect to other memory, so different registers are used...

The accumulator is the main register and does the simple job of transfering data from memory to memory....


Log in to reply.