Shop OBEX P1 Docs P2 Docs Learn Events
Input and return values from Assembly — Parallax Forums

Input and return values from Assembly

agimuhingagimuhing Posts: 39
edited 2011-01-04 22:47 in Propeller 1
How do you input values into an assembly program and have the assembly program return a value?


If you know C, I'm trying to make assembly programs receive and return values like inline functions

my question might be worded a little funny so fell free to ask for clarifications

Comments

  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-01-04 15:10
    Your PASM program needs to know the hub address(es) of your input variable(s) and where to put the results. Since you can only pass one parameter to a PASM cog (in the PAR register) this is usually the start of a structure of variables that you've laid out in your main program (which live in the hub). I've attached a dirt-simple demo that illustrates one way to communicate between Spin and PASM.

    Edit: Whoops, found a small typo in the demo (though it didn't affect the way it ran). This line
    jmp     done                            ' invalid command
    

    should be:
    jmp     #done                           ' invalid command
    

    The # sign is required here.
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-01-04 15:25
    Inline assembly in C means to use assembly instructions inside of usual C code just by using the asm-instruction, right?

    That's not possible with the propeller. You have to understand how the propeller works. After booting there will be one COG running the SPIN interpreter, which is executing the main SPIN program. The COG only has 512 longs of memory and the SPIN interpreter needs all that memory. The SPIN program simply stays in HUB-RAM. The interpreter fetches the instructions and executes the subroutines which implement the instruction.
    PASM can only be used in a free COG. But then there is no more room for a SPIN interpreter.

    So, you either have a SPIN COG or a PASM COG.

    When starting a PASM COG you can pass exactly one parameter. As this is not enough for most cases this parameter is used to pass a start address of a memory area which is then used to EXCHANGE parameters. Both, the code that want's to pass parameters and the PASM exactly "know" which parameter has to be put where and where to find the results and of course how big this memory area is.
    It's a matter of the PASM code to read data from there or write data back to these locations. And it's a matter of the code that wants to communicate with the PASM COG to put the values there when needed or take back the result.

    A concept used very often is to have one long with which the SPIN code can tell the PASM COG what to do (give an instruction). Finding a value different than 0 tells the COG that it has to execute a command and the PASM COG sets this memory location back to 0 if it's done with its job.
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-01-04 15:26
    @Jonny: Hi again Jonny ;o)
  • agimuhingagimuhing Posts: 39
    edited 2011-01-04 15:57
    MagIO2 wrote: »
    Inline assembly in C means to use assembly instructions inside of usual C code just by using the asm-instruction, right?

    actually I want to write a PASM program that can input and output data similar to an inline function, not use assembly inside C
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-01-04 15:57
    @Mag: We have to stop meeting like this!

    @Agimuhing: MagIO's post is an excellent description of what my little demo is doing. Between my code and his description it should all come together for you.
  • max72max72 Posts: 1,155
    edited 2011-01-04 22:47
    You can use a Pasm COG as a kind of coprocessor (check float objects for example). Probably this is what you are looking for.

    Moreover you can have inline assembly. SpinLMM is a solution, and another is from Beau. Both are on the obex.
    Check http://forums.parallax.com/showthread.php?125599-Pepper-SPIN-with-Assembly

    Massimo
Sign In or Register to comment.