what is wrong in this code ?

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
rasesh
Posts: 6
Joined: Sat Nov 08, 2014 12:01 pm

what is wrong in this code ?

Post 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.
psychobeagle12
Member
Member
Posts: 41
Joined: Wed Oct 26, 2011 9:31 am

Re: what is wrong in this code ?

Post by psychobeagle12 »

Can we see the code for cprint(c)? May be helpful to see what else is going on here.
My i386-based kernel: https://github.com/bmelikant/missy-kernel
Picking a name for my kernel was harder than picking my wife, so I just used her name until I decide!
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: what is wrong in this code ?

Post 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.
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: what is wrong in this code ?

Post by sortie »

You cannot compile to 16 bit code with gcc. Use assembly or switch to 32 bit mode.
rasesh
Posts: 6
Joined: Sat Nov 08, 2014 12:01 pm

Re: what is wrong in this code ?

Post 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) );
}
rasesh
Posts: 6
Joined: Sat Nov 08, 2014 12:01 pm

Re: what is wrong in this code ?

Post 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.
rasesh
Posts: 6
Joined: Sat Nov 08, 2014 12:01 pm

Re: what is wrong in this code ?

Post 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
.
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: what is wrong in this code ?

Post 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.
User avatar
iansjack
Member
Member
Posts: 4708
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: what is wrong in this code ?

Post 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
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: what is wrong in this code ?

Post 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.
"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 ]
rasesh
Posts: 6
Joined: Sat Nov 08, 2014 12:01 pm

Re: what is wrong in this code ?

Post 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.
rasesh
Posts: 6
Joined: Sat Nov 08, 2014 12:01 pm

Re: what is wrong in this code ?

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