User forums > Using Code::Blocks
ASM function in C
(1/1)
xor_NTG:
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:
--- Code: ---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;
}
--- End code ---
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:
--- 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);
}
--- End code ---
The problem is that I get some errors:
--- Code: ---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 ===|
--- End code ---
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.
BlueHazzard:
hello,
do you need the near key word for your platform? I don't think that this is standard conform...
I don't think you need it on newer platforms like x64....
btw. this is a compiler question and not related to c::b so this breaks the rules and i think it will be locked by admins:
http://forums.codeblocks.org/index.php/topic,9996.0.html
xor_NTG:
Oh, I didn't know that. I'm sorry. Thank you anyway.
Jenna:
--- Quote from: BlueHazzard on March 12, 2013, 06:43:37 pm ---[...]
this is a compiler question and not related to c::b so this breaks the rules and i think it will be locked by admins:
http://forums.codeblocks.org/index.php/topic,9996.0.html
--- End quote ---
Topic locked !
Navigation
[0] Message Index
Go to full version