C++ video memory access

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
jpal
Posts: 3
Joined: Sat Jan 12, 2013 10:35 am

C++ video memory access

Post by jpal »

In my kernel I am trying to write to the screen.

Code: Select all

*((char*)0xB8000) = 72;
*((char*)0xB8001) = 15;
which compiles to

Code: Select all

movb $72, ($0xB8000)
movb $15, ($0xB8000)
displays an 'H', but

Code: Select all

char* ptr = (char*)0xB8000;
*ptr = 72;
*(ptr + 1) = 15;
which compiles to

Code: Select all

movl $0xB8000, -0x4(%ebp)
mov -0x4(%ebp), %eax
movb $72, (%eax)

mov -0x4(%ebp), %eax
add %eax, 1
movb $15, (%eax)
does not show any characters.

I do use a cross compiler so that is not the problem.
Please help! Thanks.
YaYo
Posts: 6
Joined: Fri Dec 06, 2013 1:49 pm

Re: C++ video memory access

Post by YaYo »

maybe because you are using a char* pointer,
i.e. :
char* ptr = (char*)0xB8000;
*ptr = 72; // now ptr equals 0xB8000
*(ptr + 1) = 15; // but now is (0xB8000 + sizeof(char*)) equals 0xB8004
// try casting.
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: C++ video memory access

Post by thepowersgang »

@YaYo - Actually, that's not the problem.

@OP - Are you running on bochs/qemu and putting a cli;hlt loop soon afterwards? Iirc one (or both) of these will stop updating the screen when the CPU is halted in this manner.

The code looks correct to me (pretty obviously compiled with optimisaton off), so it's probably either the code not running, or something else hiding its effect.
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
YaYo
Posts: 6
Joined: Fri Dec 06, 2013 1:49 pm

Re: C++ video memory access

Post by YaYo »

try to change this line
*(ptr + 1) = 15;
to this *((char*)(((unsigned long)ptr) + 1)) = 15;.

Note: Bochs, Qemo, VBox,...etc have nothing to do with this problem.
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: C++ video memory access

Post by thepowersgang »

@YaYo - Please actually check what you're saying, and don't comment unless you have actually read and understood the post.

The generated assembly is correct (It's adding one to the address, not four). Which points to an error outside of the provided code, which is most likely bochs/qemu not updating the screen (this has caught almost everyone once, myself included)
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
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: C++ video memory access

Post by Combuster »

jpal wrote:In my kernel I am trying to write to the screen.

Code: Select all

*((char*)0xB8000) = 72;
*((char*)0xB8001) = 15;
which compiles to

Code: Select all

movb $72, ($0xB8000)
movb $15, ($0xB8000)
That's obviously wrong. I get
0: c6 05 00 80 0b 00 48 movb $0x48,0xb8000
7: c6 05 01 80 0b 00 0f movb $0xf,0xb8001
What else did you not copy-paste properly?
"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 ]
evoex
Member
Member
Posts: 103
Joined: Tue Dec 13, 2011 4:11 pm

Re: C++ video memory access

Post by evoex »

Combuster wrote: What else did you not copy-paste properly?
I'd daresay this line:

Code: Select all

add %eax, 1
Which ought to be:

Code: Select all

add $1, %eax
Post Reply