Playing with Procedures


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

Thread: Playing with Procedures174 days old

Hybrid View

  1. #1
    Senior Member
    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

  2. 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. #2
    Senior Member
    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

  4. 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. #3
    Prolific Poster John Drew's Avatar
    Join Date
    Feb 2002
    Posts
    2,858
    Thumbs Up
    Received: 90
    Given: 34
    Total Downloaded
    4.60 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

  6. 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. #4
    Prolific Poster
    Join Date
    Mar 2012
    Posts
    2,318
    Thumbs Up
    Received: 80
    Given: 182
    Total Downloaded
    5.30 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.

  8. 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. #5
    Senior Member
    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

  10. 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. #6
    Prolific Poster
    Join Date
    Mar 2012
    Posts
    2,318
    Thumbs Up
    Received: 80
    Given: 182
    Total Downloaded
    5.30 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.

  12. 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. #7
    Member
    Join Date
    Oct 2013
    Posts
    180
    Thumbs Up
    Received: 8
    Given: 13
    Total Downloaded
    3.14 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

  14. 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. #8
    Prolific Poster johngb's Avatar
    Join Date
    Oct 2003
    Posts
    2,171
    Thumbs Up
    Received: 96
    Given: 1
    Total Downloaded
    2.91 GB

    0 Not allowed!

    Default Re: Playing with Procedures

    @Les
    I will add Global to the list of Keywords. I am currently working on a big revamp of Studio which will provide true docking and floating windows. As this might take some time I will try and get an updated version of the current release out with the global amendment.
    JohnB

  16. 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.

  17. #9
    Prolific Poster John Drew's Avatar
    Join Date
    Feb 2002
    Posts
    2,858
    Thumbs Up
    Received: 90
    Given: 34
    Total Downloaded
    4.60 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

  18. 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.

  19. #10
    Fanatical Contributor Tim's Avatar
    Join Date
    Jan 2003
    Posts
    7,709
    Thumbs Up
    Received: 49
    Given: 95
    Total Downloaded
    1.32 GB

    0 Not allowed!

    Default Re: Playing with Procedures

    From the little I know by talking to someone who wrote a compiler that handles "proper" procedures and local variables.

    The compiler has to follow the paths of calls etc to see if a variable is used another sub. If it does then it will assign is its variable. It's never a PIC (tm) related issue.

    The compiler by said writer is very powerful and can do fantastic work. However it ends up using a lot of variables to start with. But then if you have 1000 variables and using 500 for a small program really makes no odds. In larger programs it comes into its own.

    I am a useless coder and only get stuff done using the brute force method. EG keeping pounding away at it until I understood what I was doing and get the job done. Its very time consuming and means I'm very limited in my knowledge. I put the effort into learning Proton and really love it. Procedures to me at my level just make the code look nice and neat, there is no other advantage. I never write reusable code blocks as I just never code enough to warrant the effort. Also variable usage is never an issue. I always have loads spare.

    The one time I did have to worry about code space and variables I just looked through the ASM to see what lines took the most space and rewrote my code line to make it smaller. Also variables I use bTemp1, bTemp2 etc (the prefixing variable type is fantastic, thanks for that tip Les!). If I jump to another routine I just remember what variables I used in the previous one. As I said I write every time from scratch.

    One last thing the really big code job was so tight no other compiler would have managed it. Proton is really really tight. No other compiler will match it.
    Tim

  20. 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.

  21. #11
    Prolific Poster johngb's Avatar
    Join Date
    Oct 2003
    Posts
    2,171
    Thumbs Up
    Received: 96
    Given: 1
    Total Downloaded
    2.91 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

  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.

  23. #12
    Fanatical Contributor top204's Avatar
    Join Date
    Feb 2002
    Posts
    3,517
    Thumbs Up
    Received: 319
    Given: 153
    Total Downloaded
    1.99 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.

  24. 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.

  25. #13
    Prolific Poster John Drew's Avatar
    Join Date
    Feb 2002
    Posts
    2,858
    Thumbs Up
    Received: 90
    Given: 34
    Total Downloaded
    4.60 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

  26. 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.

  27. #14
    Prolific Poster John Drew's Avatar
    Join Date
    Feb 2002
    Posts
    2,858
    Thumbs Up
    Received: 90
    Given: 34
    Total Downloaded
    4.60 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

  28. 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.

  29. #15
    Fanatical Contributor top204's Avatar
    Join Date
    Feb 2002
    Posts
    3,517
    Thumbs Up
    Received: 319
    Given: 153
    Total Downloaded
    1.99 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.

  30. 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, 11: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, 16:51
  4. Playing back recorded sound stored on pic
    By Tim in forum The Lounge
    Replies: 9
    Last Post: 14th February 2013, 21:48
  5. Playing with strings
    By AlbertoFS in forum The Lounge
    Replies: 1
    Last Post: 20th October 2009, 12:14

Posting Permissions

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