Solved

problem with linear search method

Posted on 2004-04-11
8
756 Views
Last Modified: 2008-03-17

hello, I'm trying to convert C to Assembly with the following method and I keep getting a 1 as return value, can anyone tell what's wrong ? Thanks ! :

int lsearch(const int x[], int cursize, int target)
{
int i;
for(i = 0; i < cursize; i++)
{
if(x[i]==target)
return i;
}
return -1;
}

Assembly codes:
_asm
{
         mov ebx,x
         mov ecx,cursize
         jecxz done
top:
         mov edx,[ebx]
         cmp edx,target
         jne notFound
         mov eax,ecx
         add ebx,4
         jmp next
notFound:
         mov eax,-1
         add ebx,4
next:
        loop top
done:
}

0
Comment
Question by:azcalv408
8 Comments
 
LVL 8

Expert Comment

by:manish_regmi
ID: 10803191
hi,
 where are you putting the output. I dont see in the asm code.
Also you have taken int as 4 bytes. Is it 32 bit code.
Int in 2 bytes in 16 bit code.

regards manish

0
 

Author Comment

by:azcalv408
ID: 10803271
int is 4 bytes (32 bit code)
the output is in main : it's just outputting index of found element or -1 if not found. It is also in C just for testing. I'm doing inline assembly by the way.
0
 
LVL 22

Accepted Solution

by:
grg99 earned 40 total points
ID: 10804522

If you get a match you jump to "next", which keeps on searching.  
You probably want to jump to "done".

Also take care, "mov  ebx,x" will move the contents of x, not it's address.
That's w2hat you want to do if "x" is a parameter, but NOt if X is the array itself.
Use "mov ebx,offset x"  or "lea  ebx,x" to load the address.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 3

Expert Comment

by:Dancie
ID: 10805893
something like this may be faster and get you what you want

asm
{
         push   ebx                 ;always save
         mov ecx,cursize
         mov ebx,x                 ;it is never a good idea to use ebx in a functon
                                         ;this register is used by the compiler and if it is changed
                                         ;and you compile a release mode your porgram will
                                         ;CRASH. You should update your x[] pointer in another
                                         ;function
         or   ecx,ecx               ;much faster than jecxz and give
         jz notFound               ;zero search length should also be an error
         mov  edx,[target]
         or   eax,0                  ;make eax = 0
top:
         cmp   edx,[ebx+eax*4]         ;this is an int so 4 byte steps
         je     done
         inc    eax
         cmp  eax,ecx
         jne  top
notFound:
         mov eax,-1
done:
         pop  ebx                             ;restore ebx
}
0
 
LVL 5

Expert Comment

by:twobitadder
ID: 10809118
>>  or   eax,0                  ;make eax = 0

I think you mean:  and eax,0 :P
0
 
LVL 3

Assisted Solution

by:Dancie
Dancie earned 40 total points
ID: 10822829
Ok I messed up on the last one. It is and eax,0 this makes eax == 0 and aligns the
code on a 4 byte boundary.

asm
{
         push   ebx                 ;always save
         mov ecx,cursize
         mov ebx,x                 ;it is never a good idea to use ebx in a functon
                                         ;this register is used by the compiler and if it is changed
                                         ;and you compile a release mode your porgram will
                                         ;CRASH. You should update your x[] pointer in another
                                         ;function
         or   ecx,ecx               ;much faster than jecxz and give
         jz notFound               ;zero search length should also be an error
         mov  edx,[target]
         and   eax,0                  ;make eax = 0
top:
         cmp   edx,[ebx+eax*4]         ;this is an int so 4 byte steps
         je     done
         inc    eax
         cmp  eax,ecx
         jne  top
notFound:
         mov eax,-1
done:
         pop  ebx                             ;restore ebx
}

0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Are you unable to connect or configure Hotmail email account in Microsoft Outlook 2010, 2007? Or Outlook.com emails are not downloading to Outlook? Lets’ see the problem and resolve Outlook Connector error syncing folder hierarchy (0x8004102A).
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now