extern "C" int mul_them(int a,int b); /* C++ prototype for assembly routine */
__asm__( /* assembly code, inserted as a string */
"mul_them:\n"
" mov 4(%esp),%eax # Pull first argument (a) off the stack\n"
" mov 8(%esp),%ebx # Pull second argument (b) off the stack\n"
" mul %ebx # Multiplies eax by ebx \n"
" ret # Returns value stored in %eax\n"
);
int foo(void) {
return mul_them(read_input(),0x20000);
}
Once your program is working correctly, given input 2 foo should exit with 0x40000. Don't worry about overflow, unless you want to show off!
extern "C" int mul_them(int a,int b); /* C++ prototype for assembly routine */
__asm__( /* assembly code, inserted as a string */
"mul_them:\n"
" push %ebp\n"
" mov %esp,%ebp\n"
" mov 8(%ebp),%eax # Pull first argument (a) off the stack\n"
" mov 12(%ebp),%ebx # Pull second argument (b) off the stack\n"
" mul %ebx # Multiplies eax by ebx \n"
" leave # same as mov %ebp, %esp & pop %ebp\n"
" ret # Returns value stored in %eax\n"
);
int foo(void) {
return mul_them(read_input(),0x20000);
}
extern "C" int mul_them(int a,int b); /* C++ prototype for assembly routine */
__asm__( /* PowerPC assembly code, inserted as a string */
".section \".text\"\n"
"mul_them:\n"
" mullw %r3, %r3, %r4\n"
" blr # Returns value stored in %r3\n"
);
int foo(void) {
return mul_them(read_input(),0x20000);
}
extern "C" void bar(int a,int b) /* C++ routine callable from Assembly */
{
std::cout<<a<<" "<<b<<"\n";
fflush(stdout);
}
int foo(void) {
__asm__( /* assembly code, inserted as a string */
" push $7\n"
" push $3\n"
" call bar\n"
" add $8,%esp # Clean up stack afterwards, by popping both arguments\n"
);
return 0;
}
O. Lawlor, ffosl@uaf.edu
Up to: Class Site, CS, UAF