PowerBASIC Forums
  Programming
  InLine Assembler (Page 1)

Post New Topic  Post A Reply
profile | register | preferences | faq | search

UBBFriend: Email This Page to Someone!
This topic is 3 pages long:   1  2  3 
next newest topic | next oldest topic
Author Topic:   InLine Assembler
Stavros A Petridis
Member
posted May 29, 2002 06:21 AM     Click Here to See the Profile for Stavros A Petridis     Edit/Delete Message   Reply w/Quote
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 ?


'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


#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).]

IP: Logged

Bern Ertl
Member
posted May 29, 2002 06:45 AM     Click Here to See the Profile for Bern Ertl     Edit/Delete Message   Reply w/Quote
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

IP: Logged

Stavros A Petridis
Member
posted May 29, 2002 06:54 AM     Click Here to See the Profile for Stavros A Petridis     Edit/Delete Message   Reply w/Quote
Bern,
Thanks for your reply,for some reason i hadn't seen this chapter in the help file.

Regards
Stavros

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

IP: Logged

Stavros A Petridis
Member
posted May 29, 2002 06:59 AM     Click Here to See the Profile for Stavros A Petridis     Edit/Delete Message   Reply w/Quote
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).]

IP: Logged

Brad D Byrne
Member
posted May 29, 2002 07:25 AM     Click Here to See the Profile for Brad D Byrne     Edit/Delete Message   Reply w/Quote

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

------------------
Brad@ByrneWorld.com

IP: Logged

Stavros A Petridis
Member
posted May 29, 2002 07:47 AM     Click Here to See the Profile for Stavros A Petridis     Edit/Delete Message   Reply w/Quote
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).]

IP: Logged

Brad D Byrne
Member
posted May 29, 2002 08:10 AM     Click Here to See the Profile for Brad D Byrne     Edit/Delete Message   Reply w/Quote
??? Big difference ?

------------------
Brad@ByrneWorld.com

IP: Logged

Stavros A Petridis
Member
posted May 29, 2002 08:18 AM     Click Here to See the Profile for Stavros A Petridis     Edit/Delete Message   Reply w/Quote
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

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

IP: Logged

Brad D Byrne
Member
posted May 29, 2002 08:27 AM     Click Here to See the Profile for Brad D Byrne     Edit/Delete Message   Reply w/Quote
yeah, I see, I'm using PBCC 2.10 w/ 98... maybe why?

------------------
Brad@ByrneWorld.com

IP: Logged

Stavros A Petridis
Member
posted May 29, 2002 08:31 AM     Click Here to See the Profile for Stavros A Petridis     Edit/Delete Message   Reply w/Quote
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

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

IP: Logged

Stavros A Petridis
Member
posted May 29, 2002 08:40 AM     Click Here to See the Profile for Stavros A Petridis     Edit/Delete Message   Reply w/Quote
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

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

IP: Logged

Semen Matusovski
Member
posted May 29, 2002 09:00 AM     Click Here to See the Profile for Semen Matusovski     Edit/Delete Message   Reply w/Quote
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

IP: Logged

Wayne Diamond
Member
posted May 29, 2002 09:08 AM     Click Here to See the Profile for Wayne Diamond     Edit/Delete Message   Reply w/Quote
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

IP: Logged

Stavros A Petridis
Member
posted May 29, 2002 09:09 AM     Click Here to See the Profile for Stavros A Petridis     Edit/Delete Message   Reply w/Quote
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

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

IP: Logged

Stavros A Petridis
Member
posted May 29, 2002 09:14 AM     Click Here to See the Profile for Stavros A Petridis     Edit/Delete Message   Reply w/Quote
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).]

IP: Logged


This topic is 3 pages long:   1  2  3 

All times are EasternTime (US)

next newest topic | next oldest topic

Administrative Options: Close Topic | Archive/Move | Delete Topic
Post New Topic  Post A Reply
Hop to:

Contact Us | PowerBASIC BASIC Compilers

Copyright © 1999-2005 PowerBASIC, Inc. All Rights Reserved.


Ultimate Bulletin Board 5.45c