From: Fabrizio B. on
Hi all,
I have to program a numerical machine that use a language likes Assembler or Basic (jumps, go, break instructions).
So, I'd like tu use Matlab script for testing and simulate the algorithms. For this, I would to implement jump instruction in Matlab.
How can I do this? How can I implemet in Matlab an istruction likes "IF A=0 go line20"?

Thanks in advance.
Fabri
From: Matthias Fripp on
On Jul 2, 11:59 am, "Fabrizio B." <fabrizio.bone...(a)gmail.com> wrote:
> Hi all,
> I have to program a numerical machine that use a language likes Assembler or Basic (jumps, go, break instructions).
> So, I'd like tu use Matlab script for testing and simulate the algorithms.. For this, I would to implement jump instruction in Matlab.
> How can I do this? How can I implemet in Matlab an istruction likes "IF A=0 go line20"?
>
> Thanks in advance.
> Fabri

Instead of trying to convert the numerical machine's language directly
into Matlab instructions, you could try using Matlab to represent the
evolving state of the machine over time. This would probably work a
little differently for an Assembler vs. Basic.

If you are working with assembly code, you would probably want to
convert it into machine code (a series of numbers corresponding to
individual instructions), and then get Matlab to simulate the tick-by-
tick operation of the microprocessor. e.g., you could create a vector
that represents the machine's RAM (mem), and variables that represent
its various registers (a, b, X, stack_pointer, program_counter,
status_flags, etc.). To load a program, you would convert your
assembly code instructions into "machine code" and store them starting
at some location in the mem vector. Then you would set program_counter
to point to that location. Then your Matlab program would have an
endless loop that reads the instruction at the program_counter,
performs the corresponding operation, and then moves the
program_counter to the next instruction. e.g., suppose the
program_counter is currently at 100, and mem(100) contains 185, which
means "add the contents of the following memory location to
accumulator a". Your Matlab program would read mem(101) (say that is
1073), and then add mem(1073) to a. Then your Matlab program would set
the program_counter to 102, and return to the start of the loop. Now
suppose that mem(102) has an instruction that means "jump to the
following address" and mem(103) contains the address 100. The next
time your Matlab program goes through its loop, it will read the value
100 from mem(103), store it in the program_counter, and then return to
the top of its loop. The next time Matlab goes through the loop, it
will read the instruction from mem(100) and proceed from there. (Not
much of a program!)

If you are writing your program in Basic, you will probably want to
create more complex (and separate) structures for programs and data.
For example, you could create a "program" cell array that looks
something like this:

{10, "LET X=10";
20, "LET X=X+10";
30, "GOTO 10"}

You would also have a cell array for data (maybe called "variables")
that looks something like this:
{"Y", 121;
"A$", "telephone"}

Finally, you would have a variable (e.g., current_line) showing which
line of the program you were at.

Your Matlab interpreter program would have an endless loop that
retrieves a line from program(current_line, :), interprets it,
performs any necessary action, updates current_line, and then
repeats.

For the example above, current_line would be 1 when you start the
loop. Your program would retrieve line 1: "LET X=10". It would check
whether there's an entry in the "variables" cell array corresponding
to "X". If not, it would create an entry like {"X", 10} in the
"variables" cell array. Then it would set current_line to 2, and
return to the top of the loop. The next time through the loop, your
Matlab program would retrieve line 2: "LET X=X+10". It would find the
"X" entry in the "variables" cell array, add 10 to the corresponding
value, update current_line to 3 and return to the top of the loop. The
next time through the loop, your Matlab program would retrieve line 3:
"GOTO 10". It would find out which line in the "program" cell array is
labeled 10 (line 1), store that to current_line and go back to the
start of the loop. In this example, that would put it back at the
start of the program.

Good luck with your project. Sounds like fun!

Matthias
From: Fabrizio B. on
Thanks Matthias! It's a good idea. I'll try to implement something fun!
F.
From: Jan Simon on
Dear Fabrizio,

> Thanks Matthias! It's a good idea. I'll try to implement something fun!

I'm deeply impressed, that someone dares to implement a Basic interpreter in Matlab. The complete operating system of the ZX81 together with the graphic engine (64*44 pixels, better bricks) and the Sinclair Basic fit into 1kB. Therefore I assume that you have good chances to get it to run.

I really hope, that you are going to publish it in the FEX. After we have seen Matlab driving a Boing 747 there, I wish to see Matlab driving a Zilog Z80 simulator.

Jan
From: Matthias Fripp on
"Jan Simon" <matlab.THIS_YEAR(a)nMINUSsimon.de> wrote in message <i0l06s$b5u$1(a)fred.mathworks.com>...
> I'm deeply impressed, that someone dares to implement a Basic interpreter in Matlab. The complete operating system of the ZX81 together with the graphic engine (64*44 pixels, better bricks) and the Sinclair Basic fit into 1kB. Therefore I assume that you have good chances to get it to run.

It would be a great Matlab project. Looking at the list of core BASIC commands at http://en.wikipedia.org/wiki/BASIC , I think it would be pretty easy to do. I'm almost tempted to try myself...

I had no idea there were so few distinct commands in BASIC. And I think the Timex Sinclair 1000 (oh the memories) actually stored them in a reduced form. You would type in a line number, then it would switch into "command" mode, where each key on the keyboard corresponded to one command (e.g., "G" for "GOTO"). I think it must have stored a single byte for each command, pre-parsed. Then you'd type in any other elements of that line. The only hard work for the interpreter must have been handling free-form operations like "Hello," & X$ or 1+2*V. But even those shouldn't be too hard to parse.