Tiny VM in C

Discussion in 'C Programming' started by tekk.nolagi, Feb 11, 2014.

  1. tekk.nolagi

    BartC Guest

    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).
     
    BartC, Feb 21, 2014
    #81
    1. Advertisements

  2. 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.
     
    Keith Thompson, Feb 21, 2014
    #82
    1. Advertisements

  3. Oh, sorry about that; I did not realize. I'm new to this whole newsgroup
    thing.
     
    Maxwell Bernstein, Feb 21, 2014
    #83
  4. 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?
     
    Kenny McCormack, Feb 21, 2014
    #84
  5. On 2/21/14, 8:18 AM, BartC wrote:
    [snip]
    That's fair. It appears, then, that I am building something JIT as
    opposed to compiled.
     
    Maxwell Bernstein, Feb 21, 2014
    #85
  6. What would a language like this normally do? Use indentation for
    "blocks"? Have a labelled section end when another begins? Have a
    "label-end"?
     
    Maxwell Bernstein, Feb 22, 2014
    #86
  7. tekk.nolagi

    BartC Guest

    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.
     
    BartC, Feb 22, 2014
    #87
  8. 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.
    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
     
    glen herrmannsfeldt, Feb 24, 2014
    #88

  9. 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
     
    Rick C. Hodgin, Feb 25, 2014
    #89
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.