Shop OBEX P1 Docs P2 Docs Learn Events
SUBs, FUNCs, #s of Parameters — Parallax Forums

SUBs, FUNCs, #s of Parameters

PJAllenPJAllen BannedPosts: 5,065
edited 2008-07-25 04:10 in General Discussion
I have a couple of related questions regarding SUB and FUNC.
·
I have read the SX/B Help and have included part of the program
for the I2C experiment therein.

SUB min, max -- min is the minimum number of required Parameters, max is the maximum number of required parameters.
FUNC Return Count, Min, Max -- min is the minimum number of
parameters passed to the FUNCtion, max is the maximum number of
parameters passed to the FUNCtion.

Given:
MEM_OUT· SUB· 3
... and ...
SUB MEM_OUT has __WPARAM12, __PARAM3, tmpW1, tmpB1.·
Which are the parameters (re. min = 3)?· Are they "__WPARAM12, __PARAM3"; min = #parameters + 1 (n+1)?· Is it 3 because __WPARAM is a Word (2 bytes) and __PARAM is 1 byte, 2+1=3?

Given:
MEM_IN· FUNC· 1,2
... and ...
FUNC MEM_IN has __WPARAM12, tmpW1, tmpB1.·
Which are the parameters (re. min = 2)?· Is it __WPARAM12 (n+1)?· Is it 2 because __WPARAM12 is a Word (2 bytes)?

' ---------------------------------
' Variables
' --------------------------------- 
addr     VAR  Word
addrLo   VAR  addr_LSB
addrHi   VAR  addr_MSB
outVal   VAR  Byte
inVal    VAR  Byte
tmpW1    VAR  Word
tmpB1    VAR  Byte
tmpB2    VAR  Byte
tmpB3    VAR  Byte
tmpB4    VAR  Byte
[color=red]MEM_OUT  SUB  3       ' ***********
MEM_IN   FUNC 1,2     ' ***********[/color] 

SUB MEM_OUT
  tmpW1 = __WPARAM12
  tmpB1 = __PARAM3
  I2CSTART SDA
  tmpW1_MSB = tmpW1_MSB & $03
  tmpW1_MSB = tmpW1_MSB << 1
  tmpW1_MSB = tmpW1_MSB | SlaveID
  tmpW1_MSB.0 = 0
  I2CSEND SDA, tmpW1_MSB
  I2CSEND SDA, tmpW1_LSB
  I2CSEND SDA, tmpB1
  I2CSTOP SDA
  ENDSUB
FUNC MEM_IN
  tmpW1 = __WPARAM12
  I2CSTART SDA
  tmpW1_MSB = tmpW1_MSB & $03
  tmpW1_MSB = tmpW1_MSB << 1
  tmpW1_MSB = tmpW1_MSB | SlaveID
  tmpW1_MSB.0 = 0
  I2CSEND SDA, tmpW1_MSB
  I2CSEND SDA, tmpW1_LSB
  I2CSTART SDA
  tmpW1_MSB.0 = 1
  I2CSEND SDA, tmpW1_MSB
  I2CRECV SDA, tmpB1, Nak
  I2CSTOP SDA
  RETURN tmpB1
ENDFUNC


·

