Java For Starters: Difference between revisions

[unchecked revision][unchecked revision]
Content deleted Content added
m Bot: Replace deprecated source tag with syntaxhighlight
 
(8 intermediate revisions by 5 users not shown)
Line 7:
==The problem with OS development in Java==
 
The main problem for people writing an OS in a language without flexible native tools is the lack of the tools. Also Java program needs Java runtimeRuntime environmentEnvironment (JRE) to be run. But during OS startup there's no JRE and we should somehow manage to run Java program without it. As we can see, there's a need in tools and runtime support now. More information on using different languages in OS development is present [[Languages | here]].
 
==How to run you without a car?==
Line 13:
Well, it's simple - you can use your legs. But yes, it's not very convenient. And it's better to have some kind of a vehicle to help you to get some speed. That's why it is important to understand what tools we have.
 
First, it is possible to use some combination of tools, written in C or assemblyAssembly, with some source code, written according to the Java syntax, and another part of code, written in C or assemblyAssembly. It's actually the way many people prefer, [[Pascal_Bare_Bones | and here is the Pascal example]], which shows such approach. But it should be noted, there's just a small part of the job done in Pascal. Without tools, written in other languages, there will be no Pascal OS. Also, without code in other languages (mostly assembly) there will be no Pascal OS.
 
Second, it's possible to write absolutely everything in the preferred language. And of course, it means somebody should write the "everything". Because in case of many languages there's no native tools available, it's the OS developer who should write "everything".
Line 21:
==Here are some tools available for Java OS developers.==
 
First, of course, it's the standard tool set [[Getting_Started#Choosing_your_development_environment | as described here]]. It supports the first way of doing things - just some language flavor around the same C and assemblyAssembly based environment. And the flavor can be compiled by the [https://gcc.gnu.org/java/ GNU Compiler for the Java] (GCJ), for example. The GCJ was written in C and supports only standard C related conventions. If a developer prefers this way it should be noted this article is not intended to be of a great help for such approach.
 
The second option (everything in Java) is described below. But it introduces some tools, already written in Java. The tools here are used as an example and there's no attempt to enforce you to use them. Following article shows how to create a simple bootloader in Java. There is a jump in the end of the bootloader code and all your potential Java OS can start after the jump is performed. So, your OS in Java can start with this article and end in some very interesting form, unseen until you'll have it done.
Line 36:
First we have to look in the depth of the software execution. For the software to be executed there should be a processor which is able to understand software's instructions. Usually the code you see in many languages is not understandable by the processor. So, there's a translation layer between the code and the executable software. But the translation can be implemented in many forms. First it was the assembly. People had written short textual commands and software was required to translate the commands in the form the processor understands. Next people understood it's very tedious and time consuming to write software in such a form of short textual commands, so the high level languages were born. And as we know, Java is the one. But the translation level between Java and the actually executed code hides a lot of details. It is done for a reason. It helps a developer to concentrate on the goal instead of gory details. And with the goal achieved faster we, unfortunately, lose some flexibility the low level commands have. And finally people managed to mix the high level with the low level in a consistent manner. It was called "inline assembly". It's not ideal and there's still a way for better compilers with the ability to understand such a mixture without requiring a programmer to jump between two languages. But unfortunately, such compilers are still far away from Java developers. However, the idea of inline assembly works perfectly even in Java.
 
Now we can try to look at the way Java can inline the assembly. First, of course, there should be [http://jembryos.org/inline.html the assembly for Java]. It's not the only possible implementation, but other variants are far too different from the assembly syntax. Because the assembly syntax is widely used in the hardware documentation and sowtwaresoftware examples it is a good idea to have the inline version which is as close to the generally accepted assembly syntax as possible.
 
And as we mentioned above there can be voices like "it's not Java!", we have to clear the subject a bit. If we remember the way a program goes along before being executed we can see some intermediary steps. It's the translation phase. The translation phase hides low level details and produces an executable file. But if we forget about the translation then it really can look like this - we write a program and (after some magic applied) it just runs. Yes, we can forget about translation and be happy with the actually running program. But the OS development is a thing that just requires us to go into the low level details. And if we go then it becomes obvious there's no more "the program" and there are just some bytes, produced by tools and executed by a processor. The actual execution details can be very different from what we expect while looking at the initial program. And the following information (hopefully) can show how things actually work between the layer of the programming language abstraction and the hardware layer. It's not Java or not Java talk, it's just way of doing things in Java. The same is true for every C or assembly program - there just must be some tools for the translation phase to work. And the tools can be written in C or assembly or (yes!) in Java.
Line 47:
 
The collection of assembly instructions can look like this:
<sourcesyntaxhighlight lang="java">
mov.x32(EAX,EDX);
</syntaxhighlight>
</source>
 
* Here the "mov" is the class's field name.
Line 58:
 
Next it is required to understand how the instruction above can be executed. For it to be possible the translation phase is required. But before the translation step it is usually a good idea to have the actual program. The program actually is just a number of text strings in the form described above. It can be good to separate code fragments intended for different purposes in separate methods like this:
<sourcesyntaxhighlight lang="java">
protected void writeBootCodeInitialization(short stackAddress, short bootLoaderAddress)
{
Line 69:
mov.x16(SP,stackAddress);
}
</syntaxhighlight>
</source>
And the class, where code fragments are supposed to be, can look like this:
<sourcesyntaxhighlight lang="java">
public class AnAssemblyWriter extends AssemblerProgram
{
Line 79:
... // your code fragment methods
}
</syntaxhighlight>
</source>
Here we see the class's constructor with two parameters, the mode parameter can be one of the following:
CODE16 // Generate 16-bit code
Line 88:
 
Next you can add a particular code to the bootloader for it to be able to actually load something. You can write a method like this:
<sourcesyntaxhighlight lang="java">
protected void loadImageUsingInt13x42(String problemLabel, String diskAddressPacketLabel, int imageSizeInDiskSectors)
{
Line 120:
}
}
</syntaxhighlight>
</source>
The the final program (it's main method) can look like this:
<sourcesyntaxhighlight lang="java">
private void createBootCode(int imageStartAbsoluteBlockNumber, int imageSizeInDiskSectors,
short memoryAddressToPlaceBootstrapImageAt, short bootLoaderNewAddress,
Line 141:
imageSizeInDiskSectors,imageStartAbsoluteBlockNumber,memoryAddressToPlaceBootstrapImageAt);
}
</syntaxhighlight>
</source>
And here you can see how helper procedures and data structures are written together with the actual bootloader's code.
<sourcesyntaxhighlight lang="java">
private void writeProceduresAndData(String diskReadProblemLabel, String diskAddressPacketLabel,
int imageSizeInDiskSectors, int imageStartAbsoluteBlockNumber, short memoryAddressToPlaceBootstrapImageAt)
Line 175:
writeBytes(bs); // now add the bs buffer to the actual program
}
</syntaxhighlight>
</source>
After you have all inline assembly instructions in place you can first call the methods with the instructions for them to write their binary representation and next call AssemblerProgram's getProgram() method to get the actual machine code stored in the returned byte array. The resulting bytes can be written to the boot sector using appropriate tools.
 
Line 185:
 
[[Category:Bare bones tutorials]]
[[Category:Java]]