Page 1 of 1

what is wrong in this code ?

Posted: Sat Nov 08, 2014 12:10 pm
by rasesh

Code: Select all

void bprint(short bp,short size)
{
	char c;	
	while(size)
	{
	    __asm__ __volatile__("movb %%es:%1,%0\n":"=a"(c):"m"(bp));
	    cprint(c);	
	    __asm__ __volatile__("incw %0"::"m"(bp));
	    size--;
	}
}
I am trying to print buffer which is pointed by bp and this buffer reside in ES.
while i am running this code in bochs emulator it showing following error:
write_virtual_checks(): write beyond limit, r/w

thank you in anticipation.

Re: what is wrong in this code ?

Posted: Sat Nov 08, 2014 12:49 pm
by psychobeagle12
Can we see the code for cprint(c)? May be helpful to see what else is going on here.

Re: what is wrong in this code ?

Posted: Sat Nov 08, 2014 6:17 pm
by thepowersgang
Ok, quite a few wtfs here.

1. Why are you trying to read from ES:BP in C code?
2. Have you checked that the generated assembler is sane.
3. Have you double-checked the register values that cause the error? Including comparing the register used by the load to the base of ES.

Re: what is wrong in this code ?

Posted: Sat Nov 08, 2014 7:06 pm
by sortie
You cannot compile to 16 bit code with gcc. Use assembly or switch to 32 bit mode.

Re: what is wrong in this code ?

Posted: Sun Nov 09, 2014 1:56 am
by rasesh
psychobeagle12 wrote:Can we see the code for cprint(c)? May be helpful to see what else is going on here.
void cprint(short c)
{
/*INT 0x10 - VIDEO TELETYPE OUTPUT
AH = 0x0E
AL = Character to write*/
//print character
__asm__ __volatile__ ("int $0x10" : : "a"(0x0e00 | c), "b"(0x0007) );
}

Re: what is wrong in this code ?

Posted: Sun Nov 09, 2014 2:01 am
by rasesh
thepowersgang wrote:Ok, quite a few wtfs here.

1. Why are you trying to read from ES:BP in C code?
2. Have you checked that the generated assembler is sane.
3. Have you double-checked the register values that cause the error? Including comparing the register used by the load to the base of ES.
I use ES segment as buffer so every file open is going to reside in ES.That why i am trying to print the block started at ES:BP.

Re: what is wrong in this code ?

Posted: Sun Nov 09, 2014 2:06 am
by rasesh
sortie wrote:You cannot compile to 16 bit code with gcc. Use assembly or switch to 32 bit mode.
yes you can http://stackoverflow.com/questions/2713 ... -16-bit-os using
.code16gcc
.

Re: what is wrong in this code ?

Posted: Sun Nov 09, 2014 3:13 am
by sortie
rasesh wrote:
sortie wrote:You cannot compile to 16 bit code with gcc. Use assembly or switch to 32 bit mode.
yes you can http://stackoverflow.com/questions/2713 ... -16-bit-os using
.code16gcc
.
No you can't. This is why your code doesn't work. Use assembly or do 32 bit code. Stack overflow is not evidence. The GNU assembler does support 16 bit code, gcc doesn't.

Edit: Ok. .code16gcc does work, it doesn't make gcc support 16 bit code, it just magically translate at assembly time. I don't consider this a good approach, but it might just work.

Re: what is wrong in this code ?

Posted: Sun Nov 09, 2014 4:05 am
by iansjack
The Linux kernel uses gcc to produce 16-bit code. It's not straightforward, but it is possible:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59672

More details:

http://dc0d32.blogspot.co.uk/2010/06/re ... iting.html

Re: what is wrong in this code ?

Posted: Sun Nov 09, 2014 6:19 am
by Combuster
sortie wrote:You cannot compile to 16 bit code with gcc. Use assembly or switch to 32 bit mode.
The correct formulation is "If a person requires help, that person cannot compile to 16 bit code with gcc". The bug in the code is "You are using code16gcc when you don't know the implications at all.", and everything in what you posted show you are relying on someone else's magic that you just happen to have copied and can't deal with.

The appropriate solution definitely is to use proper assembly, if only to learn how things should work in reality.

Re: what is wrong in this code ?

Posted: Sun Nov 09, 2014 9:32 am
by rasesh
iansjack wrote:The Linux kernel uses gcc to produce 16-bit code. It's not straightforward, but it is possible:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59672

More details:

http://dc0d32.blogspot.co.uk/2010/06/re ... iting.html

Thank you for your answer. I use the same approach to compile my code but first i used .code16 instead of .code16gcc directive. Now it is working fine.

Re: what is wrong in this code ?

Posted: Sun Nov 09, 2014 9:54 am
by rasesh
Combuster wrote:
sortie wrote:You cannot compile to 16 bit code with gcc. Use assembly or switch to 32 bit mode.
The correct formulation is "If a person requires help, that person cannot compile to 16 bit code with gcc". The bug in the code is "You are using code16gcc when you don't know the implications at all.", and everything in what you posted show you are relying on someone else's magic that you just happen to have copied and can't deal with.

The appropriate solution definitely is to use proper assembly, if only to learn how things should work in reality.
yes I am newbie.I trying to do something by myself.This not someone else magic.My git repo https://github.com/raseshshah/Ranix. This might not as neat code as you are able to write but this is my first attempt. I wanted to write 16 bit kernel in c , that's why i used inline assembly whenever there is need.

Btw thanks for your kind words.