OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 5:36 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Question On C Code in Bare Bones
PostPosted: Thu Jun 14, 2018 8:24 am 
Offline

Joined: Sun Jun 10, 2018 9:28 am
Posts: 2
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.


Top
 Profile  
 
 Post subject: Re: Question On C Code in Bare Bones
PostPosted: Thu Jun 14, 2018 8:49 am 
Offline
Member
Member

Joined: Thu Jul 03, 2014 5:18 am
Posts: 84
Location: The Netherlands
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.

_________________
My blog: http://www.rivencove.com/


Top
 Profile  
 
 Post subject: Re: Question On C Code in Bare Bones
PostPosted: Thu Jun 14, 2018 9:55 am 
Offline

Joined: Sun Jun 10, 2018 9:28 am
Posts: 2
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.


Top
 Profile  
 
 Post subject: Re: Question On C Code in Bare Bones
PostPosted: Thu Jun 14, 2018 11:52 am 
Offline
Member
Member

Joined: Fri Aug 19, 2016 10:28 pm
Posts: 360
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], SemrushBot [Bot] and 81 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group