Comments

  • BeanBean Posts: 8,129
    edited 2008-07-23 23:31
    Yes the value is the number of BYTES, not the number of parameters.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    "A government big enough to give you everything you want, is big enough to take away everything you·have."·· Thomas Jefferson

    "It is our choices, Harry, that show what we truly are, far more than our abilities."·Dumbledore from Harry Potter

    www.iElectronicDesigns.com

    ·
  • JonnyMacJonnyMac Posts: 9,216
    edited 2008-07-23 23:37
    That's probably a legacy issue in the file as the original version of SX/B only supported bytes and bits, so a parameter always was a byte.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2008-07-23 23:40
    Thanks for the clarifications.
  • Sparks-R-FunSparks-R-Fun Posts: 388
    edited 2008-07-24 00:25
    [noparse][[/noparse]I guess this is a late entry...]


    PJ,

    I think the word "parameters" can be somewhat misleading because, as you are asking, what makes a parameter? In this context a parameter is a byte that is passed. Therefore, the min and max values refer to the number of bytes that are passed into the subroutine.

    MEM_OUT  SUB  3
    

    Requires three bytes be passed to the subroutine. If you fail to pass at least three bytes when calling it the compiler should warn you of this condition.

    NOTE: Because you have not specified a maximum number of bytes you can pass more than three if you like.

    Inside the subroutine you can access the three bytes (or possibly more) in any way that makes sense to you and your program.
    __PARAM1 holds the value of the first byte.
    __PARAM2 holds the value of the second byte.
    __PARAM3 holds the value of the third byte.
    __PARAM4 might hold the value of the fourth byte if you passed one in.
    __PARAM5 might hold the value of the fifth byte if you passed one in. (See the additional comments following this post.)

    __WPARAM12 holds the value of the first two bytes together as a word value.
    __WPARAM23 holds the value of the second and third bytes together as a word value.
    __WPARAM34 holds the value of the third and fourth bytes together as a word value.
    __WPARAM45 holds the value of the fourth and fifth bytes together as a word value.

    Normally it makes the most sense to refer to the parameters according to the type of data being passed into the subroutine, meaning you use the corresponding byte parameter where you expect a byte and a word parameter where you expect a word value. But those parameters are just assigned aliases for the data in those positions. If you pass in three bytes of data you could access those values using __WPARAM12 to access the first two bytes together as a word and __WPARAM23 to access the second and third bytes together as a word. However, unless your program is extremely clever that probably will not make good programming sense. But you can still do it. You can also pass in a word value and access the data in the subroutine separately as two distinct bytes. There are cases when this may be helpful.

    MEM_IN   FUNC 1,2
    

    With this definition you can must pass in a byte, two bytes or a word variable but nothing or possibly more. The 1 states that at least one byte is required will be returned and the 2 states that at most least two bytes can must be transfered in. The key to understanding what can be passed is to understand the number of bytes represented. (And to recognize that a FUNC declaration starts by specifying the number of bytes returned while a SUB declaration starts with the minimum number of bytes that must be passed into it.)

    Since the discussion has been about the subroutine parameter aliases I should point out the importance of using or reassigning these values almost immediately in the subroutine. Many of the more complex SX/B commands actually use those temporary addresses for their own needs and in the process overwrite the values that were originally passed in. Therefore be sure to reassign them to more suitable variable spaces (as you have already done) unless you are certain they will remain intact until needed.

    I hope this makes sense.

    - Sparks



    [noparse][[/noparse]Edited to more accurately reflect the corrections mentioned below.]

    Post Edited (Sparks-R-Fun) : 7/25/2008 4:31:34 AM GMT
  • JonnyMacJonnyMac Posts: 9,216
    edited 2008-07-24 00:48
    Sparks,

    In a FUNC (like MEM_IN), the first value is the number of bytes returned, the second is the minimum bytes the function will accept, the third (if used) is the maximum number of bytes the function will accept. So, the declaration of MEM_IN that you cite above is stating that it will return one byte and expects you to pass two bytes (a word) to it; the word value is the address within the device, the return value is the byte that was read from that address.
  • ZootZoot Posts: 2,227
    edited 2008-07-24 05:21
    Somebody said...
    __PARAM5 might hold the value of the fifth byte if you passed one in.

    PJ -- one clarification regarding __PARAM5. __PARAM5 is an alias of __PARAMCNT, which is really the more useful of the two. __PARAMCNT is available to the sub/func and is a count of BYTES in the parameters. So you can have subs/funcs that accept a flexible number of byte parameters and which parse the count at the start of the subroutine to decide how to proceed.

    __PARAM5 will only hold an actual fifth param byte if the sub/func is passed 5 bytes (the max allowed param count for any SX/B subroutine), in which case there is no param count.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
  • BeanBean Posts: 8,129
    edited 2008-07-24 13:01
    Note that __PARAMCNT !!ONLY!! gets set IF the sub/func allows a variable number of parameters.
    So if the sub/func only allows 2 parameters, then __PARAMCNT will NOT be set to two.

    Also __PARAM5 can only be used IF the sub/func expects exactly 5 parameter bytes.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    "A government big enough to give you everything you want, is big enough to take away everything you·have."·· Thomas Jefferson

    "It is our choices, Harry, that show what we truly are, far more than our abilities."·Dumbledore from Harry Potter

    www.iElectronicDesigns.com

    ·
  • Sparks-R-FunSparks-R-Fun Posts: 388
    edited 2008-07-25 04:10
    Thanks all.

    I am enlightened! smile.gif


    - Sparks
Sign In or Register to comment.