Playing with Procedures


+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: Playing with Procedures31 days old

  1. #1
    Senior Member AlbertoFS's Avatar
    Join Date
    Apr 2005
    Posts
    688
    Thumbs Up
    Received: 91
    Given: 2
    Total Downloaded
    4.62 GB

    0 Not allowed!

    Default Playing with Procedures

    I am trying to modify all my libraries made with preprocessor and assembler.
    And it doesn't seem so obvious. The macros in assembler have a lot of versatility combining with the preprocessor.
    Les, please, do not remove assembler macros from the compiler.

    I could not continue because the local variables are missing for the procedure. I had managed to build some
    Local Variables for my libraries and the Procedure does not have them yet.

    I use the WIN7 and the compiler version 3.7.2.5.

    First example: I am comparing a macro in assembler and a Procedure.
    For this test I choose a code written in the manual page 435.

    I have a library for local variables, but in this case I will not use it.

    TEST 1: Compare Assembler Macro with Procedure.
    For this first test see the file: TestProcedure-1.bas

    Code: Macro in assembler.
    This Macro could be modified to use more variables: Byte, Word,Dword, SByte,Sword,Sdword.
    Could the same Procedure Input have several input variables?
    Code:
    '/ Declare some Local Variables.
        Dim LocalBVar0 As Byte System
        Dim LocalBVar1 As Byte System    
        Dim LocalSWVar0 As SWord System
    '/------------------------------------------------------------------------------
        Dim LVWordin As LocalSWVar0
        Dim LVDigit As LocalBVar0
        Dim LVDigitLoop As LocalBVar1
    
    '/ Preprocessor & ASM Macro: 62 Bytes (Subroutine) + 5 Bytes (Input Variables + Call + Output Variable)
    $define DoDig16A(pWordin, pDigit) _mDoDig16A pWordin, pDigit
    
    _mDoDig16A Macro- pWordin, pDigit
        #if (Prm_Count != 2)         
            ' Ensure the correct amount of parameters
            #error "Incorrect amount of parameters for _mDoDig16A macro. 2 Parameters required!"    
            Exitm
        #else
            #if (Prm_1 != SWord)
                #error "_mDoDig16A - (Param1) Should be a SWord!"
                Exitm
            #else
                #if (Prm_2 != Byte) && (Prm_2 != Num8)
                    #error "_mDoDig16A - (Param2) Should be a Byte or Number8!"
                    Exitm
                #else
                    #if (Prm_1 == SWord)
                        'Word_Word pWordin,LVWordin
                        Asm
                        Movff pWordin+1,LVWordin+1
                        Movff pWordin,LVWordin
                        EndAsm
                    #endif
                    #if (Prm_2 == Byte)
                        'Byte_Byte pDigit,LVDigit
                        Asm
                        Movff pDigit,LVDigit
                        EndAsm
                    #endif
                    GoSub _mDoDig16A_Sub
                    #if (_mDoDig16A_RETURN != 1)
                        #error "_mDoDig16A - Mandatory return parameter misssing"
                        Exitm
                    #else
                        #if (Return_Type != Byte) 
                            #error "_mDoDig16A - Return variable should be a Byte or Word variable"
                            Exitm
                        #else
                            Wreg_Byte Return_Var
                        #endif
                    #endif
                #endif
            #endif
        #endif                    
    Endm
    
    #ifMacro- _mDoDig16A
    _mDoDig16A_Sub:
        STARTSIZE(DoDig16A_LIB)
        LVWordin = Abs LVWordin
        If LVDigit > 0 Then
            For LVDigitLoop = (LVDigit -1) To 0 Step -1
                LVWordin = LVWordin / 10
            Next
        EndIf
        WREG = LVWordin // 10
        Return
        ENDSIZE(DoDig16A_LIB)
    #endIfMacro-
    Compile and view the asm file.

    #define LVWordin LocalSWVar0 It is a Local Variable.
    #define LVWordinH LocalSWVar0H It is a Local Variable.
    #define LVDigit LocalBVar0 It is a Local Variable.
    #define LVDigitLoop LocalBVar1 It is a Local Variable.
    These variables can be reused.

    Code: By Procedure.
    Code:
    '/ Procedure: 62 Bytes (Subroutine) + 5 Bytes (input Variables + Call + Output Variable)
    Proc DoDig16B(pWordin As SWord, pDigit As Byte), Byte
        Dim LVDigitLoop As Byte
    
    STARTSIZE(DoDig16B_LIB)    
        pWordin = Abs pWordin
        If pDigit > 0 Then
            For LVDigitLoop = (pDigit -1) To 0 Step -1
                pWordin = pWordin / 10
            Next
        EndIf
        Result = pWordin // 10
    ENDSIZE(DoDig16B_LIB)
    EndProc
    Compile and view the asm file.
    DoDig16BpWordin = 0X3B It is NOT a Local Variable.
    DoDig16BpWordinH = 0X3C It is NOT a Local Variable.
    DoDig16BpDigit = 0X3D It is NOT a Local Variable.
    DoDig16BResult = 0X3E It is NOT a Local Variable.
    DoDig16BDigitLoop = 0X3F It is NOT a Local Variable.

    These variables have their own name connected to this procedure. They are global variables.
    The Procedures are under construction and have no variable premises yet. See the manual.
    I have used my <STARTSIZE_ENDSIZE.INC> library to directly calculate the number of bytes of each code.
    It seems that both methods generate the same number of lines of code.

    TEST 2: Test some local variables.

    I found a method for the Procedure to use local variables.
    For this second test see the file: TestProcedure-2.bas

    I have written the procedure this way.
    Code:
    '/ Declare some Local Variables.
        Dim LocalBVar0 As Byte System
        Dim LocalBVar1 As Byte System    
        Dim LocalSWVar0 As SWord System
    
    Proc DoDig16B(pWordin As LocalSWVar0, pDigit As LocalBVar0), Byte
        Dim DigitLoop As LocalBVar1
    
    STARTSIZE(DoDig16B_LIB)    
        pWordin = Abs pWordin
        If pDigit > 0 Then
            For DigitLoop = (pDigit -1) To 0 Step -1
                pWordin = pWordin / 10
            Next
        EndIf
        Result = pWordin // 10
    ENDSIZE(DoDig16B_LIB)
    EndProc
    In the asm file.
    #define DoDig16BpWordin LocalSWVar0 It is a Local Variable.
    #define DoDig16BpWordinH LocalSWVar0H It is a Local Variable.
    #define DoDig16BpDigit LocalBVar0 It is a Local Variable.
    #define DoDig16BDigitLoop LocalBVar1 It is a Local Variable.

    And this code works perfectly with the same variables as the equivalent in assembler.
    The variables are reused.
    I can continue with my library...

    Regards
    Alberto

    Test_Procedures-1.zip
    Last edited by AlbertoFS; 15th October 2019 at 21:57.
    73's de Alberto ea3agv

Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

