Announcement

Collapse
No announcement yet.

some jumps

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • some jumps

    the following droves me crazy. please take the code
    and step through the code in the pb debugger. i was really
    wondering, that i got half baken results without doing
    anything in my asm procedure.

    Code:
    $DIM ALL
    #REGISTER NONE
    #COMPILE EXE
    #INCLUDE "WIN32API.INC"
    
    
    FUNCTION F1() AS LONG
    
        DIM x AS LONG
        x = 3
    l1:
        x = x -1
        IF x > 0 THEN
            GOTO l1
        END IF
    
    END FUNCTION
    
    FUNCTION F2() AS LONG
    
        DIM x AS LONG
        x = 3
    l1:
        x = x -1
        IF x > 0 THEN
            GOTO l1
        END IF
        
    END FUNCTION
    
    FUNCTION F3() AS LONG
    
        !pushad
        !mov    esi ,3
    l1:
        !dec    esi
        !jnz    l1
        !popad
    END FUNCTION
    
    FUNCTION F4() AS LONG
    
        !pushad
        !mov    esi ,3
    l1:
        !dec    esi
        !jnz    l1
        !popad
    END FUNCTION
    
    
    FUNCTION PBMAIN() AS LONG
    
        CALL F2()
        CALL F4()
       
    END FUNCTION
    ------------------




    [This message has been edited by Torte Torsten (edited August 05, 2001).]

  • #2
    Torte,

    Labels need to be unique, It is not good enough that they are unique
    in a given sub or function, they need to be unique in the whole program.

    The jump in f4 goes to the label in f1 and that is not good.

    Regards
    Trevor

    ------------------

    Comment


    • #3
      you are right, trevor, but it would be better the
      compiler reminds me, before -after hours- i have to
      install a very strong personal service pack.

      regards

      torsten or i am not sure

      ------------------


      [This message has been edited by Torte Torsten (edited August 04, 2001).]

      Comment


      • #4
        Originally posted by Trevor Lane:
        Torte,

        Labels need to be unique, It is not good enough that they are unique
        in a given sub or function, they need to be unique in the whole program.
        That statement is for all I know only true for non PBDLL/60 PBCC/20 compiler



        ------------------
        Fred
        mailto:[email protected][email protected]</A>
        http://www.oxenby.se

        Fred
        mailto:[email protected][email protected]</A>
        http://www.oxenby.se

        Comment


        • #5
          Hello Torte,

          I just noticed in functions F3 and F4 you are forgot to !POPAD after you had !PUSHAD.

          ------------------
          Cheers!

          Comment


          • #6
            mark
            ooops

            guys (and girls), what do you think? It's time for pb7 compiler,
            isn't it?

            ------------------

            Comment


            • #7
              I seem to be wrong here, and the documentation as well.
              in assembler, jump to a label is not behaving according to doc.
              Sorry for my ignorant remark...

              ------------------
              Fred
              mailto:[email protected][email protected]</A>
              http://www.oxenby.se

              Fred
              mailto:[email protected][email protected]</A>
              http://www.oxenby.se

              Comment


              • #8
                Labels must be unique to the Sub/Function in which they are located. You can use the same label name in other subs/functions.

                However, the current version of the compiler still permits out-of-scope label reference with inline-assembler code. Out-of-scope jumping such as shown above is almost certain to cause problems!

                Also, R&D have said that it is quite likely that future versions of PowerBASIC will prevent inline-assembler code doing out-of-scope jumps, etc.

                ------------------
                Lance
                PowerBASIC Support
                mailto:[email protected][email protected]</A>
                Lance
                mailto:[email protected]

                Comment


                • #9
                  Torte,

                  The repeat labels are only 1 problem, you are using PUSHAD without
                  POPAD after it so you stack is a mess as well.

                  They are symetrical stack instructions so they should be matched,
                  its something like doing a PUSH without a natching POP after it
                  somewhere. Thsi example is too short to show it but it will crash
                  a normal size app id you do it there.

                  Regards,

                  [email protected]

                  ------------------
                  hutch at movsd dot com
                  The MASM Forum

                  www.masm32.com

                  Comment


                  • #10
                    hi all,

                    i have added the forgotten !popad. you are right, an example
                    must be complete. next time i will take more care about code
                    i am posting

                    my intention was to show a strange behaviour, that confused me.
                    first i wrote an image manipulation function using standard asm.
                    after that i wrote the mmx optimized version. for the mmx version
                    i took the source code from the standard asm function as base.
                    so came one jump label in two function. the joke is, that in the
                    first loop the mmx code was taken, in all next loops the asm code.
                    so i got very strange result images, and could not explain why.

                    i guess that many of us are copying and pasting code.

                    lance, the choice of your words underestimates the problem.
                    you write "still permit" in german is it "erlaubt noch". fact
                    is, "still premit" must be pronounced "always, by design".
                    the scope for asm jumps seems to be the entire executable module.
                    asm jumps takes the first label with the given name and this
                    causes serious problems. you must maintain all lables in a database
                    or something similar to avoid "out of scope jumping".
                    this makes a lot of trouble, if you work in a developer team.
                    yes, there are naming conventions, but sometimes it takes more
                    time to find and write a proper name than to write the whole procedure.
                    i prefer to let x be x, instead of making x to
                    x_in_this_module_in_this_procedure_in this_context_at_this_position.
                    that means anything i declare in a procedure is private, that include labels.

                    i call for treating this jump problem as serious problem (bug?).

                    regards,

                    torsten

                    p.s.: i love pb.



                    ------------------

                    Comment


                    • #11
                      This matter was discussed a while agon on the NG.
                      I still think this a handy feature.
                      use basic code as it should, use asm for particular cases.

                      The example above is not so great i think, it can be replaced with a do loop etc..
                      (Of course)
                      Why is the label so important to use it with asm.
                      I would use goto.

                      ??


                      ------------------
                      hellobasic

                      Comment


                      • #12
                        Edwin,

                        Using GOTO is supposed to turn you code into spaghetti where JMP
                        is respectable assembler. Since everybody is into "structured"
                        code, one has to observe such serious fashions and avoid these
                        old programming bad habits.

                        If you get your jumps right, you will end up with a real pizza
                        which has to be better than spaghetti

                        Regards,

                        [email protected]

                        ------------------
                        hutch at movsd dot com
                        The MASM Forum

                        www.masm32.com

                        Comment


                        • #13
                          hi, (unfortunatly english is not my native language.)

                          the goto's in f1/f2 are only there for demonstration purposes.
                          if you are making real time image processing, you
                          will try gosub/goto against a function call for sub processing.
                          any function call produces some overhead, so gosub/goto is faster.
                          if any cpu tick counts, you are happy, to be able to use such old fashioned
                          programming style. PowerBasic rocks that way, and because of that
                          fact i love PowerBasic.

                          if you are programming routines in asm/mmx, the asm source code
                          looks always spaghetti.

                          somebody said, use the right tool for the right job. that is correct,
                          but you have to accept that any tool has it's own special flavour.
                          if you use asm/mmx you have to deal with some spaghetti like source code.

                          for my current work i mesured the following for processing a
                          640x480px24bit image, using gettickcount(not very exact, but exact enough).

                          standard pb syntax 70ms
                          standard asm syntax 50ms
                          mmx syntax 0ms

                          the problem is not that i could use do..loop, while..wend, etc,
                          the problem is that, if i make an asm-jump, i do not really know,
                          where i land.

                          regards, and have a nice week.

                          torsten

                          ------------------

                          Comment


                          • #14
                            For the life of me I have trouble seeing how assembler
                            can be considered anything but spaghetti del grande yet
                            jumping to a label with Goto would be considered spaghetti. Sure, the old days of jumping all over a program (no Functions; no Subs) with Goto/Gosub was spaghetti - albeit unavoidable! However, even then it resembled assembler in a spaghetti kind of way.

                            Gotoing within a Sub/Function is usually done to prevent the inclusion of many additional lines of code and the resulting inefficiency in order to keep the code "structured", pretty and easy to read. (What's so hard about reading the word Goto?)

                            One of the following is a horrible programming practice. Can you identify the offensive code?
                            Code:
                            Label1:
                                !jmp Label1
                             
                            Label1:
                                Goto Label1

                            Comment


                            • #15
                              Ron,

                              I thought someone would get the tongue in cheek approach to GOTO
                              versus the REAL thing. While I still will short circuit "nice"
                              looking structured code with a jump of some type, I was taking a
                              swipe at fashion, GOTO is BADDDDDDDD, structured is GOOOOOOOD.

                              Now the simple way around the problem so you can write fashionable
                              code that goes fast too is to do your branching with either conditional
                              or unconditional jumps.

                              Roughly the distinction is that you can make HOT spaghetti but
                              a good pizza really sizzles. Its probably one of the world's worst
                              kept secrets that GOTO is usually translated to a JMP during the compiler
                              stage so the distinction is vacuous. At least JMP means you are trying
                              to make it go fast.

                              Regards,

                              [email protected]

                              ------------------
                              hutch at movsd dot com
                              The MASM Forum

                              www.masm32.com

                              Comment


                              • #16
                                Goto's don't make spaghetti, people do!

                                Lance, I favor the idea of preventing this behavior, but in the interest
                                of preserving existing code (if any use this capability) I hope R&D
                                will make it an option for us to sellect.

                                I always felt PB did not have enough compile options, especially
                                optimization options.

                                ------------------
                                Thanks,

                                John Kovacich

                                [This message has been edited by John Kovacich (edited August 06, 2001).]
                                Thanks,

                                John Kovacich
                                Ivory Tower Software

                                Comment


                                • #17
                                  Out-of-scope jumps in inline-assembler will very likely be prevented in future versions of PowerBASIC (since they are very dangerous as the stack frame is very likely to be corrupted - leave this kind of technique to someone who knows what they are doing!!!), however, there are ways around it. For example, the target sub/function could be designed to return the CODEPTR of a label to the calling code, and then a GOTO DWORD used with the returned address.

                                  Just don't blame us when an out-of-scope jump causes you problems!

                                  ------------------
                                  Lance
                                  PowerBASIC Support
                                  mailto:[email protected][email protected]</A>
                                  Lance
                                  mailto:[email protected]

                                  Comment

                                  Working...
                                  X