?
Solved

problem with linear search method

Posted on 2004-04-11
8
Medium Priority
?
764 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
[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 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 160 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 160 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

What's worse than having your data encrypted by ransomware? Getting attacked by a so-called "wiper," which simply destroys the data and offers you no hope of ever seeing it again.
Active Directory can easily get cluttered with unused service, user and computer accounts. In this article, I will show you the way I like to implement ADCleanup..
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

801 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