c and assembly problem

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

c and assembly problem

Post by huh »

I write my os mostly in c, but sometimes I NEED to use assembly. if i define a variable in c:

Code: Select all

main()
{
  long var = 0;
}
and then i want to do something with it in assembly:

Code: Select all

asm("movl $1, var\n");
the gcc compiler gives me this error:

Code: Select all

/tmp/ccpKcN36.o: In function `main':
test.c:(.text+0xf): undefined reference to `var'
collect2: ld returned 1 exit status
why?
User avatar
bubach
Member
Member
Posts: 1223
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

Re: c and assembly problem

Post by bubach »

The wiki has a section about inline assembly, and examples of correct syntax:
http://wiki.osdev.org/Inline_Assembly
"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: c and assembly problem

Post by qw »

This is GCC specific. In MSVC it is perfectly valid to use local variable names in inline assembly directly. This is mainly a matter of syntax.
Post Reply