Shop OBEX P1 Docs P2 Docs Learn Events
How to implement CALL in LMM ? — Parallax Forums

How to implement CALL in LMM ?

BeanBean Posts: 8,129
edited 2010-02-03 22:25 in Propeller 1
I've just been playing around with LMM and was trying to figure out how to implement CALL #label.

Here is what I've come up with, but it seems like alot of instructions. And requires 2 temp variables.

Is there an easier way ?

  CALL #label ->      rdlong temp,pc  ' temp = address of "LONG 0-0"
                      LONG @label_RET
                      mov temp2,pc    ' pc=next instruction
                      add temp2,#16   ' add 4 instructions
                      wrlong pc,temp  ' set return address
                      rdlong pc,pc    ' jump to label
                      LONG @label
                      ...
                    label
                      ...
   RET        ->      rdlong pc,pc    ' jump back to caller
                    label_RET
                      LONG 0-0


I'm not concerned about handling a CALL in PASM. I'll just generate whatever code is needed to replace CALL.

Bean
·

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Use BASIC on the Propeller with the speed of assembly language.

PropBASIC thread http://forums.parallax.com/showthread.php?p=867134

Comments

  • AleAle Posts: 2,363
    edited 2010-02-03 19:51
    I use this:

    krnl_lcall      mov     krnl_lr,krnl_pc ' save PC in link register
                    add     krnl_lr,#4
                    rdlong  krnl_pc,krnl_pc ' address of destination
                    jmp     #krnl_fetch
    
    krnl_ljmp       rdlong  krnl_pc,krnl_pc   ' reads address of destination
                    jmp     #krnl_fetch
    
    ' LMM code follows
    
                    jmp     #krnl_lcall
                    long     routine_to_call
    
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Visit some of my articles at Propeller Wiki:
    MATH on the propeller propeller.wikispaces.com/MATH
    pPropQL: propeller.wikispaces.com/pPropQL
    pPropQL020: propeller.wikispaces.com/pPropQL020
    OMU for the pPropQL/020 propeller.wikispaces.com/OMU
  • BeanBean Posts: 8,129
    edited 2010-02-03 20:40
    Ale,
    I don't see how nested subroutines would be handled ? Is there something I'm missing ?

    Bean

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Use BASIC on the Propeller with the speed of assembly language.

    PropBASIC thread http://forums.parallax.com/showthread.php?p=867134
  • Bill HenningBill Henning Posts: 6,445
    edited 2010-02-03 20:54
    You have to maintain a software based return stack, either in-cog (faster) or in hub memory (slower, but larger stack possible)
    Bean said...
    Ale,
    I don't see how nested subroutines would be handled ? Is there something I'm missing ?

    Bean
    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.mikronauts.com E-mail: mikronauts _at_ gmail _dot_ com 5.0" VGA LCD in stock!
    Morpheus dual Prop SBC w/ 512KB kit $119.95, Mem+2MB memory/IO kit $89.95, both kits $189.95 SerPlug $9.95
    Propteus and Proteus for Propeller prototyping 6.250MHz custom Crystals run Propellers at 100MHz
    Las - Large model assembler Largos - upcoming nano operating system
  • RossHRossH Posts: 5,519
    edited 2010-02-03 22:25
    Hi Bean,

    As Bill mentioned, you need a call stack - here is how Catalina does it (the stack is in hub RAM and only requires one or two additonal instructions to manage):

    f_ret
           rdlong PC,SP         ' read the PC
           add    SP,#4         ' increment the SP
           jmp    #LMM_loop     ' execute the next instruction
    '
    ' f_call - call the routine at the address pointed to by the PC (increment the PC, save return address on stack)
    '
    f_call                    
           rdlong RI,PC         ' get the address to call
           add    PC,#4         ' increment the PC (this is the return address)
           sub    SP,#4         ' decrement the SP
           wrlong PC,SP         ' save current PC to stack
           mov    PC,RI         ' get the address to jump to
           jmp    #LMM_loop     ' execute next instruction
    
    



    Then a call and a return might look like:
    my_function
           ... do_stuff_here ...
           jmp #f_ret
    
    my_main
           ... do stuff ...
           jmp #f_call
           long @my_function
           ... do_more_stuff ...
    
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Catalina - a FREE C compiler for the Propeller - see Catalina
Sign In or Register to comment.