VGA Memory Byte Order

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
songziming
Member
Member
Posts: 71
Joined: Fri Jun 28, 2013 1:48 am
Contact:

VGA Memory Byte Order

Post by songziming »

I just managed to link asm and c together, and I want to print a char from C

Code: Select all

void kmain(void) {
    unsigned char *video = (unsigned char *)0xb8000;
    video[0] = 'K';
    video[1] = 0x07;
}
But what I got is
Image

Can any one tell me why?

EDIT:
if I do

Code: Select all

video[1] = 'K';
video[0] = 0x07;
I can get the right result, But afaik the higher byte holds the attribute and the lower byte holds the char, why I got opposite after entering C.
Attachments
video.png
video.png (6.47 KiB) Viewed 1059 times
Last edited by songziming on Mon Jun 02, 2014 11:55 am, edited 1 time in total.
Reinventing the Wheel, code: https://github.com/songziming/wheel
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: write video ram from C

Post by Combuster »

My crystal ball says you missed the Posting Checklist, and especially the gcc/ld part.
"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
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: write video ram from C

Post by sortie »

This is probably because how little endian works. I'll admit, this does seem the opposite of what I would expect, too. If changing the order works, just do that instead, though.
songziming
Member
Member
Posts: 71
Joined: Fri Jun 28, 2013 1:48 am
Contact:

Re: write video ram from C

Post by songziming »

Combuster wrote:My crystal ball says you missed the Posting Checklist, and especially the gcc/ld part.
I use clang as the compiler
Reinventing the Wheel, code: https://github.com/songziming/wheel
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: VGA Memory Byte Order

Post by Combuster »

I use clang as the compiler
That implies a number of things:
1) You did not use a tutorial (the shady and worse ones use gcc, the better ones a cross-compiler, but none that I know use clang)
2) Therefore, you have to have invented your own command line invocations
3) All the things regarding improper gcc use can equally apply to clang.

Point is, the shifting of bytes is a signature indication of running code of a different architecture - 64 bit code run in 32-bit mode gives a lot of off-by-one errors due to encoding differences.
"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 ]
alexfru
Member
Member
Posts: 1112
Joined: Tue Mar 04, 2014 5:27 am

Re: VGA Memory Byte Order

Post by alexfru »

If the data segment's base address is off by 1 in the GDT, you may see such behavior.
songziming
Member
Member
Posts: 71
Joined: Fri Jun 28, 2013 1:48 am
Contact:

Re: VGA Memory Byte Order

Post by songziming »

It seems to be the problem of x86-64. If I pass the -m32 option to the compiler, it would get the correct result.
Reinventing the Wheel, code: https://github.com/songziming/wheel
Post Reply