defining registers in c

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
huh
Posts: 13
Joined: Wed Jan 25, 2012 10:05 am

defining registers in c

Post by huh »

I use c for my os and sometimes have to use the registers in my code, I know I can use asm(); but I want to know how I can define them for use in c code. Example: Say I want to have an if statement: if ( reg_eax == 0 ) { do this }. I know Linux defines at least the segment registers becaouse I downloaded the linux kernel and looked in linux/arch/x86/boot/boot.h and saw that it defined the registers somehow but I can't make sense of it.
User avatar
bubach
Member
Member
Posts: 1223
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

Re: defining registers in c

Post by bubach »

Google says:

Code: Select all

register int *foo asm ("eax");
"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
huh
Posts: 13
Joined: Wed Jan 25, 2012 10:05 am

Re: defining registers in c

Post by huh »

Code: Select all

register int *foo asm ("eax");
So that wil define foo as constant poiter to eax register like in an if statement? didn't you mean asm("%eax");.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: defining registers in c

Post by bluemoon »

huh wrote:I use c for my os and sometimes have to use the registers in my code
If you do it that way you are slicing sushime with katana. You will volatile the C specification with has no concept of x86 registers.
I suggest inline assembly or separate assembly stub to manipulate registers.
User avatar
bubach
Member
Member
Posts: 1223
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

Re: defining registers in c

Post by bubach »

Why don't you give it a try, and find out? That also goes for using Google.
"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
Post Reply