[Solved]Inline assembler

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
User avatar
leyley
Member
Member
Posts: 35
Joined: Thu Jun 28, 2012 1:54 am

[Solved]Inline assembler

Post by leyley »

I'm writing a C function that uses inline assembler to move data into a memory, but it doesn't work. How can I solve this problem?
Thanks!

Code: Select all

/* Output a charactor; 32bits protected mode; */
void __putc(unsigned short x,unsigned short y,unsigned char c,unsigned char s){
	unsigned int p=(y*0x50+x)*2;
	unsigned short ax=s|(c<<8);
	__asm__ __volatile__("movl $0xb8000,%%ecx\n\t"
	"addl %0,%%ecx\n\t"
	"movw %1,%%ax\n\t"
	"movw %%ax,(%%ecx)"::"b"(p),"d"(ax));
}
Last edited by leyley on Sun Sep 02, 2012 8:32 pm, edited 1 time in total.

Code: Select all

#rm -rf /
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Inline assembler

Post by iansjack »

Look at the generated code. It should then be obvious what is going wrong.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Inline assembler

Post by Combuster »

It actually works for me. Although using assembler in this fashion is rather pointless - it's slower than it's equivalent C code.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
leyley
Member
Member
Posts: 35
Joined: Thu Jun 28, 2012 1:54 am

Re: Inline assembler

Post by leyley »

Combuster wrote:It actually works for me. Although using assembler in this fashion is rather pointless - it's slower than it's equivalent C code.
Yep, I use it to store the data into registers to test... How can you did it? Would you give me some information about your developing configuration? thanks.
I'm using a DELL e5420 with Archlinux x86_64, all of my software has its lastest version in the official repo.
The simulator is Virtualbox and bochs.
Thanks!

Code: Select all

#rm -rf /
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Inline assembler

Post by Combuster »

"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
leyley
Member
Member
Posts: 35
Joined: Thu Jun 28, 2012 1:54 am

Re: Inline assembler

Post by leyley »

Combuster wrote:GCC Cross-Compiler
Maybe the reason why it doesn't work for me is the 64bits arch of my host?
I will try to compile them at a 32bits guest system...

Code: Select all

#rm -rf /
User avatar
leyley
Member
Member
Posts: 35
Joined: Thu Jun 28, 2012 1:54 am

Re: Inline assembler

Post by leyley »

After using 32bits gcc & ld, it finally works!
When I compilied the C source with -S option(output assembler), I've found that the 64bits environment will add a 'cltq' instrument at the end of movs.

Code: Select all

void __putc(unsigned short x,unsigned short y,unsigned char c,unsigned char s){
	unsigned int *p=0xb8000+(y*0x50+x)*2;
	unsigned short ax=s|(c<<8);
	(*p)=ax;
}

Code: Select all

#rm -rf /
Post Reply