Inline Assemble Code - A problem relevant to register usage


1. Source code as followings

#include <stdio.h>

int main()
{
    int arg1, arg2, add, sub, mul, quo, rem ;
    printf( "Enter two integer numbers : " );
    scanf( "%d%d", &arg1, &arg2 );

    __asm__ ( "movl $0, %%edx;"
    "movl %2, %%eax;"
    "movl %3, %%ebx;"
    "idivl %%ebx;" : "=a" (quo), "=d" (rem) : "g" (arg1), "g" (arg2) );

    printf( "%d / %d = %d\n", arg1, arg2, quo );
    printf( "%d %% %d = %d\n", arg1, arg2, rem );
    return 0 ;
}


2. compile

gcc -o tt1.x tt1.c


3. run

./tt1.x

Floating point exception (core dumped)


4. Root Cause

objdump -D tt1.x >111

vi 111, you will see below

  4005c9:       e8 d2 fe ff ff          callq  4004a0 <__isoc99_scanf@plt>
  4005ce:       8b 45 f0                mov    -0x10(%rbp),%eax
  4005d1:       8b 55 f4                mov    -0xc(%rbp),%edx
  4005d4:       ba 00 00 00 00          mov    $0x0,%edx
  4005d9:       89 c0                   mov    %eax,%eax
  4005db:       89 d3                   mov    %edx,%ebx
  4005dd:       f7 fb                   idiv   %ebx
  4005df:       89 45 f8                mov    %eax,-0x8(%rbp)
  4005e2:       89 55 fc                mov    %edx,-0x4(%rbp)
  4005e5:       8b 55 f4                mov    -0xc(%rbp),%edx
  4005e8:       8b 45 f0                mov    -0x10(%rbp),%eax
  4005eb:       8b 4d f8                mov    -0x8(%rbp),%ecx


5. Statement

The root cause is  that arg2 use register edx, but edx

is cleared zero before idiv


6. improvement

Change arg2 to memory location rather than register.


#include <stdio.h>

int main()
{
    int arg1, arg2, add, sub, mul, quo, rem ;
    printf( "Enter two integer numbers : " );
    scanf( "%d%d", &arg1, &arg2 );

    __asm__ ( "movl $0, %%edx;"
    "movl %2, %%eax;"
    "movl %3, %%ebx;"
    "idivl %%ebx;" : "=a" (quo), "=d" (rem) : "m" (arg1), "m" (arg2) );

    printf( "%d / %d = %d\n", arg1, arg2, quo );
    printf( "%d %% %d = %d\n", arg1, arg2, rem );
    return 0 ;
}




你可能感兴趣的:(assembly)