[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

problem with linear search method

Posted on 2004-04-11
8
Medium Priority
?
765 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
On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

 
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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

Desired Skill Set for Microsoft Dynamics CRM Technical Resources – Part III
Geo-targeting is the practice of distributing content based on a person’s location, as best as you can determine it. Let’s look at some ways you could successfully use this tactic. The following tips and case studies could lead to meaningful results.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

649 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