Shop OBEX P1 Docs P2 Docs Learn Events
Execute Code in String — Parallax Forums

Execute Code in String

crgwbrcrgwbr Posts: 614
edited 2008-05-26 20:45 in Propeller 1
If I have a string containing a chunk of code, is it possible to tell the Propeller to execute the string? I'm sure that this would be impossible if the string contained SPIN, as the Prop itelf cannot compile SPIN to bytecode. However, if it was ASM, I think this would possible. Any Ideas? Do I have to wait for the PropII?

Thank,
Craig

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
My system: 1.6 GHz AMD Turion64 X2, 4GB DDR2, 256MB ATI Radeon Graphics card, 15.4" Widescreen HD Screen

I have a duel boot of Ubuntu Linux and Windows Vista. Vista, because it came with the PC, Ubuntu because I like software that works.

"Failure is not an option -- it comes bundled with Windows."

Use The Best...
Linux for Servers
Mac for Graphics
Palm for Mobility
Windows for Solitaire

Comments

  • Robot FreakRobot Freak Posts: 168
    edited 2008-05-26 19:53
    There are some posts about an on-chip-development system, so I guess they've got to interpret or compile the string into code.
    So the answer is Yes, it is possible but hard to do.

    It's easier to use some kind of preprocessor on the computer to translate the string into bytes, words or longs.
    But maybe you can't use such thing...
  • StefanL38StefanL38 Posts: 2,292
    edited 2008-05-26 20:09
    Hello Craig,

    if you already have the longvalues of the ASM-commands you could store them into COG-RAM, set the target-value of a jump-command to that adress and then
    the cog is executing this code

    But i would like to ask one of my favorite questions: i'm curious about the backround why you want to do so.

    Maybe there is an alternative solution to the REAL problem


    best regards

    Stefan
  • Mike GreenMike Green Posts: 23,101
    edited 2008-05-26 20:16
    What does the string contain? There's no built-in compiler or assembler so the string would have to contain compiled Spin bytecodes or assembled instructions. The instructions would have to be stored on long-word boundaries or copied to a long-word aligned area to be able to start it using COGNEW or COGINIT. If you've got Spin bytecodes, it's much more complicated in that there are internal tables that have to be patched.

    The Prop II won't initially have any kind of on-chip development system.

    There are several FORTH systems available for on-chip Propeller development. None of them are actively supported, but they're far enough along to be quite useful and FORTH does include an assembler. There's also FemtoBasic in various versions which can be used much as you describe. You simply hand a string to a routine that replaces keywords with special codes, then hand that processed string to the interpreter to execute.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-05-26 20:22
    If the machine-language code in the string is properly formatted (i.e. LONG-aligned, four characters per instruction, little endian order — and why are you using a string, instead of an array of LONGs, exactly?) and assembled to begin at location zero, you could just start it using a COGNEW, with the address of the string as the first argument. An alternative would be to feed the address to one of the several LMM interpreters that have been posted. (It would still have to execute in a separate cog, however. 'No getting around that unless someone writes an assembly code simulator in Spin.)

    -Phil

    Post Edited (Phil Pilgrim (PhiPi)) : 5/26/2008 8:29:36 PM GMT
  • crgwbrcrgwbr Posts: 614
    edited 2008-05-26 20:45
    Thanks everyone for the great replies. Basically, what I'm trying to do, is to write a small OS for the Prop. One of the features I'm working on is the ability to write a program in Spin or ASM and store it in a text file. Then, I want to be able to execute that program by copying a line into an array (of bytes?). Then executing that line, and copying in the next line.

    The OS will also have an interpreter for a Python-like language (called Cobra), but I wanted to give the option to run Prop native commands. So, the Cobra interpreter could be interpreting/running a Cobra file, but then immediately switch to running a few Spin or ASM commands and then back to Cobra. This should function something like the ASM command in SX-B.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    My system: 1.6 GHz AMD Turion64 X2, 4GB DDR2, 256MB ATI Radeon Graphics card, 15.4" Widescreen HD Screen

    I have a duel boot of Ubuntu Linux and Windows Vista. Vista, because it came with the PC, Ubuntu because I like software that works.

    "Failure is not an option -- it comes bundled with Windows."

    Use The Best...
    Linux for Servers
    Mac for Graphics
    Palm for Mobility
    Windows for Solitaire
Sign In or Register to comment.