Solved

problem with linear search method

Posted on 2004-04-11
8
762 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 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
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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Assemlby_ARM 3 533
c code optimization 6 493
z390 Assembly 5 531
How to Building Five Part Assembly Name for Master Page Inherit 5 565
Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
Invest in your employees with these five simple steps to improve employee engagement and retention.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

739 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