The advertisements we display are relevant to this web site and your browsing history

Please consider supporting us by disabling your ad blocker.


Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues


Thank you for your attention.

  • #2
    Senior Member AlbertoFS's Avatar
    Join Date
    Apr 2005
    Posts
    688
    Thumbs Up
    Received: 91
    Given: 2
    Total Downloaded
    4.62 GB

    1 Not allowed!

    Default Re: Playing with Procedures

    It's even easier when I use my library of Local Variables <DeclareLocalVars.inc> as follows.
    So I will not have to modify the more complex asm macros.

    Macro in assembler:
    Code:
    '/ Declare some Local Variables.
        LocalByte0(LVDigit)
        LocalByte1(LVDigitLoop)
        LocalSWord0(LVWordin)    
    
    ASM file:
    ;USER ACCESS RAM VARIABLES
    lvs_byte0 = 0X0D
    lvs_byte1 = 0X0E
    lvs_sword0 = 0X0F
    lvs_sword0H = 0X10
    For the Procedure:
    Code:
    
    LocalByte0(LocalBVar0)
    LocalByte1(LocalBVar1)
    LocalSWord0(LocalSWVar0)
    
    In the asm file.
    #define DoDig16BpWordin lvs_sword0       ' It is a Local Variable.
    #define DoDig16BpWordinH lvs_sword0H     ' It is a Local Variable.
    #define DoDig16BpDigit lvs_byte0         ' It is a Local Variable.
    #define DoDig16BDigitLoop lvs_byte1      ' It is a Local Variable.
    Fantastic, they are Local Variables!!!
    Test_Procedures-3.zip
    73's de Alberto ea3agv

  • Attention
    ×

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #3
    Prolific Poster John Drew's Avatar
    Join Date
    Feb 2002
    Posts
    2,777
    Thumbs Up
    Received: 83
    Given: 34
    Total Downloaded
    4.30 GB

    0 Not allowed!

    Default Re: Playing with Procedures

    Greetings Alberto,
    You have a great knowledge of the compiler and finding a way to have real local variables is quite an achievement. In the past Les has explained his solution of pseudo local variables (my words not his) because of the limited stack in our micros. All variables are global but that fact is hidden from us.
    The real strength of Proton procedures is that they are so much easier to understand than macros and secondly they are not compiled if they are not called.
    When I'm free (I'm supposed to be reading a 400 page agenda right now) I'll try out your procedure example.
    I do think that if you can update your programs to using procedures it will be really helpful for your readers.
    Best wishes
    John

  • Attention
    ×

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #4
    Prolific Poster towlerg's Avatar
    Join Date
    Mar 2012
    Posts
    2,208
    Thumbs Up
    Received: 74
    Given: 180
    Total Downloaded
    5.15 GB

    0 Not allowed!

    Default Re: Playing with Procedures

    The only downside I see with with local variables is that they can't be reused
    George.

  • Attention
    ×

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #5
    Senior Member AlbertoFS's Avatar
    Join Date
    Apr 2005
    Posts
    688
    Thumbs Up
    Received: 91
    Given: 2
    Total Downloaded
    4.62 GB

    0 Not allowed!

    Default Re: Playing with Procedures

    Hi George,
    Maybe you are confusing internal variables in a Procedure with local variables.
    I start from the definition that if 2 routines use variables with different names but in the end they are the same variables name in the asm file? I will try to explain my procedure in another way.

    I have written a library, a few years ago, that creates variables that can be reused. The only problem that cannot be used 2 same local variables with same name. As the PDS Procedures change the name in each procedure, this problem never exists for me. What happens that 2 aliases occur, what the compiler and assembler accept.

    My code does the following:

    1- Certain variables are created by my library.
    LocalByte0(LVDigit)
    LocalByte1(LVDigitLoop)
    LocalSWord0(LVWordin)

    What the library produces first.
    USER ACCESS RAM VARIABLES
    lvs_byte0 = 0X0D
    lvs_byte1 = 0X0E
    lvs_sword0 = 0X0F
    lvs_sword0H = 0X10

    2- Then the libray creates aliases.
    Dim LVDigit As lvs_byte0
    Dim LVDigitLoop As lvs_byte1
    Dim LVWordin As lvs_sword0

    Read the ASM file:
    #define LVDigit lvs_byte0
    #define LVDigitLoop lvs_byte1
    #define LVWordin lvs_sword0
    #define LVWordinH lvs_sword0H

    3- Declare again the same variables for the Procedure.
    LocalByte0(LocalBVar0)
    LocalByte1(LocalBVar1)
    LocalSWord0(LocalSWVar0)

    For the Procedure:
    The library does not create again the same variables, but aliases.
    See the asm file.
    #define LocalBVar0 lvs_byte0
    #define LocalBVar1 lvs_byte1
    #define LocalSWVar0 lvs_sword0
    #define LocalSWVar0H lvs_sword0H

    In turn, the Procedure creates another alias.
    #define DoDig16BpWordin lvs_sword0
    #define DoDig16BpWordinH lvs_sword0H
    #define DoDig16BpDigit lvs_byte0
    #define DoDig16BDigitLoop lvs_byte1

    To finish see the compiled code. Only the end of the subroutine changes. I use the WREG variable for the macro. All local variables are identical.

    Macro Subroutine:
    Code:
    _mDoDig16A_Sub
    F1_000126 equ $ ; IN [TESTPROCEDURE-3.BAS] LVWORDIN = ABS LVWORDIN
        btfss lvs_sword0H,7
        bra _pblb__2
        negf lvs_sword0,0
        clrf WREG,0
        subfwb lvs_sword0H,F,0
    _pblb__2
    F1_000127 equ $ ; IN [TESTPROCEDURE-3.BAS] IF LVDIGIT > 0 THEN
        movf lvs_byte0,F,0
        bz _lbl__4
    F1_000128 equ $ ; IN [TESTPROCEDURE-3.BAS] FOR LVDIGITLOOP = (LVDIGIT -1) TO 0 STEP -1
        decf lvs_byte0,W,0
        movwf lvs_byte1,0
    _frlb__5
    F1_000129 equ $ ; IN [TESTPROCEDURE-3.BAS] LVWORDIN = LVWORDIN / 10
        movff lvs_sword0H,PP0H
        movff lvs_sword0,PP0
        clrf PP1H,0
        movlw 10
        movwf PP1,0
        rcall __divide_u1616_
        movwf lvs_sword0,0
        movff PP0H,lvs_sword0H
    _ctlb__7
    F1_000130 equ $ ; IN [TESTPROCEDURE-3.BAS] NEXT
        decf lvs_byte1,F,0
        bc _frlb__5
    _nxlb__6
    F1_000131 equ $ ; IN [TESTPROCEDURE-3.BAS] ENDIF
    _lbl__4
    F1_000132 equ $ ; IN [TESTPROCEDURE-3.BAS] WREG = LVWORDIN // 10
        movff lvs_sword0H,PP0H
        movff lvs_sword0,PP0
        clrf PP1H,0
        movlw 10
        movwf PP1,0
        rcall __divide_u1616_
        movf PP2,W,0
    F1_000133 equ $ ; IN [TESTPROCEDURE-3.BAS] RETURN
        return 0
    Procedure subroutine:
    Code:
    DoDig16B
    F1_000148 equ $ ; IN [TESTPROCEDURE-3.BAS] PWORDIN = ABS PWORDIN
        btfss lvs_sword0H,7
        bra _pblb__16
        negf lvs_sword0,0
        clrf WREG,0
        subfwb lvs_sword0H,F,0
    _pblb__16
    F1_000149 equ $ ; IN [TESTPROCEDURE-3.BAS] IF PDIGIT > 0 THEN
        movf lvs_byte0,F,0
        bz _lbl__18
    F1_000150 equ $ ; IN [TESTPROCEDURE-3.BAS] FOR DIGITLOOP = (PDIGIT -1) TO 0 STEP -1
        decf lvs_byte0,W,0
        movwf lvs_byte1,0
    _frlb__19
    F1_000151 equ $ ; IN [TESTPROCEDURE-3.BAS] PWORDIN = PWORDIN / 10
        movff lvs_sword0H,PP0H
        movff lvs_sword0,PP0
        clrf PP1H,0
        movlw 10
        movwf PP1,0
        rcall __divide_u1616_
        movwf lvs_sword0,0
        movff PP0H,lvs_sword0H
    _ctlb__21
    F1_000152 equ $ ; IN [TESTPROCEDURE-3.BAS] NEXT
        decf lvs_byte1,F,0
        bc _frlb__19
    _nxlb__20
    F1_000153 equ $ ; IN [TESTPROCEDURE-3.BAS] ENDIF
    _lbl__18
    F1_000154 equ $ ; IN [TESTPROCEDURE-3.BAS] RESULT = PWORDIN // 10
        movff lvs_sword0H,PP0H
        movff lvs_sword0,PP0
        clrf PP1H,0
        movlw 10
        movwf PP1,0
        rcall __divide_u1616_
        movff PP2,DoDig16BResult
    F1_000156 equ $ ; IN [TESTPROCEDURE-3.BAS] ENDPROC
        return 0 ; ENDPROC
    For another routine/Procedure you can write again:
    LocalByte0(Var0)
    LocalByte1(Var1)
    LocalSWord0(Var3)
    Etc...

    There is only a little luck that this works!
    Regards
    Last edited by AlbertoFS; 16th October 2019 at 17:38.
    73's de Alberto ea3agv

  • Attention
    ×

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #6
    Prolific Poster towlerg's Avatar
    Join Date
    Mar 2012
    Posts
    2,208
    Thumbs Up
    Received: 74
    Given: 180
    Total Downloaded
    5.15 GB

    0 Not allowed!

    Default Re: Playing with Procedures

    Perhaps I'm wrong or just didn't explain. I quite often find that I have variables that are used by several subroutines/procs, if I make them local to the proc I can't do that. Lets put a pin in weather that is a good idea or not.

    I realize in a complex interrupt stucture that stops it being reenterant but my IRQ routines are simple affairs, set a flag, clear a pointer etc. etc. I would find it just plain annoyed me if I had a local variable called ForCount, in a bunch of procs. Don't care on a PC but on a PIC device every byte a prisoner.
    George.

  • Attention
    ×

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #7
    Prolific Poster John Drew's Avatar
    Join Date
    Feb 2002
    Posts
    2,777
    Thumbs Up
    Received: 83
    Given: 34
    Total Downloaded
    4.30 GB

    0 Not allowed!

    Default Re: Playing with Procedures

    George, in that case why not just declare a global?
    Although that would mess up the easy reading of parameter passing.
    I think Les once suggested that one day he might work on real local variables but how he fits what he currently does in a day beats me.
    John

  • Attention
    ×

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #8
    Member flosigud's Avatar
    Join Date
    Oct 2013
    Posts
    129
    Thumbs Up
    Received: 8
    Given: 12
    Total Downloaded
    2.89 GB

    0 Not allowed!

    Default Re: Playing with Procedures

    Quote Originally Posted by towlerg View Post
    Perhaps I'm wrong or just didn't explain. I quite often find that I have variables that are used by several subroutines/procs, if I make them local to the proc I can't do that. Lets put a pin in weather that is a good idea or not.

    I realize in a complex interrupt sttrtucture that stops it being reenterant but my IRQ routines are simple affairs, set a flag, clear a pointer etc. etc. I would find it just plain annoyed me if I had a local variable called ForCount, in a bunch of procs. Don't care on a PC but on a PIC device every byte a prisoner.
    You can alias local variable with a global one. This wasn't possible at first but now it is. This way you can have local variables with meaningful names that are aliased to something like bTmp.
    Flosi Guğmundsson
    Reykjavík

  • Attention
    ×

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #9
    Prolific Poster towlerg's Avatar
    Join Date
    Mar 2012
    Posts
    2,208
    Thumbs Up
    Received: 74
    Given: 180
    Total Downloaded
    5.15 GB

    0 Not allowed!

    Default Re: Playing with Procedures

    Fosigud, sweet, how do I do that? For example a variable call ForCount that gets used in two procs and in main loop?

    John, unless I'm having another senior moment (see dreaded 3.3/5v jumper incident of '19) any variable dimensioned outside a proc is a Global by default.
    George.

  • Attention
    ×

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #10
    Prolific Poster John Drew's Avatar
    Join Date
    Feb 2002
    Posts
    2,777
    Thumbs Up
    Received: 83
    Given: 34
    Total Downloaded
    4.30 GB

    0 Not allowed!

    Default Re: Playing with Procedures

    Yep, that's right George, sorry if I gave a wrong impression. What, only one mistake in 19. I passed that on January 2nd.
    John

    Try this:
    Code:
    All_Digital =true
    Declare Xtal 10
    Declare LCD_Type 0                'text type
    Declare LCD_DTPin PORTD.4        'assigns data lines to B4..7  
    Declare LCD_ENPin PORTE.1        'enable pin
    Declare LCD_RSPin PORTE.0        'RS line pin
    Declare LCD_Interface 4              '4 or 8 line interface
    Declare LCD_Lines 2                'lines in the display
    Declare Float_Rounding = On
    Declare Float_Display_Type = LARGE
    
    Dim bByteTest As Byte
    
    Proc printit(testbyte As Byte)
         Print At 1,1, Dec bByteTest
    EndProc     
    
    Main:
        bByteTest = 17
        printit(bByteTest)
        Print At 2,1,Dec printit.testbyte
    End

  • Attention
    ×

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #11
    Fanatical Contributor top204's Avatar
    Join Date
    Feb 2002
    Posts
    3,399
    Thumbs Up
    Received: 290
    Given: 141
    Total Downloaded
    1.68 GB

    0 Not allowed!

    Default Re: Playing with Procedures

    As usual Alberto, excellent code and a creative mind.

    The problem with using local variables that are created globally outside of a procedure is that if 2, or more, procedures are using the variables and are calling each other from within themselves, the variable values will be mixed because they are all using them as their own locals. The compiler's local variable mechanism stops that from happening and I am investigating further RAM re-use, but it is a vey complex mechanism and if gotten wrong, different procedures that use each other will overwrite local RAM.

    I'm busy updating the manual with the newer features I've added to the Procedure mechanism, such as a Parameter can now be aliased to another variable or SFR. For example:
    Code:
    Dim MyByte as Byte
    
    Proc MyProc(pParam1 As MyByte)
    
    EndProc
    This comes in useful where certain SFRs are used as a parameter. For example:
    Code:
    Dim wFSR0 as FSR0L.Word
    
    Proc MyProc(ByRef pAddr as wFSR0)
    POSTINC0 = 20
    EndProc
    The above uses the indirect SFRs FSR0L\FSR0H to store the 16-bit address of a variable and the POSTINC0 SFR to load it with a value and auto increment. Extremely useful, and extremely efficient when working with arrays or Strings etc... The same can be done with the TBLPTRL\TBLPTRH SFRs and the BycRef directive when accessing code memory (flash) data such as Dim As Code or Cdata tables.

    Also, the return parameter can be an alias:
    Code:
    Dim wADRES as ADRESL.Word
    
    Proc MyProc(), wADRES
    Result = 1234
    EndProc
    This will use the ADRESL\ADRESH SFRs to hold the 16-bit return from the procedure. Extremey efficient if accessing the ADC within a procedure.

    Something new to watch for in the next Proton compiler version!

    I've only just added a mechansim to declare Global variables from within a procedure as well:
    Code:
    Proc MyProc()
    Global Dim MyByte as Byte
    EndProc
    In the above, the variable "MyByte" will only ever be created if the procedure "MyProc" is called within the program, and "MyByte" is a Global variable that can be used by all of the program. The Global directive works with all variable types and will be a huge bonus to make a library that uses no RAM at all, unless it is initialised, just like the procedures.

    The Global directive will be ready for the next update to the compiler, which will be version 3.7.2.9
    Last edited by top204; 17th October 2019 at 13:22.

  • Attention
    ×

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #12
    Senior Member AlbertoFS's Avatar
    Join Date
    Apr 2005
    Posts
    688
    Thumbs Up
    Received: 91
    Given: 2
    Total Downloaded
    4.62 GB

    0 Not allowed!

    Default Re: Playing with Procedures

    Les, thank you very much for these infos.
    I am very aware that the declaration of variables outside the procedure are global and dangerous. But I do it carefully and only use them at the lowest level of the subroutines so that they do not create reuse problems by accident.

    I was writing several procedures entering variables FSR and TBLPTRL\TBLPTRH. I was worried about the complexity of the system that uses 2 levels of copy to reach the FSR. This will save lines of code. I see these very interesting news.
    As I am moving ASM Macro to Procedures, I keep all the difficulties. It is not possible to change all macros to Procedures, yet. In some days I could make a relationship of those difficulties.

    @George,John
    In fact, every variable declared outside the Procedure is global. The same goes for my Local Variables declared before the Procedure.
    I don't know exactly what the true definition of Local Variable is. I believe that I have written are really Reusable Variables only.
    I have tried the following:
    Code:
    '/ Declare Local reusable variables for input parameters of Procedure.
        LocalByte0(LocalBDD16)
        LocalSWord0(LocalSWDD16) 
    
    Proc DoDig16(pWordin As LocalSWDD16, pDigit As LocalBDD16), Byte
        LocalByte1(DigitLoop)
    
        pWordin = Abs pWordin
        If pDigit > 0 Then
            For DigitLoop = (pDigit -1) To 0 Step -1
                pWordin = pWordin / 10
            Next
        EndIf
        Result = pWordin // 10
    EndProc
    The first 2 Local Variables defined outside the Procedure are used for input variables that are almost always destroyed and can be reused.
    They are compiled this way:
    First, the new variables are defined.
    Code:
    lvs_byte0 = 0X0D
    lvs_sword0 = 0X0E
    lvs_sword0H = 0X0F
    lvs_byte1 = 0X10
    Then the Procedure creates its own variables.
    Code:
    #define LocalBDD16 lvs_byte0
    #define LocalSWDD16 lvs_sword0
    #define LocalSWDD16H lvs_sword0H
    #define DoDig16pWordin lvs_sword0
    #define DoDig16pWordinH lvs_sword0H
    #define DoDig16pDigit lvs_byte0
    
    #define DoDig16DigitLoop lvs_byte1 <= Inside the Procedure

    The first 2 variables declared externally retain their name. They could be used by another Procedure. Normally, I don't do it and I change the name.
    But the Local variable declared within the Procedure has changed its name. It can be said that this variable is really local and reusable. It is very interesting and very useful.
    I hope you use my library, waiting for the Procedures to be updated.

    Alberto
    73's de Alberto ea3agv

  • Attention
    ×

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #13
    Prolific Poster johngb's Avatar
    Join Date
    Oct 2003
    Posts
    2,157
    Thumbs Up
    Received: 95
    Given: 1
    Total Downloaded
    2.59 GB

    0 Not allowed!

    Default Re: Playing with Procedures

    This is exactly the method I used when writing my PRTOS24. I was able to reduce the number of variables need to about 4 which I reused in every procedure by simply aliasing to them. Obviously this only works with variables which are temporal and non persistent.
    JohnB

  • Attention
    ×

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #14
    Fanatical Contributor top204's Avatar
    Join Date
    Feb 2002
    Posts
    3,399
    Thumbs Up
    Received: 290
    Given: 141
    Total Downloaded
    1.68 GB

    0 Not allowed!

    Default Re: Playing with Procedures

    I was just looking at your excellent PRTOS for the 8-bit devices the other day and wondering if I can find the time to make it procedural.

    Also John, can you add the word "Global" to the Studio please? It is a compiler keyword.

    I was busy working on a mechanism for RAM re-use on the Proton24 just before my injury. The procedure Tlist is built from structures and each procedure has a seperate struct within teh Tlist. The struct has everything about the procedure including the procedures it calls and the variables used within the procedure etc... With this information I should, hopefully, find a way of re-using the local variables and parameters in a form of software stack.

  • Attention
    ×

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  • #15
    Prolific Poster John Drew's Avatar
    Join Date
    Feb 2002
    Posts
    2,777
    Thumbs Up
    Received: 83
    Given: 34
    Total Downloaded
    4.30 GB

    0 Not allowed!

    Default Re: Playing with Procedures

    As a minnow in the sea of knowledge in this Forum it seems to me that a software stack is the most reliable way of introducing true local variables. I appreciate the obstacles such as size management but the alternative of keeping the storage of local variables in registers or making use of system variable space is a trickier process and likely to be device specific.
    I'm probably way off track.
    John
    Last edited by John Drew; 18th October 2019 at 04:16. Reason: Clarified stuff

  • Attention
    ×

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

    Thread Information

    Users Browsing this Thread

    There are currently 1 users browsing this thread. (0 members and 1 guests)

       

    Similar Threads

    1. Pickit 2 not playing fair
      By RGV250 in forum The Lounge
      Replies: 6
      Last Post: 27th April 2016, 18:28
    2. Procedures question
      By johngb in forum Proton 24
      Replies: 15
      Last Post: 10th March 2016, 12:01
    3. playing with serial comm's and unexpected results on GLCD
      By pe1pme in forum Proton Plus Compiler v3
      Replies: 0
      Last Post: 19th January 2015, 17:51
    4. Playing back recorded sound stored on pic
      By Tim in forum The Lounge
      Replies: 9
      Last Post: 14th February 2013, 22:48
    5. Playing with strings
      By AlbertoFS in forum The Lounge
      Replies: 1
      Last Post: 20th October 2009, 12:14

    Members who have read this thread since 2nd November 2019, 07:59 : 0

    Actions :  (Set Date)  (Clear Date)

    You do not have permission to view the list of names.

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts