Hello!
I want to create function that returns the position of an element found in an array. If the element isn't in that array, it will return -1.
In C language, the function looks like that:
int find(int *a, size_t n, int x)
{ int i;
for(i=0; i<n; i++)
{
if(x == a[i])
return i;
}
return -1;
}
Well, I want to write that function in ASM, and then to insert it into a C program, using the asm{ ... } label (or whatever it is).
I found in a book, the implementation in ASM of that function and I tried to write it in a C function, as you may see in the following code:
#include<stdio.h>
int cauta(int near *a, size_t n, int x)
{
asm{
push si
push cx
push dx
mov si, a
mov cx, n
jcxz not_gasit
mov dx, x
sub si, 2
}
reluare:
asm{
add si, 2
cmp [si], dx
loopnz reluare
jnz not_gasit
mov ax, n
sub ax, cx
dec ax
jmp final
}
not_gasit:
asm mov ax, -1
final: asm{
pop dx
pop cx
pop si
}
return _AX;
}
int main()
{
int a[100];
a[0] = 4;
a[1] = 5;
a[2] = 1;
a[3] = 9;
printf("%d", cauta(a, 4, 1);
}
The problem is that I get some errors:
D:\C++ Projects\Preparing for ONI\main.c|4|error: expected ';', ',' or ')' before '*' token|
D:\C++ Projects\Preparing for ONI\main.c||In function 'main':|
D:\C++ Projects\Preparing for ONI\main.c|49|warning: implicit declaration of function 'cauta'|
D:\C++ Projects\Preparing for ONI\main.c|49|error: expected ')' before ';' token|
D:\C++ Projects\Preparing for ONI\main.c|50|error: expected ';' before '}' token|
||=== Build finished: 3 errors, 1 warnings ===|
I compile that using the gcc compiler which comes with the Code::Blocks IDE. Also, I mention that I compile it from a x64 architecture machine.
I think that might be a problem with the near keyword.
How can I fix that?
Thank you respectfully.