Shop OBEX P1 Docs P2 Docs Learn Events
4 byte function? — Parallax Forums

4 byte function?

skynuggetskynugget Posts: 172
edited 2010-01-05 05:32 in General Discussion
hey all, can someone tell me why this doesn't compile ?

SX/B VERSION 2.00.30
Unable to assemble due to errors in source code:

... <__ PARAM9> is not defined
... <__ PARAM13> is not defined

TEST4 FUNC 4 'setup function to return 4 bytes


' =========================================================================
  PROGRAM Start
' =========================================================================
start:
    string = test4
goto start
' -------------------------------------------------------------------------
' Subroutine / Function / Task Code
' -------------------------------------------------------------------------
func TEST4
    b1 var byte
    b2 var byte
    b3 var byte    
    b4 var byte

    b1 = 11 'plug in random values
    b2 = 99
    b3 = 91
    b4 = 10

return    b1,b2,b3,b4
endfunc    




Thanks again!

Post Edited (skynugget) : 1/3/2010 12:39:37 AM GMT

Comments

  • JonnyMacJonnyMac Posts: 9,211
    edited 2010-01-03 04:30
    It would be easier to check your program if you'd post it -- the code above is incomplete.
  • skynuggetskynugget Posts: 172
    edited 2010-01-03 05:04
    yea sorry about that, haste makes waste when ya copy and paste! i should have explained better.
    i was working on something else, that i scrapped(and didn't save, doh), but it left me thinking.
    Though this code is incomplete, i would think it should compile given that i would have remembered to paste the following in the example:
    string var byte(4)
    
    



    i tried to come up something simple basically to ask if its a bug, or if im just dumb and don't get it the syntax

    i thought you could return a max of 5 bytes from a function, and the get stored in __PARAM1 thru __PARAM5
    i dint know param9 and param13 existed.. i kinda thought it was a bug... but ive been known to be pritty dense sometimes.

    Post Edited (skynugget) : 1/3/2010 5:10:19 AM GMT
  • JonnyMacJonnyMac Posts: 9,211
    edited 2010-01-03 07:07
    You can have a function return five bytes -- you just have to declare that.

    I have personally written hundreds of SX/B programs and helped weed out a lot of "gotchas" (I'm part of the development team); in all that time I have never seen the compiler complain about [noparse][[/noparse]the non-existent] __param9 or __param13. Again, had you attached your code (the whole thing, not just fragments), one of use could compile it on our end and give you better guidance.

    Note that SX/B can only return two bytes using the form

    someVariable = SomeFunction

    ...where 'someVariable' is a word that will receive __wparam12 (__param1 and __param2).

    In order to get the the others you need to capture them manually, right after the call; in your case:

    myVar(0) = TEST_FUNC
    myVar(1) = __param2
    myVar(2) = __param3
    myVar(3) = __param4

    Note that you shouldn't use symbols like string and test; these are reserved words in other flavors of BASIC and might create troubles.

    Post Edited (JonnyMac) : 1/3/2010 7:17:50 AM GMT
  • JonnyMacJonnyMac Posts: 9,211
    edited 2010-01-03 07:21
    Another way to populate an array within a fuction or subroutine is to pass the address (@) of the array and use the __RAM() array to populate it:

    SUB FILL_4
      pntr    VAR     tmpB1
    
      pntr = __param1
      __RAM(pntr) = 1
      INC pntr
      __RAM(pntr) = 2
      INC pntr
      __RAM(pntr) = 3
      INC pntr
      __RAM(pntr) = 4
      INC pntr
      ENDSUB
    



    To call the above function:

    FILL_4 @myArray
    




    Another note: Local variables generate a butt-load of code; I don't recommend them unless they're absolutely necessary (I've never found that to be the case).
  • skynuggetskynugget Posts: 172
    edited 2010-01-03 13:58
    here is the code that produces the param9 and param13 errors SASm errors if you are curious.

    So I was using it wrong?

    a sub can handle 5 bytes, but RETURN can only return 2 bytes not 5, and you have to work around that?

    thanks for the tip on the local vars, i didn't realize they create such a mess.. cool idea though

    note:
    If i make the local variables global, it compiles the file, and the sasm source uses param 1 through 4
    so the local variables where using up the Param space? i thought they where crammed in the stack space?


    thanks again for helping, and putting up with my dingy-ness
  • JonnyMacJonnyMac Posts: 9,211
    edited 2010-01-05 05:32
    Bean will have to look at it -- I can confirm what you're seeing, but then, the way you're trying to use local variables may be inciting this.

    Using local variables in SX/B is a bad idea; it complicates the output and generates a huge amount of extra code. While a function can in fact return four bytes you cannot move them into an array as you're attempting to do. I showed you how to move data to an array using the __RAM() pointer; I've updated your program to let you see for yourself.

    Post Edited (JonnyMac) : 1/5/2010 5:37:17 AM GMT
Sign In or Register to comment.