Welcome, Guest. Please login or register.

Author Topic: getreg() SASC function under GCC ...  (Read 1170 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline dcr8520Topic starter

  • Full Member
  • ***
  • Join Date: Mar 2002
  • Posts: 107
    • Show only replies by dcr8520
    • http://Amiga.SourceForge.net
getreg() SASC function under GCC ...
« 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.

 

Offline CodeSmith

  • Sr. Member
  • ****
  • Join Date: Sep 2002
  • Posts: 499
    • Show only replies by CodeSmith
Re: getreg() SASC function under GCC ...
« Reply #1 on: September 25, 2003, 06:44:28 PM »
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;
}
 

Offline falemagn

  • Sr. Member
  • ****
  • Join Date: May 2002
  • Posts: 269
    • Show only replies by falemagn
    • http://www.aros.org/
Re: getreg() SASC function under GCC ...
« Reply #2 on: September 25, 2003, 08:51:36 PM »
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.