Amiga.org
Operating System Specific Discussions => Amiga OS => Amiga OS -- Development => Topic started by: dcr8520 on September 25, 2003, 06:23:00 PM
-
Hi all,
I have a piece of code with the follow call:
...
a = getreg(REG_D1);
...
I tryed to implement it using:
long getreg( int null)
{ register long x __asm("d1");
return x;
}
but seems it doenst work properly...
I have no much idea about registers anyway...
can someone let me know how I must do it ?
Thanks in advance..
Kind regards.
-
I haven't used 68K gcc in a while, but I think this should work (I used a similar trick in x86 a while back):
extern inline unsigned long get_d1()
{
unsigned long retval asm("d1");
return retval;
}
int main()
{
printf("the value of D1 is %ld\n",get_d1());
return 0;
}
-
You don't want to use normal functions to do that, because some registers could be overwritten in the very process of calling the function and/or returning from it. Moreover, the usage of the asm construct that way is not very reliable, as gcc uses it only as an "hint". What you want to do is using proper inline assembly, better if wrapped up in a macro like this one (Oh, I so wish the [code] tag worked here...):
#define GET_REG(reg) \
({ \
register unsigned long ret; \
\
__asm__ __volatile__ \
( \
"movl %%" #reg ",%0\n": \
"=r"(ret)::"cc" \
); \
\
ret; \
})
To use it, simply call it like this:
GET_REG(), where can be one of any of the registers, like d0, d1, and so on.