Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Assambler problem

Posted on 1999-07-17
18
Medium Priority
?
172 Views
Last Modified: 2010-04-06
I need a assistance for translate a piece of code from Pascal (DOS) to Delphi 3.0 The function is next:

Function Next_CPos
        (Srch_ch:  Char;  Src_str:  String;  Strt_at:  Byte):  Byte;  Assembler;

{This function searches for the next occurence of Srch_ch in Src_str AFTER position Strt_at.  The function returns the offset from the beginning of the string, NOT the offset from Strt_at.
}

Asm  { Function Next_CPos }
                                                     XOR                AX,AX         {        0, AX }                                                        MOV                AL,Strt_at    {        Move position to start at to AL }
LES                DI,Src_str    {        Set ES:DI to start of Src_str }
XOR                CX,CX         {        0 CX }
MOV                CL,[ES:DI]    {        Store length of Src_str in CL }
INC                DI            {        Set ES:DI to first char of Src_str }
MOV                BX,CX         {        Move CX to BX }
SUB                CX,AX         {        Set CX to length of string after Strt_at }
ADD                DI,AX         {        Set ES:DI to char at Strt_at in Src_str }
MOV                AL,Srch_ch    {        Move Srch_ch to AL }
CLD                 {        Clear direction flag }
REPNZ        SCASB         {        Look for character following Strt_at }
JNZ                @NOTFND       {        If not found, jump to end of procedure }
SUB                BX,CX         {        Set BX to position char found in }
JMP                @DONE         {        Jump to end of procedure }
@NOTFND:        XOR                BX,BX         {        Srch_ch not found, set BX to 0 }
@DONE:                MOV                AX,BX         {        Move position found at (BX) to AX }
End;  { Function Next_CPos }
0
Comment
Question by:danuti
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
  • 2
  • +1
18 Comments
 
LVL 13

Accepted Solution

by:
Epsylon earned 600 total points
ID: 1389250
This should be it:

Function Next_CPos (Srch_ch:  Char;  Src_str:  String;  Strt_at:  Byte):  Byte;
var i: Integer;
begin
  i := Strt_at;
  while (i <= Length(Src_str)) and (Src_str[i] <> Srch_ch) do
    Inc(i);
  if i > Length(Src_str) then
    Result := 0
  else
    Result := i;
end;

0
 
LVL 13

Expert Comment

by:Epsylon
ID: 1389251
Well, if it should find the first 'Srch_ch' AFTER 'Strt_at' then change the line

  i := Strt_at;

to

  i := Strt_at + 1;


Cheers,

Epsylon.
0
 
LVL 15

Expert Comment

by:simonet
ID: 1389252
Listening...
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 13

Expert Comment

by:Epsylon
ID: 1389253
You have many ears, Alex  :o)
0
 
LVL 15

Expert Comment

by:simonet
ID: 1389254
Hehehe! I'll stop using "listening" on questions I follow. From now on I'll use "monitoring" or "following". You guys never stop joking about the "listening" thing. ;|

Alex
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1389255
here...

Function Next_CPos (Srch_ch:  Char;  Src_str:  String;  Strt_at: Integer) :  Integer;
begin
  result := Pos(Srch_ch, Copy(Src_str, Strt_at, Length(Src_str)));
end;

and you can use it like so...

Caption := IntToStr(Next_CPos('a', Edit1.Text, 1));

..-=ViKtOr=-..
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 1389256
A very short one, Viktor, but slow and memory consuming when you want to search very large strings. The Copy function should not be used then.

Eps.
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1389257
hehe not true,,,, first my method uses assembler... great... second yours uses a loop which needs a comparison on each iritation of the loop... bad... you've another comparison in there which slows it down a LOT especially if called many times in another loop... it's not memory consuming at all... Delphi's been created in a way that the memory manager is smart and knows what to do when this occurs...
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1389258
btw, Epsylon, if you don't like that one you could try the following which is EXTREMELY fast...

Function Next_CPos1 (Srch_ch:  Char;  Src_str:  PChar;  Strt_at: Integer) :  Integer;
begin
  Result := Pos(Srch_ch, PChar(Src_Str+Strt_at-1));
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
     Caption := IntToStr(Next_CPos('a', PChar(Edit1.Text), 1));
end;

..-=ViKtOr=-..
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 1389259
> hehe not true

True! The Copy function copies the whole string from index 'Strt' until the end.


> second yours uses a loop which needs a comparison on each iritation of the loop

What do you think the Pos function does?


Eps.
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1389260
or even this...if you want to use a String and simply typecast it within the function for less confusion...

Function Next_CPos1 (Srch_ch:  Char;  Src_str:  String;  Strt_at: Integer) :  Integer;
      begin
        Result := Pos(Srch_ch, PChar(PChar(Src_Str)+Strt_at-1));
      end;

      procedure TForm1.Button1Click(Sender: TObject);
      begin
           Caption := IntToStr(Next_CPos('a', Edit1.Text, 1));
      end;

..-=ViKtOr=-..
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 1389261
And the second comparison you talk about must be 'hidde' in the Pos function too.
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1389262
>>What do you think the Pos function does?
well yeah... didn't think of that... anyway... take a look at the new function... cant even compare it with yours in speed ;)
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1389263
plus ur code can be optimized...
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 1389264
Haha, that function does not work correctly   :o)
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1389265
well it does on my computer....
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1389266
opps... i feel so embarassed ;o))

hehe... I completely forgot that the Strt_at could be a a different value than 1 and didnt even test that ;-))

this one works alright....

Function Next_CPos(Srch_ch:  Char;  Src_str:  String;  Strt_at: Integer) :  Integer;
begin
  Result := Pos(Srch_ch, PChar(PChar(Src_Str)+Strt_at-1));
  if Result <> 0 then Inc(Result, Strt_at-1);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Caption := IntToStr(Next_CPos('a', Edit1.Text, 3));
end;
0
 

Author Comment

by:danuti
ID: 1389267
Thanks Epsylon's for your answer. It is fastest and I use it in my program.
If I can help you, please ask me.
Thanks again.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question