Shop OBEX P1 Docs P2 Docs Learn Events
How should comments and expressions work in PASM that isn't inside Spin? — Parallax Forums

How should comments and expressions work in PASM that isn't inside Spin?

ersmithersmith Posts: 5,914
edited 2019-11-11 16:07 in Propeller 2
I've been working on expanding the inline assembly facility in FlexC and FlexBASIC, to bring it in line with what's already available in the Spin language (e.g. so that you can put PASM programs to run in another COG directly in the .c or .bas source code). But in doing that I ran into a fundamental question: what should the syntax for comments and expressions be in this inline assembly? In a .c file should all the comments be C-style ones, even inside the asm block? Or should the rules for Spin PASM apply in there?

The C style code would look like:
__asm {
        // code for blinking a pin
        // gets the pin number as a parameter
	org	0
entry
        rdlong  pinNum, ptra++

LR__0001
	drvnot	pinNum
        waitx   ##0x20000000 /* wait a bit */
	jmp     #LR__0001
            
pinNum  long    0
}

whereas Spin style assembly inside a C file would look like:
__asm {
        ' code for blinking a pin
        ' gets the pin number as a parameter
	org	0
entry
        rdlong  pinNum, ptra++

LR__0001
	drvnot	pinNum
        waitx   ##$20000000 ' wait a bit
	jmp     #LR__0001
            
pinNum  long    0
}

I can see arguments for doing it both ways:

The first way, using C style comments and expression syntax, makes the integration with C much more seamless and allows us to make use of preprocessor #defines and such inside of the inline assembly. It's also a little bit easier to implement.

The second way, using Spin style comments and expression syntax, makes cutting and pasting PASM code between languages easier, and will look more familiar to assembly language programmers.

What preferences do you guys have? (Assume I can only do one... my time is not unlimited!). I'd be particularly interested in how other language designers feel about this. @cgracey , what would you recommend? Should PASM syntax be the same within all languages? Or should it adapt in a (limited) way to the language in which it's embedded?
«1

