ASM within a FlexBASIC routine

JRoarkJRoark Posts: 197
edited 2019-12-02 - 14:02:01 in Propeller 2
After three hours of headbanging, I'm ready to swallow my pride and ask the experts. :)

I'm trying to dip my toe into putting assembly code inside of a FlexBASIC function. Here is what I'm trying to do (as an example).
   dim myVar, shifts as uLong
   myVar = 1024
   shifts = 8
   print BitShiftRight(myVar, shifts)

FUNCTION BitShiftRight(x as ulong, y as ulong) as ulong
'shifts X to right Y places
   dim theAnswer as ulong
   ASM
        (dark magic happens here to get stuff into, and out of the assembly block after shifting X to the right Y places)
   END ASM

   return theAnswer

END FUNCTION
I feel like a total noob here. The shifts I have no problem with. I can write this in standalone assembly. But I have utterly no idea how to "hook-up" the assembly stuff to the input params, and then "get the answer" out of the assembly block and into the BASIC RETURN value. Can anyone enlighten me how this works?

Comments

  • Oh for the love of Pete. It turns out I was REALLY overthinking this. The answer is frighteningly simple.
    FUNCTION BitShiftRight(x as ulong, y as ulong) as ulong
    'shifts X to right Y places
    
       ASM
          SHR	x, y
       END ASM
    
       return x
    
    END FUNCTION
    
    That's it. No fancy pointers. No address-of. No mucking about with fancy passing methods.

    Now if someone could please hand me the dunce hat, I'll go work on some plumbing or something... :)
  • JRoark wrote: »
    Oh for the love of Pete. It turns out I was REALLY overthinking this. The answer is frighteningly simple.
    FUNCTION BitShiftRight(x as ulong, y as ulong) as ulong
    'shifts X to right Y places
    
       ASM
          SHR	x, y
       END ASM
    
       return x
    
    END FUNCTION
    
    That's it. No fancy pointers. No address-of. No mucking about with fancy passing methods.

    Now if someone could please hand me the dunce hat, I'll go work on some plumbing or something... :)

    LOL, you are not alone when when it comes to missing the simple and obvious when immersed in something that is usually more complex. Done the forehead smack many times after spending time on what turns out to be very simple.
    In science there is no authority. There is only experiment.
    Life is unpredictable. Eat dessert first.
  • @kwinn Yeah, this was definitely one of those “Captain Obvious” moments. I’m also sort of glad to see that a temporary case of “Dumbo-itis” is occasionally manifested in others too.

    I’m really trying to figure a way to blame @ersmith for this. But it just isnt working... like... at all.

    In retrospect, I have to admit that FlexBASIC really is da bomb. It even makes *me* look good when I’m being obtuse. Lol!
  • ersmithersmith Posts: 3,640
    edited 2019-12-03 - 11:47:04
    I did have an example in the basic.pdf discussion about ASM, but it showed a subroutine rather than a function. I'll add a function example as well.
    FlexGUI, a GUI for programming the P1 and P2 in Spin, PASM, BASIC, and C.
    Help support its development at Patreon or PayPal.
  • JRoarkJRoark Posts: 197
    edited 2019-12-03 - 16:34:55
    @ersmith That would be great! You might also add a blurb on what registers/flags/stack (etc) we can safely monkey with in our asm code, and which need to be kept pristine (or at least restored to the entry state before leaving).

    FWIW, I am really enjoying the Flex suite. You’ve done some amazing work here.
  • @JRoark: Thank you for your kind words, and for your helpful bug reports. It's great to have help in developing these applications!
    FlexGUI, a GUI for programming the P1 and P2 in Spin, PASM, BASIC, and C.
    Help support its development at Patreon or PayPal.
Sign In or Register to comment.