Page 1 of 1

is this inline assembly ok?

Posted: Thu Feb 24, 2005 10:31 am
by Poseidon
i'm not too good at inline assembly.. is this code ok?

Code: Select all


#define read_cr0(void) ({ \
    unsigned ret; \
    __asm__ volatile ("movl %%cr0, %%edx": : "d" (ret)); \
    ret; \
})    

#define read_cr3(void) ({ \
    unsigned ret; \
    __asm__ volatile ("movl %%cr3, %%edx": : "d" (ret)); \
    ret; \
})

#define write_cr0(value) ({ \
    __asm__ volatile ("movl %%edx, %%cr0" :: "d" (value)); \
})    

#define write_cr3(value) ({ \
    __asm__ volatile ("movl %%edx, %%cr3" :: "d" (value)); \
})    

thanx in advance

Re:is this inline assembly ok?

Posted: Thu Feb 24, 2005 11:01 am
by pini
Try this for reading :

Code: Select all

#define read_cr0(void) ({ \
    unsigned ret; \
    __asm__ volatile ("movl %%cr0, %%edx": "=d" (ret) : ); \
    ret; \
})    

#define read_cr3(void) ({ \
    unsigned ret; \
    __asm__ volatile ("movl %%cr3, %%edx": "=d" (ret) : ); \
    ret; \
})
I'm not sure if gcc takes care of your use of edx, maybe you should replace "d" and "=d" by "g" and "=g" to let gcc choose itself which general register to use.

Re:is this inline assembly ok?

Posted: Thu Feb 24, 2005 11:08 am
by Pype.Clicker
#defines do not need "(void)" but instead "()", iirc.

you may want to use static __inline__ readcr0(void) { ... } instead of a define, too ...