Calling assembly from c

This is a discussion on Calling assembly from c within the C Programming forums, part of the General Programming Boards category; Hello, I have a problem with calling assembly from c. typedef unsigned char digit; // apn is a pointer to ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    3

    Calling assembly from c

    Hello, I have a problem with calling assembly from c.

    typedef unsigned char digit; // apn is a pointer to array of digits.

    Functions:

    add(apn number1,apn number2,apn result)

    subtract(apn number1,apn number2,apn result)

    multiply(apn number1,apn number2,apn result)

    I must call these functions from C and then these funct. must use assembly to add,sub,mul operations. That means i need assembly code for this functions. Numbers are in a char array. Example;

    number1="123"//number1 and number2 are digit array.

    number2="999"

    then when i call add(number1,number2,result) from c

    result will be "1122"

    May you help me to write assembly code for these functions?(Please remember that numbers are char array) If you give me some links to learn from or explain something about this topic, you will be helped me to write

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Kuluzuva View Post
    May you help me to write assembly code for these functions?
    You should post this in an ASM forum.

    Also, if you've never written ASM before it's time to crack the books and start learning... but be warned, it's a truly nebulous subject.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    3
    Actually, I've written Asm before. I'm searching information about this topic from morning but I got nothing... I cant find sites, information, anything. If you write some links to look for me, I'll be happier...

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So then what exactly do you have a question about?

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Kuluzuva View Post
    Actually, I've written Asm before. I'm searching information about this topic from morning but I got nothing... I cant find sites, information, anything. If you write some links to look for me, I'll be happier...
    assembly language forum - Google Search

    About 7,150,000 results (0.13 seconds)
    Are you sure you looked?
    Last edited by CommonTater; 05-14-2011 at 06:57 PM.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,566
    Also, if you're working on an x86: Intel x86 Function-call Conventions - Assembly View.

  7. #7
    Registered User Alexander.'s Avatar
    Join Date
    May 2011
    Location
    Idaho
    Posts
    9
    If you are having trouble implementing an assembly in to C, you may wish to state your platform first as the syntax differs. For example you can process the char array and extract numerics out of them (i.e. '3' - '0' = 3) and pass them as a parameter in your assembly, i.e. in GNU syntax
    Code:
           asm ( 
                 assembly template
               : output operands                  /* optional */
               : input operands                   /* optional */
               : list of clobbered registers      /* optional */
               );
    See: GCC-Inline-Assembly-HOWTO
    (or MSVC: Inline Assembler)

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,672
    > Hello, I have a problem with calling assembly from c.
    Without telling us the names of all your compilers and assemblers, you're never going to get a lot of help on this.

    Having tools provided by the same vendor is pretty much a "must" if you want relatively easy integration.

    Otherwise, it takes some fiddling to get things like name decoration and calling conventions ironed out.

    If your linker is telling you "unresolved symbols", then there is probably something that can be done.
    If your linker is telling you "bad object file format", then you're pretty much screwed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    3
    My compiler is gcc and assembler is nasm. I made my makefile. I need to learn taking arguments from function to assembly code. For example, these functions take 3 arguments and how i will use them in assembly? I wrote something that is:
    PHP Code:

     
    .MODEL SMALL

     
    .CODE

    global _add_num

    _add_num PROC

           push    ebp 
    ;Pushing arguments from function
           
    mov    ebp,esp
           pushad
           mov eax
    ,0
           mov edx
    ,0
           mov esi
    ,[ebp+8]
           
    mov esp,[ebp+4]
           

           
    mov edi,esi
           sub ecx
    ,ecx
           not ecx
           sub al
    ,al
           cld
           repne scasb
           neg ecx
           sub ecx
    ,3
           mov ebx
    ,ecx ;ebx has lenght of first argument

           mov edi
    ,esp
           sub ecx
    ,ecx
           not ecx
           sub al
    ,al
           cld
           repne scasb
           neg ecx
           sub ecx
    ,3
           mov edi
    ,[ebp+4]
           
    mov ebp,ecx ;ebp has lenght of first argument

           sub eax
    ,eax
           sub edx
    ,edx
           mov ecx
    ,2

           L1
    :
           
    mov al,[esi+ebp]
           
    mov dl,[esp+ebx
           
    sub dl,48 ;sub ascii code
           add al
    ,dl 
           mov 
    [edi+ebx],al       ;result is in eax
           dec ebp
           dec ebx
           loop L1

           pop ebp
     ret

    _add_num ENDP

     END 
    This code is correct for add function:
    add(char* number1,char* number2,char* result)

    And I'm getting memory for result before calling function.
    There is any wrong about this code?

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,672
    > There is any wrong about this code?
    I can see quite a few things...

    1. .MODEL SMALL
    What does this even mean in a 32-bit program?

    2. pushad
    When does this get reversed (ie, popad at the end)?

    3. mov esp,[ebp+4]
    Trashing the stack pointer ?

    4. mov ebp,ecx ;ebp has lenght of first argument
    Trashing the base pointer ?

    A typical function should have this prologue / epilogue

    Code:
           push   ebp ; save frame pointer
           mov    ebp,esp ; create a new frame
    
    // Your code here
    
           mov    esp,ebp ; reset stack pointer back as it was (see comment below)
           pop     ebp
           ret
    You can also use leave to replace the two instructions before the ret

    Why not try something simple first, like the asm versions of
    Code:
    char firstChar ( char *line ) {
      return line[0];
    }
    
    int stringLength ( char *line ) {
      int i = 0;
      while ( line[i] != '\0' ) i++;
      return i;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling Assembly Language programs from C++ program
    By EonsNearby in forum C++ Programming
    Replies: 6
    Last Post: 04-23-2011, 04:58 AM
  2. Calling an Assembly Function
    By piommi in forum C Programming
    Replies: 4
    Last Post: 12-07-2007, 02:29 AM
  3. calling functions & assembly code
    By Micko in forum C++ Programming
    Replies: 1
    Last Post: 02-25-2004, 02:27 PM
  4. Calling C++ from assembly?
    By Chris361 in forum C++ Programming
    Replies: 2
    Last Post: 10-22-2003, 06:17 AM
  5. Calling functions written in assembly?
    By The V. in forum C Programming
    Replies: 5
    Last Post: 10-24-2001, 08:11 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21