Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 766
  • Last Modified:

problem with linear search method


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
azcalv408
Asked:
azcalv408
2 Solutions
 
manish_regmiCommented:
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
 
azcalv408Author Commented:
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
 
grg99Commented:

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
[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

 
DancieCommented:
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
 
twobitadderCommented:
>>  or   eax,0                  ;make eax = 0

I think you mean:  and eax,0 :P
0
 
DancieCommented:
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now