OSDev.org
https://forum.osdev.org/

Question On C Code in Bare Bones
https://forum.osdev.org/viewtopic.php?f=1&t=33004
Page 1 of 1

Author:  dPhillips [ Thu Jun 14, 2018 8:24 am ]
Post subject:  Question On C Code in Bare Bones

I've just completed the bare bones tutorial to confirm my system will compile code with the cross compiler and run. Most of the code is very simple to understand, but I had some questions in the terminal_initialize function.

I have a primitive understanding of pointers, but I'm not exactly sure what this is doing:

Code:
terminal_buffer = (uint16_t*) 0xB8000;


I understand 0xB8000 is the VGA address for text mode, and that uint16_t is an unsigned 16 bit number, and that terminal_buffer is an unsigned 16 bit variable. Are we just converting that hex number to 16 bit?

terminal_buffer is setup as an array within the inner x loop; but doesn't C require we specify the bounds of an array when we set it up?

Code:
terminal_buffer[index] = vga_entry(' ', terminal_color);


Lastly (for now), I wanted to confirm what terminal_buffer is doing. It is effectively, using the VGA_WIDTH, VGA_HEIGHT to create the "area" of our terminal. The two loops are creating our rows and columns that are the terminal bounds.

--Edit: My apologies, I just saw the wiki portion of the forum at the bottom. I will ensure any future questions are posted there.

Author:  dseller [ Thu Jun 14, 2018 8:49 am ]
Post subject:  Re: Question On C Code in Bare Bones

Quote:
I understand 0xB8000 is the VGA address for text mode, and that uint16_t is an unsigned 16 bit number, and that terminal_buffer is an unsigned 16 bit variable. Are we just converting that hex number to 16 bit?

The terminal_buffer variable is not a 16-bit variable, it's a pointer to a series of 16-bits values. The pointer is, probably, a 32-bits value (this depends on the architecture) that stores the address to this series of 16-bits values. In the code snippet, you're just saying that the terminal_buffer pointer points to 0xB8000.

Quote:
terminal_buffer is setup as an array within the inner x loop; but doesn't C require we specify the bounds of an array when we set it up?

You are correct; usually C requires the bounds when you create an array. However, this is not an array, it's a pointer.

Quote:
Lastly (for now), I wanted to confirm what terminal_buffer is doing. It is effectively, using the VGA_WIDTH, VGA_HEIGHT to create the "area" of our terminal. The two loops are creating our rows and columns that are the terminal bounds.

I'm not really sure what you mean here. But I do strongly advise you to start reading up on pointers and how they work. You absolutely will need to thoroughly understand this mechanic in order to be able to write an OS.

Author:  dPhillips [ Thu Jun 14, 2018 9:55 am ]
Post subject:  Re: Question On C Code in Bare Bones

I completely missed the * for uint16_t. Thank you for calling it out. That makes more sense now. I did pick up a book on pointers that i started this week (Pointers on C 1st Edition). I'm also open to other suggestions if you have any for pointers. I get the concept, but it get's complicated.

The last question was confirming what the loop was doing.

Code:
static const size_t VGA_WIDTH = 80;
static const size_t VGA_HEIGHT = 25;


Code:
   for (size_t y = 0; y < VGA_HEIGHT; y++) {
      for (size_t x = 0; x < VGA_WIDTH; x++) {
         const size_t index = y * VGA_WIDTH + x;
         terminal_buffer[index] = vga_entry(' ', terminal_color);
      }
   }


This loop is setting up the terminal; I just wanted to make sure I was correct. It starts off at Column 0, and then creates 25 rows. It then goes to column 1, and creates another 25 rows... getting to column 80 and setting up another 25 rows. It's a two dimensional loop for creating our terminal. the terminal_buffer is just setting up blanks with our color scheme for now.

I'm really just making sure I fully understand the whole kernel.c before I move to the meaty skeleton. Most of it is very straight forward, I've just never done real low level programming like this. The fact that I even got "Hello world" to print on bare hardware was fun.

Author:  simeonz [ Thu Jun 14, 2018 11:52 am ]
Post subject:  Re: Question On C Code in Bare Bones

dPhillips wrote:
This loop is setting up the terminal; I just wanted to make sure I was correct. It starts off at Column 0, and then creates 25 rows. It then goes to column 1, and creates another 25 rows... getting to column 80 and setting up another 25 rows. It's a two dimensional loop for creating our terminal. the terminal_buffer is just setting up blanks with our color scheme for now.
It looks more like it runs row by row, rather than column by column to me.

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/