Yet another function arguments question

This is a discussion on Yet another function arguments question within the C Programming forums, part of the General Programming Boards category; I have this one... Code: typedef long Function(); void test(int n1, int n2) { printf("%d and %d", n1, n2); } ...

  1. #1
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489

    Yet another function arguments question

    I have this one...

    Code:
    typedef long Function();
    
    void test(int n1, int n2) {
      printf("%d and %d", n1, n2);
    }
    
    int main()
    {
      Function *p = (Function *)test;
    
      int a = 1;
      int b = 2;
    
      asm("push %0" : "r"(a));
      asm("push %0" : "r"(b));
      (*p)();
    }
    Can anybody tell me why the output is "2 and 2"??

    Is there something wrong?

    I tried...

    Code:
      asm("push %0" : "a"(a));
      asm("push %0" : "a"(b));
    But it gave me the same result... T_T"

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You could POSSIBLY do this by writing the whole call as inline assembler (using p as an argument to the inline assembler) , but otherwise, you have no idea exactly what the compiler does here, and whether it will add extra stuff on the stack (e.g. storing and restoring some register before calling the function).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Thanks matsp, but how to call function pointer via inline asm,...

    Code:
    asm("call %0" : "r"(p));
    seems doesn't work.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Try "call (%0)", "r"(p)

    Don't forget to clean up the stack afterwards!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 02:17 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Easy question about arguments in a function
    By pond-person in forum C++ Programming
    Replies: 5
    Last Post: 12-12-2002, 10:36 PM
  5. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 07:53 AM

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