Comments

  • Isn't the /* .. */ a multiline comment? (BTW I usually do the opposite way: /* */ as header multiline comment; and // as line comment)
    If not, then what is the difference between detecting ' or /* or both?

    Anyway, if only one is possible I vote to keep pasm syntax with pasm code.
  • I’d also vote to keep PASM syntax for PASM code, regardless where it may be found.

    As part if my evil plan, I’d like to use BASIC as “bait” or “training wheels”. My minions can gain confidence in basics like programming flow and logic, and then push them to get a bit more adventurous and try some inline assembly (or Spin, see below). The ultimate goal would be to let them work their way up to discarding BASIC almost entirely.

    Eric: Any plans to include inline SPIN inside a BASIC program, ala the way ASM blocks are done now?
  • dMajo wrote: »
    Isn't the /* .. */ a multiline comment? (BTW I usually do the opposite way: /* */ as header multiline comment; and // as line comment)
    Yes, my example was probably not ideal stylistically, although it was valid C. The main point is that Spin and C have different comment markers and expression syntaxes (and even operator priorities) so for example if we interpret things in C style:
       mov a, #b//c
    
    moves the constant "b" into a, and has a comment "c", whereas in Spin it moves the remainder of b divided by c into a.

    Similarly, if interpreted C style:
       mov a, #'A'
    
    would copy the value 65 (ASCII value of 'A') into register a, whereas in Spin it is a syntax error.

    And for extra scary differences, in C the expression "2+3<<4" evaluates to "(2+3)<<4" or 80, whereas in Spin it evaluates to "2+(3<<4)" or 50.


  • yetiyeti Posts: 818
    edited 2019-11-11 19:04
    Reading stuff like "||r" or "|<3" in BASIC's or C's inline assembler would kill me.

    Ok... let's try it...
    ...whatever you think...

    Having 3 different PASM styles makes no sense for most PASM users.
    Having PASM mimic the style of the outer language would be nice too... at least for me...

    I have the QuickReference.pdf as cheat sheet... so there is a small chance, I'll survive it... whatever direction it will take...
  • I would prefer the rules for SPIN/PASM (SPASM, lol) remain the same.

  • I think it would be better to use PASM syntax in inline assembly code. This will make it easier to cut-and-paste PASM code from Spin programs. This should help avoid syntactical gotchas that the programmer might encounter.
  • JRoark wrote: »
    Eric: Any plans to include inline SPIN inside a BASIC program, ala the way ASM blocks are done now?

    Shoe-horning in just enough of Spin to do PASM inside of BASIC is going to be hard enough (although not nearly so bad for BASIC as it is for C, since BASIC syntax is already closer to Spin). Don't expect in-line Spin anytime soon.
  • potatohead wrote: »
    I would prefer the rules for SPIN/PASM (SPASM, lol) remain the same.

    Just to be clear: I have no intention whatsoever to change any rules for PASM inside Spin. It's only PASM inside C and BASIC that is in question.

    To clarify further: there is already provision for in-line assembly (ASM blocks) in C and BASIC. It's just that so far the only use of these has been relatively simple, small enough that the need for comments and complicated expressions hasn't come up. (At the moment comments and expression parsing in in-line assembly follow the rules of the enclosing language.)

    I really am torn about this, because for small snippets it's much more logical to the programmer for the ASM expressions to follow the rules of the language around it; but for large blocks of code like a whole COG program it's no longer so clear.

    Maybe we need two kinds of in-line assembly, __asm and __pasm, where __pasm indicates Spin syntax. That's going to be a lot of work though :(.

    I can see now why the GCC people chose to make their in-line assembly go inside of strings, it makes the parsing much easier and avoids these syntactical issues.
  • jmgjmg Posts: 15,148
    ersmith wrote: »
    I can see arguments for doing it both ways:

    I've used assemblers that can accept both. Comment to end of line is fairly easy to manage/alias.

    They also tolerate 0xabcd and 0abcdH on numbers.. & can work with 32b numbers in expressions.

  • Cluso99Cluso99 Posts: 18,069
    IMHO pasm syntax makes more sense as it permits pasm blocks to be copied to/from wherever. This may make it easier to also inline spin at some future time if that ever gets done.

    Comment syntax ‘ for spin/pasm would be nice to remain ‘.

    As for the block comments syntax (spin/pasm) this could be changed to the local hosts version before inserting if necessary. Remember we use { } and {{ }} in spin/pasm. So Maybe we (coder) have to change any comment block encoding to the local syntax eg /* */.
  • potatoheadpotatohead Posts: 10,254
    edited 2019-11-11 20:17
    Yes, I understood that Eric. Given your recent comments, perhaps "enclosing language rules" makes better sense for FastGUI.

    I see SPIN+PASM as one thing. Technically, on P2, that does not have to be true as PASM only development is possible, and some would argue, practical.

    With so many coming from P1, and or simple preferences for syntax, they are probably going to continue to be one thing. I myself much prefer things like $ for hex, and the %% notation for binary and two bit data. The 0x business is hard to read. But that's me.

    There are cases:

    One is a snippet. That seems like it can follow enclosing language rules with minimal fuss.

    Two, a program, driver. Here, we want to leave it be. Too much to change. These won't be inlined, or if they are, it is sort of a pathological edge case, right?

    Three, someone is writing SPASM. Enclosing language rules is consistent.

    And there is my revised answer.

    Enclosing language rules is much better than the gcc way, FWIW! So, there is that bonus.

    If a particular snippet ends up being difficult, it can always be packaged up as a class, etc...
  • jmgjmg Posts: 15,148
    ersmith wrote: »
    Yes, my example was probably not ideal stylistically, although it was valid C. The main point is that Spin and C have different comment markers and expression syntaxes (and even operator priorities) so for example if we interpret things in C style:
       mov a, #b//c
    
    moves the constant "b" into a, and has a comment "c", whereas in Spin it moves the remainder of b divided by c into a.

    Similarly, if interpreted C style:
       mov a, #'A'
    
    would copy the value 65 (ASCII value of 'A') into register a, whereas in Spin it is a syntax error.

    And for extra scary differences, in C the expression "2+3<<4" evaluates to "(2+3)<<4" or 80, whereas in Spin it evaluates to "2+(3<<4)" or 50.
    Yes, expressions are trickier.
    I thought Chip had cleaned up Spin, so the worse syntactic chaff was removed ? Maybe the // missed the cut ?
    Something that 'fails silently' is best avoided...

    Operator precedence is something users can check for in the LIST files, and brackets can always ensure you get what you expect ?


  • I also think spin comments are right, because // is a operator in spin/pasm.

    Mike
  • ersmith wrote: »
    I can see now why the GCC people chose to make their in-line assembly go inside of strings, it makes the parsing much easier and avoids these syntactical issues.
    Maybe you could adopt the same convention as GCC, and require opening and closing quotes. However, this would require escaping quote characters within the string. So mov a, #"A" would need to be written as mov a,#\"A\". Also, if you changed __asm{...} to __asm("...") it would adhere to the C syntax.

  • please don't.

    GCCs asm integration is just cruel and wrong.

    Putting PASM into strings is simply ugly.

    Mike
  • Seconded. Enclosing language rules is a much more workable alternative.

  • GCC inlining rules make more sense when you understand why they are the way they are. Primarily, you're writing an ASM template allowing the compiler to substitute variables and registers in the optimization stage, and specifying what the rules are regarding use of some of the registers so you don't have to do your own register save/restore, and it doesn't have to save/restore registers you use if it isn't using them.
  • msrobots wrote: »
    I also think spin comments are right, because // is a operator in spin/pasm.

    Mike

    Well, but it's not an operator in C. The way you write remainder in C is with %, not //.

    For programmers who are used to Spin/PASM things like // will seem natural. But a C programmer who doesn't know Spin is going to read a line like:
       mov x, #C // 10
    
    very differently from a Spin programmer, and if you're in the middle of writing C code you might very well code this as:
       mov x, #C % 10
    
    since you're thinking in C.

    There probably isn't any one correct answer. Right now I'm leaning towards leaving __asm the way it is now (inside an __asm block comments and expressions are parsed the way the surrounding language does). I might be able to additionally achieve a subset of PASM with Spin rules inside a new keyword like __pasm. I don't know if that would make things even more confusing.
  • I say stay with comments that coincide with the surrounding language... It's easy to paste pasm code into C sources "as-is". A C-familiar programmer will most likely see any ' type comments and figure out that there's a difference from standard C comments. If they are in the process of embedding inline PASM code, they'll be thinking in the pasm mindset, anyway.

    The difference in what '//' means in C versus Spin as shown in posts above, would really throw someone off (their code will execute differently, not just give them a syntax error).

    Just my .02$
    dgately
  • msrobots wrote: »
    please don't.

    GCCs asm integration is just cruel and wrong.

    Putting PASM into strings is simply ugly.

    Mike
    Sorry, just making a few suggestions that might make it easier to implement PASM comments. However, if Eric can implement PASM comments using the __asm{…} syntax, that would be great.
  • Does semicolon; have any meaning in PASM? That's the usual comment in x86 assembly.

    As a side-benefit, having the comment differ between languages is how most Polyglot programs work (though I doubt we want to target inline assembly at polyglots...).
  • kwinnkwinn Posts: 8,697
    I'm crossing my fingers and hoping for a single Pasm comment character for all high level languages. Not too fussy about what specific character is used, although I would prefer a single key press like ; or / or \.
  • jmgjmg Posts: 15,148
    Does semicolon; have any meaning in PASM? That's the usual comment in x86 assembly.
    semicolon is very common in assembler and many table-generate programs comment with semicolons.
    kwinn wrote: »
    I'm crossing my fingers and hoping for a single Pasm comment character for all high level languages. Not too fussy about what specific character is used, although I would prefer a single key press like ; or / or \.
    // is not moving a finger like /* does and is seen more often in assemblers now as an alias comment.
    It may be possible to allow 3 EOL cases , meaning the semicolon could become a common/portable ASM standard, clear of other syntax issues ?

  • Yeah, thinking about it, Spin/PASM never uses the semi-colon, does it?
    It is also the comment character in a lot of other assembly languages and more easily reachable than the forward slash (at least on my QWERTZ keyboards. Shift+7, owie ouch)
  • The semi-colon as comment character goes way back, to M80 and PALASM and earlier probably. It's what I'm used to and appears more elegant to me.

    Please could ' and ; both be accepted in all P2 assemblers?
  • kwinnkwinn Posts: 8,697
    TonyB_ wrote: »
    The semi-colon as comment character goes way back, to M80 and PALASM and earlier probably. It's what I'm used to and appears more elegant to me.

    Please could ' and ; both be accepted in all P2 assemblers?

    Request seconded.
  • I don't care exactly how this gets resolved, but it needs to be resolved, along with a number of other issues. Like, NOW.

    P2 is coming. When the original Propeller came there was a common development language for it. If you went to the OBEX looking for code you knew what language it would be in and you knew it would work in the development system you were using.

    I don't care what development system that ends up being for P2. I really don't. (except I really, really hate C++ but that's just me.) But even if it's C++ we need to be clear on this. If the driver I need for a video mode, a temperature sensor, or serial port is there, it needs to either be in the language I'm using, or in the one I know I need to know to translate from. This is the way it was for P1 until $recently.

    Parallax, you need to fix this before it becomes a deal-killing problem. You need to tell us what the lingua spin is for P2, and make sure we all have access to the development tools for it, and if any other languages are being seriously supported you need to make sure we have access to good paths between those and the One True Prop Language. And please, please, please don't try to pretend there is more than one True Prop Language. Decide. Tell us. And enforce it. You have to.
  • cgraceycgracey Posts: 14,133
    edited 2019-11-13 04:36
    ersmith wrote: »
    I've been working on expanding the inline assembly facility in FlexC and FlexBASIC, to bring it in line with what's already available in the Spin language (e.g. so that you can put PASM programs to run in another COG directly in the .c or .bas source code). But in doing that I ran into a fundamental question: what should the syntax for comments and expressions be in this inline assembly? In a .c file should all the comments be C-style ones, even inside the asm block? Or should the rules for Spin PASM apply in there?

    The C style code would look like:
    __asm {
            // code for blinking a pin
            // gets the pin number as a parameter
    	org	0
    entry
            rdlong  pinNum, ptra++
    
    LR__0001
    	drvnot	pinNum
            waitx   ##0x20000000 /* wait a bit */
    	jmp     #LR__0001
                
    pinNum  long    0
    }
    

    whereas Spin style assembly inside a C file would look like:
    __asm {
            ' code for blinking a pin
            ' gets the pin number as a parameter
    	org	0
    entry
            rdlong  pinNum, ptra++
    
    LR__0001
    	drvnot	pinNum
            waitx   ##$20000000 ' wait a bit
    	jmp     #LR__0001
                
    pinNum  long    0
    }
    

    I can see arguments for doing it both ways:

    The first way, using C style comments and expression syntax, makes the integration with C much more seamless and allows us to make use of preprocessor #defines and such inside of the inline assembly. It's also a little bit easier to implement.

    The second way, using Spin style comments and expression syntax, makes cutting and pasting PASM code between languages easier, and will look more familiar to assembly language programmers.

    What preferences do you guys have? (Assume I can only do one... my time is not unlimited!). I'd be particularly interested in how other language designers feel about this. @cgracey , what would you recommend? Should PASM syntax be the same within all languages? Or should it adapt in a (limited) way to the language in which it's embedded?

    Ersmith,

    I'm not sure what would be best. There are a lot of ramifications to supporting different comment schemes and, especially, operators with their differing precedences.
  • Spin/PASM already uses ' { and {{ for comments. Why add another way to do comments? It seems like there could be better uses for the semi-colon, such as separating multiple statements on a single line. I'm not necessarily suggesting that the semicolon be used that way, but why add a fourth way to do comments?
  • kwinn wrote: »
    TonyB_ wrote: »
    The semi-colon as comment character goes way back, to M80 and PALASM and earlier probably. It's what I'm used to and appears more elegant to me.

    Please could ' and ; both be accepted in all P2 assemblers?

    Request seconded.

    I'm with @"Dave Hein", I don't think we need to add yet another way to comment. I actually agree that semicolon would have been a nicer choice for the comment character, but Spin chose single quote a long time ago and introducing a third variant of the assembly syntax seems like a dubious idea.

    Having said that, if @cgracey wants to add semicolon comments to PNut I'll support them in fastspin. I think the stand alone assembly language should be compatible across assemblers. Again, just to re-iterate, this thread has been about inline assembly inside C code. A plain .spin2 assembly file should be pretty much the same whether you compile it in PNut, p2asm, or fastspin. I did add the preprocessor, but that's a pretty common extension and can be provided by an external program. Otherwise I'd like to keep stand-alone assembler consistent.
Sign In or Register to comment.