is this inline assembly ok?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Poseidon

is this inline assembly ok?

Post 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
pini

Re:is this inline assembly ok?

Post 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.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:is this inline assembly ok?

Post 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 ...
Post Reply