Tiny VM in C

B

BartC

I do like this idea, though my first thoughts are:
a) why not just have a hash table?
b) why not just use numbers?

Pure numbers would be a little too BASIC-like: you have to keep thinking up
new ones, and remembering what they refer to. I suppose you can use macros
to assign names to numeric values, but maintenance gets hard work. But they
are still better than hard-coding indices of commands.

I'm not sure how hash-tables would be used here (to me they are just a
lookup method, but since the translation of a name to an index is only done
once, not repeatedly at runtime, the lookup doesn't matter too much).
 
K

Keith Thompson

Maxwell Bernstein said:
An alternative is the use the pre-processor. You can use the ## token
joining operator to write this:
[snip]

Well damn, I didn't think of that.

Maxwell, your news software should automatically add attribution
lines for any quoted text, which specify who wrote what. Please
don't delete those lines. Quoting other poster's words without
attribution is considered rude.
 
M

Maxwell Bernstein

Maxwell Bernstein said:
An alternative is the use the pre-processor. You can use the ## token
joining operator to write this:
[snip]

Well damn, I didn't think of that.

Maxwell, your news software should automatically add attribution
lines for any quoted text, which specify who wrote what. Please
don't delete those lines. Quoting other poster's words without
attribution is considered rude.

Oh, sorry about that; I did not realize. I'm new to this whole newsgroup
thing.
 
K

Kenny McCormack

Keith "Kiki" Thompson said:
Maxwell, your news software should automatically add attribution
lines for any quoted text, which specify who wrote what. Please
don't delete those lines. Quoting other poster's words without
attribution is considered rude.

(By stick-up-the-butt nutcases like me)

Fixed it for you.

Count me as a strong supporter of Gordon's methods of posting (and his
reasons for doing so).

--
Here's a simple test for Fox viewers:

1) Sit back, close your eyes, and think (Yes, I know that's hard for you).
2) Think about and imagine all of your ridiculous fantasies about Barack Obama.
3) Now, imagine that he is white. Cogitate on how absurd your fantasies
seem now.

See? That wasn't hard, was it?
 
M

Maxwell Bernstein

On 2/21/14, 8:18 AM, BartC wrote:
[snip]
Pure numbers would be a little too BASIC-like: you have to keep thinking
up new ones, and remembering what they refer to. I suppose you can use
macros to assign names to numeric values, but maintenance gets hard
work. But they are still better than hard-coding indices of commands.

I'm not sure how hash-tables would be used here (to me they are just a
lookup method, but since the translation of a name to an index is only
done once, not repeatedly at runtime, the lookup doesn't matter too much).

That's fair. It appears, then, that I am building something JIT as
opposed to compiled.
 
M

Maxwell Bernstein

Pure numbers would be a little too BASIC-like: you have to keep thinking
up new ones, and remembering what they refer to. I suppose you can use
macros to assign names to numeric values, but maintenance gets hard
work. But they are still better than hard-coding indices of commands.

I'm not sure how hash-tables would be used here (to me they are just a
lookup method, but since the translation of a name to an index is only
done once, not repeatedly at runtime, the lookup doesn't matter too much).

What would a language like this normally do? Use indentation for
"blocks"? Have a labelled section end when another begins? Have a
"label-end"?
 
B

BartC

Maxwell Bernstein said:
What would a language like this normally do? Use indentation for
"blocks"? Have a labelled section end when another begins? Have a
"label-end"?

An assembly-like language you mean? Just google for lots of examples of
assembly source code.

Generally this kind of code doesn't use indentation, except for perhaps a
single level to help distinguish instructions from labels. This is because
the language isn't structured into blocks; it's linear, while the labels are
all mixed up, and no label-end is needed because a label doesn't define a
block, only a point.

You can make your language more elaborate, but in practice people make use
of languages such as C instead, which will generate this target code.
 
G

glen herrmannsfeldt

BartC said:
An assembly-like language you mean? Just google for lots of examples of
assembly source code.
Generally this kind of code doesn't use indentation, except for
perhaps a single level to help distinguish instructions from labels.
This is because the language isn't structured into blocks;
it's linear, while the labels are all mixed up, and no label-end
is needed because a label doesn't define a block, only a point.

With many assemblers, there is no rule against indenting.

When I first wrote OS/360 assembly code, I indented lines without
labels one space, and one space between opcode and operands. Not so
readable, but then the programs were small.

I don't know of any reason not to indent the instructions inside
a loop. The fact that a label may be at the beginning or end,
(or both) doesn't change the visual aid of indentation.
You can make your language more elaborate, but in practice people
make use of languages such as C instead, which will generate
this target code.

Well, there is another choice, which is to make an assembler
language that looks like a high-level language, such as PL/360.

PL/360 looks like PL/I, but each statment directly translates
into machine instructions, possibly with side-effects that a
normal HLL wouldn't have. Consider:

R1=R1+R1+R1;

First, note that R1 is machine general register 1. Then note that
the generated code looks like:

LR R1,R1
AR R1,R1
AR R1,R1

(Maybe the initial LR isn't generated, I haven't looked at the
actual rules in a long time.)

The result is that the value in R1 is multiplied by 4.
(Those used to C sequence points and side effects won't be
at all surprised.)

Still, I believe such languages can be more readable than the usual
assembly format.

-- glen
 
R

Rick C. Hodgin

Generally this kind of code doesn't use indentation, except for perhaps a
single level to help distinguish instructions from labels. This is because
the language isn't structured into blocks; it's linear, while the labels are
all mixed up, and no label-end is needed because a label doesn't define a
block, only a point.

You can make your language more elaborate, but in practice people make use
of languages such as C instead, which will generate this target code.


As Bart suggests, the format can be whatever you want. Personally, I feel
that any assembly code automatically generated by a parser (C to assembly,
for example), should include the C source line being converted, and even
the sub-component within to be placed on line comments (an optional feature
by compiler switch, possibly).

Things should be indented. And based on compiler switches, as for explicit
debugging needs, the ability to view machine code opcode bytes, the raw
assembly form, as well as its text-based form (MOV R1,[R2] and MOV R1,foo),
also as by compiler switch.

Just remember, when you're creating a new thing you don't need to look at
the former constraints which have existed, but you can choose anything you
like. It should make sense, and be forward looking, but there may also be
great desires to bring forward previous source code / inertial mindset needs
of prior years of development, and of existing developer skillsets. In the
end, it depends on what your goals are.

Best regards,
Rick C. Hodgin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,075
Messages
2,570,553
Members
47,197
Latest member
NDTShavonn

Latest Threads

Top