Printing to the screen in protected mode

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
serge2k
Posts: 13
Joined: Mon Jun 08, 2009 1:00 am

Printing to the screen in protected mode

Post by serge2k »

Hi

I'm trying to print in protected mode and it's behaving very strangely.

I just got my kernel to boot from grub, haven't done anything yet.

I'm following the C++ barebones, with the gaps filled in using the C barebones. SO I have the nasm loader.s on the C barebones with the loops for constructors added in.

This works:

mov [0xb8000], 0x07690748
in loader.s right before calling my kernel

*((int*)0xb8000)=0x07690748;
works in the kernel

this doesn't
unsigned char *videoram = (unsigned char *) 0xb8000;
videoram[0] = 65; /* character 'A' */
videoram[1] = 0x07; /* forground, background color. */

this doesn't
for(int i = 0;i < 10; i++)
{

}
*((int*)0xb8000)=0x07690748;

this does
for(int i = 0;i < 10; i++)
{
*((int*)0xb8000)=0x07690748;
}

this prints high but then never prints the A
for(int i = 0;i < 10; i++)
{
*((int*)0xb8000)=0x07690748;
}
mov WORD [0xb8000], 0x0741

any ideas? is this normal, it seems like odd behaviour.
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Printing to the screen in protected mode

Post by gerryg400 »

Is your linker script okay?
If a trainstation is where trains stop, what is a workstation ?
skyking
Member
Member
Posts: 174
Joined: Sun Jan 06, 2008 8:41 am

Re: Printing to the screen in protected mode

Post by skyking »

Also you should probably use volatile qualified pointers.
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Printing to the screen in protected mode

Post by xenos »

Have you checked whether the compiler outputs what you expect?

Have you properly set up a stack? It seems that writing to the screen fails whenever you use stack variables (i, videoram).
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
serge2k
Posts: 13
Joined: Mon Jun 08, 2009 1:00 am

Re: Printing to the screen in protected mode

Post by serge2k »

linker script

Code: Select all

ENTRY (loader)

SECTIONS
{
	. = 0x100000;

	.text ALIGN(4096) :
	{
		*(.text*)
		*(.gnu.linkonce.t*)
	}

	.rodata ALIGN(4096) :
	{
		*(.rodata*)
		*(.gnu.linkonce.r*)
	}
	
	.data ALIGN(4096) :
	{
		start_ctors = .;
		*(.ctor*)
		end_ctors = .;

		start_dtors = .;
		*(.dtor*)
		end_dtors = .;
	
		*(.data*)
		*(.gnu.linkonce.d*)
	}

	.bss ALIGN(4096) :
	{
		*(.COMMON*)
		*(.bss*)
		*(.gnu.linkonce.b*)
	}
	
	 /DISCARD/ :
	 {
		*(.comment)
		*(.eh_frame) /* You should discard this unless you're implementing runtime support for C++ exceptions. */
	 }
}
for the stack I did this (as in the barebones)

Code: Select all

; reserve initial kernel stack space
STACKSIZE equ 0x4000                  ; that's 16k.

loader:	;entry point
	mov esp, stack-STACKSIZE	;setup a stack

then at the bottom

section .bss
align 4
stack:
	resb STACKSIZE	;reseve stack space
does the gui debugger for bochs work in windows or is that feature linux only? would it work in cygwin?
User avatar
zity
Member
Member
Posts: 99
Joined: Mon Jul 13, 2009 5:52 am
Location: Denmark

Re: Printing to the screen in protected mode

Post by zity »

First of all, you should take a look at the linker script here: http://wiki.osdev.org/Bran%27s_Known_Bugs

Secondly, you have a bug in your stack code. You need to add the stack size, not subtract it..

Code: Select all

loader:   ;entry point
   mov esp, stack+STACKSIZE   ;setup a stack
Remember, the stack grows downwards..

EDIT: I should probably be more specific about the linker script. You don't specify an output format and you don't specify any addresses in the linker script. These two things are shown in the script example I linked to. I'm not sure if that's whats coursing your problem, but your might look into it.
serge2k
Posts: 13
Joined: Mon Jun 08, 2009 1:00 am

Re: Printing to the screen in protected mode

Post by serge2k »

zity wrote:First of all, you should take a look at the linker script here: http://wiki.osdev.org/Bran%27s_Known_Bugs

Secondly, you have a bug in your stack code. You need to add the stack size, not subtract it..

Code: Select all

loader:   ;entry point
   mov esp, stack+STACKSIZE   ;setup a stack
Remember, the stack grows downwards..

EDIT: I should probably be more specific about the linker script. You don't specify an output format and you don't specify any addresses in the linker script. These two things are shown in the script example I linked to. I'm not sure if that's whats coursing your problem, but your might look into it.
Works now, thanks.

edit: to be clear, it was the stack issue.

one typo causes massive issues.
Post Reply