s5vi's Motorola p2k phone page
February 05, 2012, 05:06:05 pm *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Over 30000 member ! Under heavy spammer attack ! Cleaning in progress !
V-6.0.0 with CDMA support is available now.
To become VIP: you can do it via PayPal: http://www.el-co.hu/p2k05/paypal.html
 
   Home   Help Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Using in-line assembly in C  (Read 9510 times)
0 Members and 1 Guest are viewing this topic.
s5vi
Administrator
Hero Member
*****
Offline Offline

Posts: 769



WWW Email
« on: November 10, 2006, 08:25:03 pm »

The core p2k functions is in p2kapi.asm and their constants and variables defined in p2kapi.inc

The method is just like this:

Code:
int myasmbeep();
int main( void )
{
freq=1000;
dur=1000;
myasmbeep();
}
int myasmbeep()
{
 __asm
  {
   push    freq
   push    dur
   call dword ptr [Beep]
  }
  // Return with result in EAX
}

The trick is to define variables in C,and use them in inline assembly
Parameter passing is standard C like via stack

Regards....
Code:
« Last Edit: November 14, 2006, 04:15:06 pm by s5vi » Logged

Rosalee38
Newbie
*
Offline Offline

Posts: 3


Email
« Reply #1 on: November 25, 2011, 12:09:53 pm »

Well, you don't say specifically, but by your post, it appears like you're using gcc and its inline asm with constraints syntax (other C compilers have very different inline syntax). That said, you probably need to use AT&T assembler syntax rather than Intel, as that's what gets used with gcc.
So with the above said, lets look at your write2 function. First, you don't want to create a stack frame, as gcc will create one, so if you create one in the asm code, you'll end up with two frames, and things will probably get very confused. Second, since gcc is laying out the stack frame, you can't access vars with "[ebp + offset]" ad you don't know how its being laid out. That's what the constraints are for -- you say what kind of place you want gcc to put the value (any register, memory, specific register) and the use "%X" in the asm code. Finally, if you use explicit registers in the asm code, you need to list them in the 3rd section (after the input constraints) so gcc knows you are using them. Otherwise it might put some important value in one of those registers, and you'd clobber that value.
So with all that, your write2 function looks like:
void write2(char *str, int len) {
    __asm__ volatile (
        "movl $4, %%eax;"
        "movl $1, %%ebx;"
        "movl %0, %%ecx;"
        "movl %1, %%edx;"
        "int $0x80"
        :: "g" (str), "g" (len)
        : "eax", "ebx", "ecx", "edx");
}

Note the AT&T syntax -- src, dest rather than dest, src and % before the register name.
Now this will work, but its inefficient as it will contain lots of extra movs. In general, you should NEVER use mov instructions or explicit registers in asm code, as you're much better off using constraints to say where you want things and let the compiler ensure that they're there. That way, the optimizer can probably get rid of most of the movs, particularly if it inlines the function (which it will do if you specify -O3). Conveniently, the i386 machine model has constraints for specific registers, so you can instead do:
void write2(char *str, int len) {
    __asm__ volatile (
        "movl $4, %%eax;"
        "movl $1, %%ebx;"
        "int $0x80"
        :: "c" (str), /* c constraint tells the compiler to put str in ecx */
           "d" (len)  /* d constraint tells the compiler to put len in edx */
        : "eax", "ebx");
}

or even better
void write2(char *str, int len) {
    __asm__ volatile ("int $0x80"
        :: "a" (4), "b" (1), "c" (str), "d" (len));
}

Note also the use of volatile which is needed to tell the compiler that this can't be eliminated as dead even though its outputs (of which there are none) are not used.
Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.13 | SMF © 2006-2011, Simple Machines LLC Valid XHTML 1.0! Valid CSS!

Bad Behavior has blocked 48 access attempts in the last 7 days.



Google visited last this page January 09, 2012, 11:31:58 am