Shop OBEX P1 Docs P2 Docs Learn Events
ASM within a FlexBASIC routine — Parallax Forums

ASM within a FlexBASIC routine

JRoarkJRoark Posts: 1,215
edited 2019-12-02 14:02 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... :)
  • kwinnkwinn Posts: 8,697
    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.
  • @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: 6,053
    edited 2019-12-03 11:47
    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.
  • JRoarkJRoark Posts: 1,215
    edited 2019-12-03 16:34
    @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!
Sign In or Register to comment.