Author Topic: ASM function in C  (Read 6474 times)

Offline xor_NTG

  • Single posting newcomer
  • *
  • Posts: 7
ASM function in C
« on: March 12, 2013, 06:22:18 pm »
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;
}         

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);
}

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 ===|

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.

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3352
Re: ASM function in C
« Reply #1 on: March 12, 2013, 06:43:37 pm »
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

Offline xor_NTG

  • Single posting newcomer
  • *
  • Posts: 7
Re: ASM function in C
« Reply #2 on: March 12, 2013, 06:56:06 pm »
Oh, I didn't know that. I'm sorry. Thank you anyway.

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7252
Re: ASM function in C
« Reply #3 on: March 12, 2013, 07:13:49 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

Topic locked !