This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

GCC inline assembler output problems


I'm trying to multiply in assembler by shifting and adding and I get the
weirdest output from the following code. If the last two function calls in
the int main are commented out I get a normal result but otherwise I get a
normal result for the first call and two garbage results for the second and
third?


    #include <iostream>
    
    using namespace std;
    
    int times_ten(int multiply_by_ten)
    {
    	int multiplied_by_ten = 0;
    	//this multiplies by 10
    	__asm__("shld   %%eax,%1;"
    				  "movl %%eax,%%ebx;"
    				  "shld  %%eax,%1;"
    				  "shld  %%eax,%1;"
    				  "addl  %%ebx,%%eax;"
    				  : "=r" (multiplied_by_ten)
    				  : "a" (multiply_by_ten)
    	);
    
    	return multiplied_by_ten;
    }
    int times_hundred(int multiply_by_hundred)
    {
    	int multiplied_by_hundred = 0;
    	//this multiplies by 100
    	__asm__("shld   %%eax,%1;"
    				  "shld   %%eax,%1;"
    				  "movl %%eax,%%ebx;"
    				  "shld  %%eax,%1;"
    				  "shld  %%eax,%1;"
    				  "shld  %%eax,%1;"
    				  "movl %%eax,%%edx;"
    				  "shld  %%eax,%1;"
    				  "addl  %%ebx,%%eax;"
    				  "addl  %%edx,%%eax;"
    				  : "=r" (multiplied_by_hundred)
    				  : "a" (multiply_by_hundred)
    
    	);
    
    	return multiplied_by_hundred;
    }
    int main()
    {
    	cout<<times_hundred(1)<<endl;
    	cout<<times_ten(1)<<endl;
    	cout<<times_hundred(1)<<endl;
    
        return 0;
    }

The following has the clobber list but it wont compile. the errors are below
it.

    #include <iostream>
    
    using namespace std;
    
    int times_ten(int multiply_by_ten)
    {
    	int multiplied_by_ten = 0;
    	//this multiplies by 10
    	__asm__("shld   %%eax,%1;"
    				  "movl %%eax,%%ebx;"
    				  "shld  %%eax,%1;"
    				  "shld  %%eax,%1;"
    				  "addl  %%ebx,%%eax;"
    				  : "=r" (multiplied_by_ten)
    				  : "a" (multiply_by_ten)
    				  : "%%eax", "%%ebx"
    	);
    
    	return multiplied_by_ten;
    }
    int times_hundred(int multiply_by_hundred)
    {
    	int multiplied_by_hundred = 0;
    	//this multiplies by 100
    	__asm__("shld   %%eax,%1;"
    				  "shld   %%eax,%1;"
    				  "movl %%eax,%%ebx;"
    				  "shld  %%eax,%1;"
    				  "shld  %%eax,%1;"
    				  "shld  %%eax,%1;"
    				  "movl %%eax,%%edx;"
    				  "shld  %%eax,%1;"
    				  "addl  %%ebx,%%eax;"
    				  "addl  %%edx,%%eax;"
    				  : "=r" (multiplied_by_hundred)
    				  : "a" (multiply_by_hundred)
    				  : "%%eax", "%%ebx", "%%edx"
    	);
    
    	return multiplied_by_hundred;
    }
    int main()
    {
    	cout<<times_hundred(1)<<endl;
    	cout<<times_ten(1)<<endl;
    	cout<<times_hundred(1)<<endl;
    
        return 0;
    }

|In function 'int times_ten(int)':|
error: unknown register name '%%ebx' in 'asm'|
error: unknown register name '%%eax' in 'asm'|
In function 'int times_hundred(int)':|
error: unknown register name '%%edx' in 'asm'|
error: unknown register name '%%ebx' in 'asm'|
error: unknown register name '%%eax' in 'asm'|
In function 'int main()':|
error: unknown register name '%%edx' in 'asm'|
error: unknown register name '%%ebx' in 'asm'|
error: unknown register name '%%eax' in 'asm'|
error: unknown register name '%%ebx' in 'asm'|
error: unknown register name '%%eax' in 'asm'|
error: unknown register name '%%edx' in 'asm'|
error: unknown register name '%%ebx' in 'asm'|
error: unknown register name '%%eax' in 'asm'|
||=== Build finished: 13 errors, 0 warnings ===|

-- 
View this message in context: http://old.nabble.com/GCC-inline-assembler-output-problems-tp32031604p32031604.html
Sent from the gcc - Help mailing list archive at Nabble.com.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]