0

I'm developing my own operating system. I have completed the boot sector and loaded my kernel successfully. My development environment is Ubuntu 14.04 and GCC 4.8. My kernel will run under BOCHS. I wanted to print a specific character in a specific position on the screen, so I created a function like this:

void print_char(char target, int col, int row, unsigned char attribute) {
    //attribute controls the colour of the character
    char * vidmem = (char *) VIDEO_ADDRESS; //#define VIDEO_ADDRESS 0xb8000
    *vidmem = target;
    //TODO: add other control statements
}

I call this function in my main function, which is the entry point of my kernel:

print_char('T', 0, 0, (unsigned char) 0x0f);

The code above is supposed to print the character 'T' at the top-left of the screen. It does't show up! After I changed the declaration of the print_char:

void print_char(char target, int col, int row, int attribute)

Or something like this:

void print_char(char target, int col, int row) then call it like print_char('T', 0, 0)

After I change it, everything works! I am really confused about this problem. Can any one explain it to me?

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • It's pretty much impossible for us to help you with such incomplete information. But when you have strange symptoms like this it is often a result of bugs in the code causing Undefined Behaviour. The bug may not even be in the part of the code where symptoms manifest. But there is no way we can tell from the info you have provided. – kaylum Mar 30 '16 at 03:18
  • I haven't used Bochs in a while, but I remember that it has a relatively decent assembly debugger. – user253751 Mar 30 '16 at 03:26
  • Maybe I am just misreading the code. But assume `*vidmem = attribute`; is executed, and `vidmem` has address 0xB8000 that would write the attribute to 0xB8000. 0xB8000 is the first character, 0xB8001 is attribute of first character, 0xb8002 is second character and 0xb8003 is attribute of second character and so on. I guess my point is, the attribute seems to be written to the wrong byte. – Michael Petch Mar 30 '16 at 04:42
  • 2
    `*vidmem = target;` and `*(vidmem+1) = attribute;` would seem to make more sense. – Michael Petch Mar 30 '16 at 04:47
  • Sorry, I mean to write `*vidmem = target;` – Xiaosong He Mar 30 '16 at 07:07
  • I want to debug it and watch what the value of `target` is, but I do not know how to do it under my environment. – Xiaosong He Mar 30 '16 at 07:12
  • Possible duplicate of [Unexpected output when printing directly to text video memory](https://stackoverflow.com/questions/39807710/unexpected-output-when-printing-directly-to-text-video-memory) – Michael Petch Aug 28 '19 at 20:11

1 Answers1

1

I have fix the problem by modify the flags of gcc and ld to generate a 32-bit .o file and '.bin' file (My ubuntu is a 64-bit version) by the following statemens.

%.o: %.c ${HEADERS} gcc -ffreestanding -m32 -c $< ${CFLAG} -o $@

kernel.bin: kernel/kernel_entry.o ${OBJ} ld -o $@ -melf_i386 -Ttext 0x1000 $^ --oformat binary

where kernel_entry.o will make sure our routine finds the entry of main function in kernel code. then I get my os image by:

os-image: boot/boot_sect.bin kernel.bin cat $^ > os-image

where boot_sect.bin plays the role of a boot sector.

But I still don't know why the 64-bit objective file will cause the phenomena I described above...