Shop OBEX P1 Docs P2 Docs Learn Events
The use of "#" — Parallax Forums

The use of "#"

cts566cts566 Posts: 4
edited 2010-03-05 11:29 in Propeller 1
Is there any good guide for knowing when to use # or not in Propeller Assembly?
I have read manuals and guides, but there doesn't seem to be any hard and fast rules to the "#".

Thanks,
AP

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-03-02 20:57
    "#" is used to specify an immediate value in an instruction. It's normally used for any kind of jump instruction (DJNZ, JMP, JMPRET, CALL, TJZ, TJNZ) and is used in any other instruction where you want a 9-bit immediate constant value for the source field (commonly used for shift counts). It's occasionally useful to not use "#" for a jump instruction, but that's unusual and you should know what you're doing if you do that.
  • Erik FriesenErik Friesen Posts: 1,071
    edited 2010-03-02 22:57
    My opinion is that the manual needs a more detailed explanation of the use of "#" especially in regards to assembly.
  • potatoheadpotatohead Posts: 10,261
    edited 2010-03-02 23:23
    Agreed.

    "JMP #5" = the value 5 is encoded directly into the instruction "source" bit-field, and is copied to the COG program counter, when the instruction is executed. This is a direct jump, where the value inside the instruction is to be loaded directly into the program counter.

    "JMP 5" = the value 5 is encoded directly into the instruction "source" bit-field, and is the address of a COG memory location that contains the value to be loaded directly into the COG program counter. This is an indirect jump.

    Edit: Since we were talking about personification on an earlier thread, maybe this is a good time to use it. If I tell you to go through the third door to the right, that's a direct reference, and that's when you use the octothorpe. If I were to tell you go through the door that Phil tells you to go through, that's an indirect reference, and it's not when you use the octothorpe.

    Only the 9 bit "source" bit-field of an instruction is used in tandem with the "#", or otherwise known as the octothorpe.

    The thing to remember is that when the octothorpe is present, the value contained in the instruction "source" bit-field will be used directly as an address, or value. For jumps, you always want it, unless you have a good reason why not, because indirect jumps are not used much in PASM.

    Other instructions might vary.

    Let's take the shift instruction as one other easy example.

    There are basically two cases:

    1. The number of bits to shift is known, and can be directly specified in the instruction; you use the octothorpe for this case: "SHL address, #7"

    2. The number of bits is not known, and will be calculated as the program runs; you may or may not use the octothorpe

    In the not known case you can either:

    2a. use a COG memory location to hold that value for use by the shift instruction when it executes; do not use the octothorpe: "SHL address, 7"

    ,or

    2b. you can directly modify the PASM instruction before the shift instruction executes, so that when it is actually executed the intended number of shifts then occurs. For this, you do use the octothorpe because you want the value to be directly used, not serve as an indirect address after it has been modified by some other instruction.*

    *PASM includes self-modify instructions, movd, movs, movi for this purpose.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Propeller Wiki: Share the coolness!
    8x8 color 80 Column NTSC Text Object
    Safety Tip: Life is as good as YOU think it is!

    Post Edited (potatohead) : 3/3/2010 3:42:42 AM GMT
  • tonyp12tonyp12 Posts: 1,951
    edited 2010-03-02 23:42
    You use # when you want to number behind to be used
    and not the address that the number is stored at.

    With jpm, you use # as to point to a location that you have put a name on.
    The Assembler will convert this to machinecode and will put the correct value there.
  • David BDavid B Posts: 592
    edited 2010-03-03 00:38
    I can understand the confusion - I'm not all that clear on its usage, either.

    But I think I can add a few additional points that have not yet been made (someone please correct me if I got these wrong)

    First, when variables are being declared, the # is not used preceding digits.

    Second, when using variables, the # is not used before the name. But names from a CON section look like variables but are treated like immediate numbers, both in the code section and in the data section, so on casual reading, there seems to be a random mix of "#names" and "names".


    CON

    con_name = 4

    ...

    DAT org


    mov myvalue, #con_name
    mov myvalue, dat_name_x
    mov myvalue, #4

    ' data

    dat_name_x long con_name
    dat_name_y long 4

    myvalue res 1
  • mparkmpark Posts: 1,305
    edited 2010-03-03 00:50
    Whether or not to use # in front of a name does not depend on where the name is defined.

    Better just to learn what # really means.
  • MicrocontrolledMicrocontrolled Posts: 2,461
    edited 2010-03-03 00:56
    From what I understand, when you place a hash sign you are saying "take this number" or "take the number stored in this variable" where as no hash means "go to this memory location (indicated by number and in cog RAM) take the value from that and use it". You would only not use the hash if you had used the "par" constant to return a pointer, and a pointer is a variable that points to a location in memory, so you would not use the hash for that. Other then that, I use the hash in front of everything.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Don't click on this.....
  • jazzedjazzed Posts: 11,803
    edited 2010-03-03 01:16
    cts566 said...

    Is there any good guide for knowing when to use # or not in Propeller Assembly?
    I have read manuals and guides, but there doesn't seem to be any hard and fast rules to the "#".
    Generally, # tells the PASM assembler to produce an instruction with the "I bit" set so the Propeller knows to use #source as the number or address and not the data contained at the address specified by the #source label or number.
    • "instruction destination, source" PASM says "Propeller, interpret the source as the content which lives at the source address."
    • "instruction destination, #source" PASM says "Propeller, interpret the source as a number or an address and not the content of an address."
    • "jmp #source" says "Propeller resume execution at the source label address."
    • "jmp source" says "Propeller resume execution at the data contained at the source label address."
    Post Edited (jazzed) : 3/3/2010 2:19:50 AM GMT
  • VIRANDVIRAND Posts: 656
    edited 2010-03-03 06:14
    MOV register,#$1FF    ' register := $1FF
    MOV register,$1FF       'register = LONG[noparse][[/noparse]$1FF] (not exactly like that Spin HUB RAM code, but PASM and cog RAM)
    
    'Similar idea: (this example is SPIN not PASM)
    'some_variable := "A"   'some variable is set equal to the character number for "A" 
    'Some_variable := A      'some variable is set equal to the same number as the variable labeled A is set to
    
    JUMP #0              'jump to the first long in this Cog
    JUMP 0                 'jump to the address stored in the first long in this Cog           
    
    RDLONG register,#label   'not so useful because this only works if label is in first 512 longs of HUB RAM
    
    RDLONG register,pointer  'useful for all of HUB RAM and also ROM (font, math tables, etc)
    pointer LONG label           'label can be anywhere in HUB RAM (is this correct for DAT also?)
    
    WRLONG            '---           uses the same exact operands, unlike MOV destination,source
    
    MOVI,MOVD,MOVS             'these change selected 9 bit parts of the instruction, and its operands within the instruction,
                                             'which is very unusual but useful for using lookup tables or arrays in Cog RAM 
                                             'but modifying the instruction itself is a very unusual option with unexpected uses.
    
    # means something like equals, the data is in that instruction line, but can only accept 9 bit numbers, 0 to 511
    no # means read a register  in Cog RAM, there are only 511 registers but they hold numbers to up to 4 billion
    RDLONG and WRLONG       'are the only ways to access HUB RAM in a Cog
    Each special MOV(I,D,S) instruction changes only one of three 9-bit-parts of an instruction in a Cog.
    MOV  register1,register2    'changes the whole LONG of register1 to the same as whats in register2
    MOV  register1,#%1_1111_1111   'sets the maximum allowed by #, nine bits, of register2 , to nine one-bits.
    
    


    All these have often come up as confusing, I think.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    I should be typing in Spin now.
    Coming soon. My open Propeller Project Pages and favorite links index.

    Post Edited (VIRAND) : 3/3/2010 6:23:20 AM GMT
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2010-03-03 11:17
    Potatohead,

    Nobody says octothorpe apart from 1960's telephone engineers (according to wikipedia) [noparse];)[/noparse] Better to just use # then it can be translated as appropriate, I will read hash, others pound and you and your fellow telephone engineers octothorp/octothorpe/octathorp/octatherp [noparse];)[/noparse] [noparse];)[/noparse]

    All,

    Firstly on page 241 of recent manuals there is a bit about the # symbol.

    Much of the confusion about # comes from not understanding how the assembly instructions are made up of 32bits containing the instruction name and 9-bit fields for source and destination. For each command in the docs you will see something like this:

    SValue (s-field) is a register or a 9-bit literal ....

    So you supply an address of a register (which is the same as its name) or you supply a number you want to be taken literally and immediately(just use it don't use it to get some other number). So to add 10 to a register you do add total,#10 because you want 10 to be added to the total rather than the value stored at memory address 10.

    This is also why you can't do add total,#600 because 600 won't fit in 9 bits.

    For the jump commands remember that the label is the address of that part of code so you want the code to go to that address directly, hence the #.

    Graham
  • Erik FriesenErik Friesen Posts: 1,071
    edited 2010-03-03 13:14
    Everything said is all good, so why not add some of it to the manual?
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2010-03-03 16:24
    As I mentioned in the newer manual it is discussed on page 241.

    There is also the issue of what exactly the manual is for, it is more than a datasheet but less than a learn programming book.

    Graham
  • potatoheadpotatohead Posts: 10,261
    edited 2010-03-03 16:55
    Grahan, yep.

    Your point is nicely taken. I'll go OT here for a moment, because words are fun, and "playing" with them some, not being inhibited in expression, has some value.

    It's funny you mention telephony engineers because I worked with some of them last week, and paid one of them a considerable amount to assist in the "programming" of a phone PBX. We here, from the perspective we have, would call that "configuration", and the differences start there, and build rapidly! I've experienced clashes of this kind on occasion, and always react the same way; namely, to put some of what was learned to use just enough to solidify it, and make it a part of my working lexicon. So, I'll use it some to explore the elements that contribute to it, and then move on.

    Had I done a bit more of that last time, I wouldn't have been paying for stuff this time. The concepts behind the PBX are simple, easily understood, and not difficult to manipulate, but the terminology gets in the way enough to be a problem. Of course, this is by design, and I don't consider it nefarious, just costly, and something to be avoided where it makes sense.

    What my life experience tells me is that when I feel that sense of intrigue that comes with learning a new thing, that's a clear sign that element of expression is not yet solidified enough to be permanent. Why learn it, only to have it forgotten, confusing, and or useless, requiring dollars and or time, which are one and the same a subsequent time around?

    One of the tasks we have in front of us, where I work, is dealing with people over the phone. Introducing some of them to octothorpe, in a playful way, has saved a considerable number of words, and that translates into time, which presents as opportunity. Until recently, it was "that pound sign", or "shift 3 on your keyboard", etc... All of the other characters are largely understood by their actual name, meaning I can say, "tilde, asterisk, ampersand, etc... with this one being no different, over some time.

    There is a self interest there that also drives why I will sometimes do this kind of thing.

    Finally, there is a subtle element in play that I take seriously on a lot of levels. Branding, community, messaging, etc... all will depend on specific word use. If a person takes the time to recognize labels, coined phrases, uncommon word use, and other contributing elements, one then can easily recognize when and why said messaging occurs, and decide whether or not to buy into it, or more importantly: propagate it, build new messaging, or leverage what it out there.

    In the case of "our users", of "the software", some company, branding specific, precise language, stands out nicely, and over the years has served as one of our subtle, but recognizable and potent differentiators.

    So there you go. It's a bit more complex than what I read essentially as, "don't pick up bad habits from telephony guys" [noparse]:)[/noparse]

    We can now return to direct, vs, indirect addressing.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Propeller Wiki: Share the coolness!
    8x8 color 80 Column NTSC Text Object
    Safety Tip: Life is as good as YOU think it is!

    Post Edited (potatohead) : 3/3/2010 5:08:09 PM GMT
  • potatoheadpotatohead Posts: 10,261
    edited 2010-03-03 17:11
    I really like how this is expressed in the manual:

    (from that page)
    The Manual @ page 241 said...
    Don't Forget the Literal Indicator '#'
    Make sure to enter the literal indicator, #, when a literal value (a.k.a. immediate value) is intended. Modifying the first line of the above example by omitting the # character (ex: ADD X, 25) causes the value in register 25 to be added to X instead of the value 25 being added to X.

    Another possible mistake is to omit the # on branching instructions like JMP and DJNZ. If the intended branch destination is a label named MyRoutine, a JMP instruction should normally look like JMP #MyRoutine rather than JMP MyRoutine. The latter causes the value stored in the MyRoutine register to be used as the address to jump to; that's handy for indirect jumping but it is usually not the intention of the developer.

    When we discussed this, and the word "octothorpe" first appeared here, there was some nice discussion about "immediate", vs, "direct", and I actually was one of the ones who had latched onto "immediate" as the primary term for this kind of addressing. The better case for direct was made, and I updated that in my own lexicon, moving "immediate" to the status of a qualifier, or descriptor, instead of the core word element needed to differentiate this addressing from indirect addressing in general. There isn't a material difference between doing that, and my remarks above on "octothorpe". Words are words. We learn them, and use them as the tools they are.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Propeller Wiki: Share the coolness!
    8x8 color 80 Column NTSC Text Object
    Safety Tip: Life is as good as YOU think it is!

    Post Edited (potatohead) : 3/3/2010 5:21:07 PM GMT
  • Erik FriesenErik Friesen Posts: 1,071
    edited 2010-03-03 20:03
    Ok, Im bad. I had 1.01 and though it was the new.
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2010-03-03 21:50
    potatohead, don't let me suppress your octothorpedness, long live the octothorpe. Strangely our shift-3 is a pound sign but not # it's £. I wonder at this point if that will display properly for you.

    Graham
  • jazzedjazzed Posts: 11,803
    edited 2010-03-04 00:28
    Graham Stabler said...
    Strangely our shift-3 is a pound sign but not # it's £.
    I've heard '#' called·pound often. It's also used as a shortcut notation for "number."
  • BergamotBergamot Posts: 185
    edited 2010-03-04 01:04
    Graham Stabler said...

    Strangely our shift-3 is a pound sign but not # it's £.



    I've heard '#' called pound often. It's also used as a shortcut notation for "number."

    # = weight pound
    £ = money pound

    'In most regions of the United States, the symbol is traditionally called the pound sign, but in others, the number sign. This derives from a series of abbreviations for pound, the unit of weight. At first "lb." was used; however, printers later designed a font containing a special symbol of an "lb" with a line through the verticals so that the lowercase letter "l" would not be mistaken for the numeral/digit "1". Unicode character U+2114 (&#8468[noparse];)[/noparse] is called the "L B Bar Symbol", and it is a cursive development of this symbol. Ultimately, the symbol was reduced for clarity as an overlay of two horizontal strokes "=" across two forward-slash-like strokes "//".'
  • potatoheadpotatohead Posts: 10,261
    edited 2010-03-05 06:02
    Graham, no worries at all. It displayed correctly. I think unicode fixes that for us, but am not sure.

    Where is your "#" character, and isn't it kind of funny how the discussion goes. Almost like "the artist formerly known as..." kind of thing.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Propeller Wiki: Share the coolness!
    8x8 color 80 Column NTSC Text Object
    Safety Tip: Life is as good as YOU think it is!
  • heaterheater Posts: 3,370
    edited 2010-03-05 06:21
    I don't recommend the use of hash.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
  • BradCBradC Posts: 2,601
    edited 2010-03-05 06:34
    heater said...
    I don't recommend the use of hash.

    It can lead to confusingly convoluted code [noparse];)[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    You only ever need two tools in life. If it moves and it shouldn't use Duct Tape. If it does not move and it should use WD40.
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2010-03-05 11:29
    And to many unfinished objects!

    Potatohead, the # is next to the return key beneath ] and on the same key as ~. Use this knowledge wisely!

    Graham
Sign In or Register to comment.