Announcement

Collapse
No announcement yet.

InLine Assembler

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

  • InLine Assembler

    The questions are :

    a)When we write an inline assembler routine which of the registers that we use we MUST save ?

    b)The following subs are a small simple cipher routines i wrote using pb statements only in the Cipher sub and inline asm in the CipherAsm sub
    trying to make it faster.Is there anything that i can do to make the ChipherAsm sub even faster ?

    Code:
    'Save it as Cipher.Bas
    
    Sub Cipher(InString As String,PassWord As String)
        Register i            As Dword
        Register Len_PassWord As Dword
        Local    Init_y       As Dword 'I edited this line after Semens Post.It was Declared as Register
    
        Local x               As Byte Ptr
        Local y               As Byte Ptr
    
        x            = StrPtr(InString)
        Init_y       = StrPtr(PassWord)
        y            = Init_y
        Len_PassWord = Len(PassWord) + Init_y - 1
    
        For i = 1  To Len(InString)
            @x = @x Xor @y
            Incr x
            If y = Len_PassWord Then
               y = Init_y
            Else
               Incr y
            End If
        Next
    
    End Sub
    
    Sub CipherAsm(InString As String,PassWord As String)
        Local Len_PassWord As Dword
        Local Len_Str      As Dword
        Local Init_y       As Dword
        Local x            As Dword
        
        x            = StrPtr(InString)
        Init_y       = StrPtr(PassWord)-1
                 
        Len_PassWord = Len(PassWord) '+ Init_y
        Len_Str      = Len(InString)
    
    
        !Push ecx
        !Push esi
        !Push edi
        !Push eax
        !Push ebx
        
        
        !Mov  ecx,Len_Str
        !Mov  esi,Init_Y
        !Mov  edi,x
        !Mov  eax,Len_PassWord
        !Add  eax,esi
    l1:
        !Inc  esi
        !Mov  bh,[esi]
        !Mov  bl,[edi]
        !Xor  bl,bh
        !Mov  [edi],bl
        !Inc  edi
        !Cmp  esi,eax
        !Jne  n1
        !Mov  esi,Init_Y
    n1:
    '    !Loop l1 This line was rem out and replaced with the 2 lines below after Berns suggestion.
        !Dec ecx  
        !Jnz l1
          
        !Pop ebx
        !Pop eax
        !Pop edi
        !Pop esi
        !Pop ecx
        
    End Sub
    This is a simple PB/CC code for benchmarks

    Code:
    #COMPILE EXE
    #REGISTER NONE
    #INCLUDE "cipher.bas"
    
    
    FUNCTION PBMAIN()
         REGISTER i AS DWORD
    
          DIM a AS STRING
          DIM pass AS STRING
    
          DIM bt AS DOUBLE
          DIM et AS DOUBLE
    
          pass = "this is a password"
    
          a=STRING$(LEN(pass)*6,0) 'i initialize it to a string of nulls
                                   '   so i can check that if len(InString) > len(Password)
                                   '   the cipher routine works well.
          cipherasm a,pass
          PRINT "Encode=";a
          cipherasm a,pass
          PRINT "Decode=";a
    
          bt=TIMER
          FOR i=1 TO 400000
              cipherasm a,pass
          NEXT
          et=TIMER
    
          PRINT "With CipherAsm 400,000 interactions in (sec):"STR$(et-bt)
          
     
          bt=TIMER
          FOR i=1 TO 400000
              cipher a,pass
          NEXT
          et=TIMER
    
          PRINT "With Cipher 400,000 interactions in (sec):"STR$(et-bt)
    
          
          a=WAITKEY$
    
    
    END FUNCTION
    Thank you

    Stavros

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




    [This message has been edited by Stavros A Petridis (edited May 30, 2002).]
    Byte Hunters of the World Unite.

  • #2
    As I understood it, if you are using ASM within a SUB or FUNCTION, you
    do not need to PUSH any of the registers because PB will do that
    automatically when calling the SUB/FN.

    You should PUSH registers when mixing ASM code with PB code to ensure
    that :

    1) your ASM code doesn't corrupt the values in the registers specified
    in the PB Docs/Help file under saving registers.

    2) any PB statements/functions mixed in with your ASM code don't change
    the contents of the registers that you are using.



    ------------------
    Bernard Ertl
    Bernard Ertl
    InterPlan Systems

    Comment


    • #3
      Bern,
      Thanks for your reply,for some reason i hadn't seen this chapter in the help file.

      Regards
      Stavros


      ------------------
      Byte Hunters of the World Unite.

      Comment


      • #4
        I don't know if its my computer or someone else can verify this but
        when i rem out the Pushes and Pops i get worst benchmarks than if i don't.

        Can someone verify this please ?
        I am using PB/CC 2.11 on Win98SE

        Regards
        Stavros


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


        [This message has been edited by Stavros A Petridis (edited May 29, 2002).]
        Byte Hunters of the World Unite.

        Comment


        • #5

          Push/pop rem out:
          w/ CipherASM secs: 1.8600...
          w/ Cipher secs: 2.0899...

          Push/pop in:
          w/ CipherASM secs: 1.7699...
          w/ Cipher secs: 2.1399...

          PB/CC 2.10 on Win98

          Brad

          ------------------
          mailto:Brad@ByrneWorld.comBrad@ByrneWorld.com</A>

          Comment


          • #6
            Thanks Brad,
            That's strange, when we have the push-pop in, theoreticaly we have more
            machine cycles,but the benchmark is better !?!

            BTW on my computer the speed difference betwen Cipher and CipherAsm is biger:
            CipherAsm 0.65 - 0.70...
            Cipher 3.00 - 3.20 ...

            Stavros

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




            [This message has been edited by Stavros A Petridis (edited May 29, 2002).]
            Byte Hunters of the World Unite.

            Comment


            • #7
              ??? Big difference ?

              ------------------
              mailto:Brad@ByrneWorld.comBrad@ByrneWorld.com</A>

              Comment


              • #8
                Brad,
                Your numbers are
                w/ CipherASM secs: 1.7699...
                w/ Cipher secs: 2.1399...

                and mine are
                CipherAsm sec 0.65 To 0.70...
                Cipher secs 3.00 To 3.20 ...

                In my case CipherAsm looks like its runing ~5 times faster than Chipher
                But in your case it looks like it is runing just a litle faster.

                Stavros


                ------------------
                Byte Hunters of the World Unite.

                Comment


                • #9
                  yeah, I see, I'm using PBCC 2.10 w/ 98... maybe why?

                  ------------------
                  mailto:Brad@ByrneWorld.comBrad@ByrneWorld.com</A>

                  Comment


                  • #10
                    I am using win98 too,i don't know the inside differences of PC/CC 2.10 - 2.11
                    that may have any affect on the assembler, only God (call me Bob) can answer this.

                    Stavros


                    ------------------
                    Byte Hunters of the World Unite.

                    Comment


                    • #11
                      Brad,
                      If i compile this code in PB/CC 2.0 then the benchmarks are:

                      CipherAsm 0,720 sec
                      Cipher 0,989 sec

                      Those numbers are familiar to yours.So i think that there is some kind
                      of difference to pre 2.11 compilers.
                      Lets wait and see what will happen in PB/CC 3


                      ------------------
                      Byte Hunters of the World Unite.

                      Comment


                      • #12
                        Stavros --
                        Your experiments are incorrect
                        You declared three (!) register variables
                        Meanwhile two only are possible (ESI/EDI).
                        2.0 and 2.1 can assign to registers different variables


                        ------------------
                        E-MAIL: matus@perevozki.ru

                        Comment


                        • #13
                          stavros, see this gem by steve hutchesson, at a glance it looks like you're working on the same sort of cipher
                          best of luck,
                          wayne



                          ------------------
                          the powerbasic crypto archives
                          -

                          Comment


                          • #14
                            Semen,
                            About the register vars you are right,i remembered 4 regiter vars but that is for ext precision vars not integer.
                            But this does not make any difference because the third declaration will be just ignored by the compiler.
                            The fact is that CipherAsm runs ~5 times faster than Chipher when compiled with PB/CC 2.11 and
                            just a little faster when compiled with 2.0 (because Chipher runs faster)
                            If you see at the begining of the test program i have #REGISTER NONE so this should turn off any
                            automatic register var assignment done by the compiler.

                            Regards

                            Stavros


                            ------------------
                            Byte Hunters of the World Unite.

                            Comment


                            • #15
                              Wayne,
                              You are right,this kind of simple encryption is a very common technique.
                              BTW it looks like i implimented it a little diffrently in CipherAsm and i will
                              benchmark it to see which is faster.
                              Regards

                              Stavros


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


                              [This message has been edited by Stavros A Petridis (edited May 29, 2002).]
                              Byte Hunters of the World Unite.

                              Comment


                              • #16
                                Wayne,
                                I can proudly say that mine (CipherAsm) runs more than 2 times faster.
                                Compiled in PB/CC 2.11
                                ------------------




                                [This message has been edited by Stavros A Petridis (edited May 29, 2002).]
                                Byte Hunters of the World Unite.

                                Comment


                                • #17
                                  Interesting! I'll have to do some tests of my own, I hope Steve isn't too disappointed


                                  [This message has been edited by Wayne Diamond (edited May 29, 2002).]
                                  -

                                  Comment


                                  • #18
                                    Just add the following code in my test program:
                                    Code:
                                         bt=TIMER
                                          FOR i=1 TO 400000
                                              XorString a,pass
                                          NEXT
                                          et=TIMER
                                    
                                          PRINT "With XorString 400,000 interactions in (sec):"STR$(et-bt)
                                    And add Steve's routine in the Chipher.bas file and run it.
                                    See for your self !

                                    I smell $10 coming my way (Wayne you didn't edit your message fast enough )


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




                                    [This message has been edited by Stavros A Petridis (edited May 29, 2002).]
                                    Byte Hunters of the World Unite.

                                    Comment


                                    • #19
                                      But there are 2 questions remaining

                                      1)Does Pb/CC 2.11 produce slower executable code than 2.0 /2.10 in some cases ?

                                      2)Why if we rem out the Push/Pops in my CipherAsm routine ,it executes slower ?

                                      God (i told you call me Bob) / Lance /Tom /Somebody can you give me an answer ?

                                      Thanks

                                      Stavros


                                      ------------------
                                      Byte Hunters of the World Unite.

                                      Comment


                                      • #20
                                        1)Does Pb/CC 2.11 produce slower executable code than 2.0 /2.10 in some cases ?
                                        It's possible, sure. But your benchmarks aren't being done on the
                                        same machine, so they have no meaning. Different CPUs optimize
                                        assembly language differently. It's perfectly typical to see things
                                        like, routine A is faster than routine B on a Pentium II, but
                                        slower than routine B on a Pentium III, and exactly as fast on an AMD K6.

                                        2)Why if we rem out the Push/Pops in my CipherAsm routine ,it executes slower ?
                                        Some quirk of the CPU optimization of your particular processor,
                                        I suspect.
                                        Normally, you'd expect it to go the other way, but modern CPUs have
                                        complex and bizarre optimization strategies.

                                        A search of the Forums will turn up some lengthy discussions of CPUs
                                        and benchmarking issues.

                                        ------------------
                                        Tom Hanlin
                                        PowerBASIC Staff

                                        Comment

                                        Working...
                                        X