Page 1 of 2
How To Know What To Do?
Posted: Sun May 23, 2010 3:24 pm
by Nathan
Hello,
Always that I try to build my OS using C or BASIC, I have the same question: How the guy know that it was to do like that? Because as example, let's use the snippet on
Printing to Screen, that we have this to print a string to the screen:
Code: Select all
void write_string(int colour, const char *string)
{
volatile char *video=(volatile char*)0xB8000;
while(*string!=0)
{
*video=*string;
string++;
video++;
*video=colour;
video++;
}
}
How the guy knows that 0xB8000 was the correct address and how did he know that he needed to use it like that: volatile char *video=(volatile char*)?
Best Regards,
Nathan Paulino Campos
Re: How To Know What To Do?
Posted: Sun May 23, 2010 3:34 pm
by Grunt
Nathan wrote:How the guy knows that 0xB8000 was the correct address
I think he tried all of the possible addresses until the damn thing worked.
Re: How To Know What To Do?
Posted: Sun May 23, 2010 3:36 pm
by Nathan
Grunt wrote:Nathan wrote:How the guy knows that 0xB8000 was the correct address
I think he tried all of the possible addresses until the damn thing worked.
And how he discovered how to use it like that?
Re: How To Know What To Do?
Posted: Sun May 23, 2010 3:48 pm
by NickJohnson
He read the manual, obviously. The VGA specification says that video memory starts at 0xB8000 in color character cell mode. As for using volatile, it may have been due to foresight, but more likely his code broke when he typed things too fast and he figured out that he needed to use a volatile pointer.
Re: How To Know What To Do?
Posted: Sun May 23, 2010 4:48 pm
by JackScott
The programmer had a good knowledge of C (or knew where to find such knowledge), a good knowledge of the x86-based PC (or knew where to find such knowledge), and the mental frameworks to tie those two things together in the way that they wanted (which you either get from practise or just generally being awesome).
There's a very simple reason why people on forums tell other people to go "RTFM". That reason is... it's where the facts are found. And facts become knowledge, once you've put some effort into understanding them. "GTFO" usually comes from a reluctance to put effort into understanding the manuals.
Re: How To Know What To Do?
Posted: Sun May 23, 2010 4:59 pm
by Nathan
I still don't understand the why to use that syntax.
Re: How To Know What To Do?
Posted: Sun May 23, 2010 5:02 pm
by JackScott
LOOKIE! Seriously, it's the second Google result for "volatile C".
The key sentence:
Variables declared to be volatile will not be optimized by the compiler because the compiler must assume that their values can change at any time.
Re: How To Know What To Do?
Posted: Sun May 23, 2010 5:09 pm
by fronty
The standard states:
An object that has volatile-qualified type may be modified in ways unknown to the
implementation or have other unknown side effects. Therefore any expression referring
to such an object shall be evaluated strictly according to the rules of the abstract machine,
as described in 5.1.2.3. Furthermore, at every sequence point the value last stored in the
object shall agree with that prescribed by the abstract machine, except as modified by the
unknown factors mentioned previously.[114] What constitutes an access to an object that
has volatile-qualified type is implementation-defined.
[114] A volatile declaration may be used to describe an object corresponding to a memory-mapped
input/output port or an object accessed by an asynchronously interrupting function. Actions on
objects so declared shall not be ‘‘optimized out’’ by an implementation or reordered except as
permitted by the rules for evaluating expressions.
Footnote pretty much sums it in a easy to read way.
EDIT: Damn, Scott posted while I was trying to get my pasting work. This really should warn when a post is made while you are posting.
The Maze of Epic Proportions.
Posted: Sun May 23, 2010 5:13 pm
by Combuster
How To Know What To Do?
There's apparently a problem with your eyesight. It starts with your coloured text, and it ends with your programming abilities. A short story follows. That said, not knowing
what to do is only an indication of having no
purpose. The interesting part is knowing
how to do it.
Basically, life is a really, really large puzzle. Ever considered what happens when you make a step forwards? I won't even explain it as it would take too long. Heck, the time it took me to learn how to do that can be measured in the order of years. Same for you, no doubt. Ever wondered why computer programs and robots fail at things like proper speech recognition, visual recognition, and bipedal movement? Because the programmers want them to just do it and not go through the whole learning process.
Computers are the purest of complex puzzles: there is no such thing of randomness. If we do A on a computer the result can be predicted exactly, assuming we know all the details. Computer programs are solutions to puzzles. Good computer programs solve many similar puzzles. See puzzles as a maze: You have an desired outcome, and several routes you can move in. Each step you take brings you closer, or further away from the solution. The stupid programmers view the maze from the inside. They can only see their location, and the few paths that lead from there. Where the exit is, they don't know. The good programmers start by climbing the nearest wall so they can view the maze in its entirety, including the exit. From their point of view it is easy to plan a route through the maze.
Efficient problem solving is not just overseeing the entire maze, but also thinking above the entire maze. A specific race of monkeys tend to climb whatever they can, and randomly jump down another end to see what's there. What works better is by realizing what paths lead to your exit, and seeing that lots of paths run parallel to each other, split off and come back together moments later. Once you realize this pattern, you can divide the maze into smaller blocks. To reach a certain state, it is easiest to get each part of the state in turn. In the large maze, you'll find that it is subdivided into larger blocks where the paths look alike. By grouping, grouping the result, and repeatedly grouping that result, you get the general direction in which to walk. You can mark out a few points along the way, and then look at the part of the maze just below you. Since you know where you should go, you can then pay attention to individual parts of the maze and find a course that brings you to one block closer to the exit. Once there, you can climb on the wall again and plot more of the details.
In the end, every maze can be solved. Some people are near-sighted and can't ever see far enough to plot a course. Some forget to look. Some are too lazy to climb up the wall. You can easily detect the bad programmers based on these traits. In your case, I hope there exist glasses strong enough to have you see beyond the next wall, let alone see the footholds to climb it. I gave up hope.
And for all the others out there, I wish you clear skies and few monkeys.
Re: How To Know What To Do?
Posted: Sun May 23, 2010 6:31 pm
by montrom
A maze is something there to confuse you in its entire, to expect the person traveling the maze to be always unsure of where the exit is. If you stand upon the wall and see the exit, then the maze is no longer a maze, it has been reduced to nothing more than a intricate walk-way. Which I suspect is everyone's ultimate desire. Thus, a maze to some and a map to others. Good analogy.
Re: How To Know What To Do?
Posted: Sun May 23, 2010 11:09 pm
by neon
Hello,
"How does the programmer know to do it like that?" should not be asked. There are lots of ways to write software, details like 0xb8000 and C syntax are irrelevant to the algorithm the programmer developed. The programmer developed the routine a specific way based on design needs and experience. The programmer knew of 0xb8000 because this is a video output routine and obtained what to do from the VGA specification.
It all comes down to experience, knowing how to search for specifications and documents (Google), and knowing your tool chain and programming languages of choice. All three are vital for any success - not just in OS development but for any complex field of programming.
On a personal note, that routine can be written better.
Re: How To Know What To Do?
Posted: Mon May 24, 2010 11:48 am
by Thomas
Hi Nathan,
Those things are quite obvious for people who have programmed the IBM PC for some time
.When I started I too had many foolish doubts and questions you will clear all of them if you
persist long. As far as I have learned, life is not only about problem solving. Life is also about self discipline,discipline in work ,discipline in your emotions, discipline of thoughts and even discipline in love itself.....
The maze puzzle can be solved by trying out all the possible ways
.Although it might take longer
,each time you make an attempt you will find ways to make a better attempt and you will eventually solve the puzzle. However you should
persist long enough and should be
disciplined enough. I guess instead of asking things here,you can make an attempt and figure out things .I am pretty sure you will figure out many things and come out euphoric solving the puzzle
--Thomas
Re: How To Know What To Do?
Posted: Mon May 24, 2010 12:06 pm
by stephenj
Nathan, the message people here are giving is simple. You can do it too! There is nothing magical about knowing such things, but there is a lot to know.
As Thomas said, just keep at it and you'll get there.
Re: How To Know What To Do?
Posted: Mon May 24, 2010 1:08 pm
by Neolander
Thomas wrote:Hi Nathan,
Those things are quite obvious for people who have programmed the IBM PC for some time
.When I started I too had many foolish doubts and questions you will clear all of them if you
persist long. As far as I have learned, life is not only about problem solving. Life is also about self discipline,discipline in work ,discipline in your emotions, discipline of thoughts and even discipline in love itself.....
The maze puzzle can be solved by trying out all the possible ways
.Although it might take longer
,each time you make an attempt you will find ways to make a better attempt and you will eventually solve the puzzle. However you should
persist long enough and should be
disciplined enough. I guess instead of asking things here,you can make an attempt and figure out things .I am pretty sure you will figure out many things and come out euphoric solving the puzzle
So well said...
Re: How To Know What To Do?
Posted: Tue May 25, 2010 2:10 pm
by technik3k
JackScott wrote:LOOKIE! Seriously, it's the second Google result for "volatile C".
The key sentence:
Variables declared to be volatile will not be optimized by the compiler because the compiler must assume that their values can change at any time.
Actually, in this particular example volatile is unnecessary. Video memory in text mode is not something that is going to change its value randomly. If you are in graphic mode and start switching display planes it is different story.
It used to be that video memory access was much slower than regular memory because all requests had to go through the bus and reads were much slower than writes, but I still don't see how "volatile" could help there.