FreeBasic Bare Bones: Difference between revisions

m
no edit summary
[unchecked revision][unchecked revision]
m (User:Combuster/FreeBasic Barebones moved to FreeBasic Barebones: Move into main namespace)
mNo edit summary
 
(15 intermediate revisions by 10 users not shown)
Line 1:
{{BeginnersWarning}}
{{Rating|2}}
While the forum has several flamewars about BASIC, it is a turing-complete language. FreeBasic adds functionality that makes it suitable for OS development. This tutorial provides a working kernel in Basic, together with the pitfalls associated with it.
 
Line 18 ⟶ 20:
 
In FreeBasic, a pointer is defined by adding the Ptr keyword.
<syntaxhighlight lang="qbasic">
Dim mypointer As Byte Ptr
</syntaxhighlight>
This one holds the location of a byte of memory.
<syntaxhighlight lang="qbasic">
Dim mypointer As Long Ptr
</syntaxhighlight>
This one points to the first of 4 bytes of memory, which together form the number.
 
You can not use pointers straight away. Consider sending a letter with no address on it will not do any good. So we must first put an address in there. That leads to the question: how do we get an address. Some things have fixed addresses, like the video card.
You can also ask variables for their addresses.
<syntaxhighlight lang="qbasic">
Dim variable as Byte
Dim pointer as Byte Ptr
pointer = @variable
</syntaxhighlight>
The @ returns the address of the variable that follows it.
<syntaxhighlight lang="qbasic">
Dim pointer as Byte Ptr
Dim address as Long
address = 12345
pointer = CPtr(Byte Ptr, address)
</syntaxhighlight>
Here CPtr (Convert to Pointer) is used to create pointers. You give it a type (Byte Ptr) and the address. You can also change pointers:
<syntaxhighlight lang="qbasic">
Dim pointer1 as Byte Ptr
Dim pointer2 as Long Ptr
pointer2 = CPtr(Long Ptr, pointer1)
</syntaxhighlight>
Be careful when you do so: a byte occupies one location in memory, a long occupies four. if we would use this pointer, we would use three locations in memory of which we know nothing about. Sometimes, they are necessary, like when we want to work with strings without having the Runtime.
 
You can access pointers with an index:
<syntaxhighlight lang="qbasic">
Dim value as Long
Dim pointer as Long Pointer
pointer = @value
' value = pointer[0]
</syntaxhighlight>
when we want to know what is behind the pointer, we ask for the memory at its location with the [ ], and then we add a number which tells us how many locations further to look. When we use [0] it simply means that we do not want to do anything with the address stored in the pointer. The result is the value stored in the memory locations pointed to by the pointer. For byte pointers, this will look at the address stored and return the result. For other pointers, it will look at a series of locations and return what those values represent.
 
Strings are useful, but tedious. A string is stored as a series of bytes in consecutive addresses. You can build a pointer to a string as well:
<syntaxhighlight lang="qbasic">
Dim s as String
Dim pointer as String Ptr
pointer = @s
</syntaxhighlight>
But since we do not have the Runtime, we have to use something that does not use it.
<syntaxhighlight lang="qbasic">
Const s = "Text"
Dim pointer as Byte Ptr
Line 59 ⟶ 76:
' pointer[3] = Asc("t")
' pointer[4] = 0
</syntaxhighlight>
The first example uses the Runtime which will not compile. The second example shows the only working method of using strings: Define one using Const, then ask for a pointer to that. Since a string is a sequence of characters (bytes), we change the type of the pointer in the process. Next we can ask for individual characters as if it were an array. However, they now appear as numbers. The ASCII codes to be precise, and the same as when you use Chr$() and Asc().
 
Basic uses an borrowed trick to tell us the end of the string. After the last character, there will always be a 0. So if we read a string in order, we can tell when it has ended.
 
<syntaxhighlight lang="qbasic">
Sub PrintString(src As Byte Ptr, x As Long, y As Long)
Line 78 ⟶ 97:
wend
End Sub
</syntaxhighlight>
 
To conclude, this function prints a string (a converted Byte Ptr string). It creates a pointer that is aimed at the [[Text UI|video card]] (it occupies among others a range starting from addresaddress B8000 hex), and we pick a location in there. Next we take a character from the string, check if its 0, and if it isntisn't, copy it to the video card and go to the next character. Due to the way the video card works, we add a color (15) as well.
 
== Tuning FreeBasic for OS Development ==
Line 93 ⟶ 113:
 
=== kernel.bas ===
<!-- GeSHi is *broken*. don't use freebasic as language here !-->
<pre>
<syntaxhighlight lang="qbasic">
Declare Sub PrintString(src As Byte Ptr, x As Long, y As Long)
Declare Sub main ()
Line 153 ⟶ 174:
wend
End Sub
</syntaxhighlight>
</pre>
 
=== link.ld ===
<syntaxhighlight lang="text">
<pre>
OUTPUT_FORMAT("elf32-i386")
ENTRY (loader)
Line 189 ⟶ 210:
}
}
</syntaxhighlight>
</pre>
 
=== Build instructions ===
<syntaxhighlight lang="bash">
<pre>fbc -c -o kernel.o kernel.bas
i586-elf-ldfbc -Tc linkkernel.ldbas -o kernel.bin kernel.o</pre>
<pre>fbci586-elf-ld -cT link.ld -o kernel.obin kernel.baso
</syntaxhighlight>
 
<tt>kernel.bin</tt> can then be loaded by [[GRUB]]
Line 202 ⟶ 225:
The Runtime is built on top of the C library. FreeBasic provides sources to the Runtime (which are written in C). You'll need to provide the C library. Since many parts of the kernel rely heavily upon each other, you will probably add the Runtime in steps.
Some functions compile cleanly with minimal effort. When you compile the CType, String and Stdlib (excluding malloc, calloc, and free) parts of the runtime can be compiled on top of those.
Before you can use the string and array instructions, you will need to have [[Memory Management|memory management]] implemented. That will allow you to compile malloc, calloc, free from the C library, after which the string and some of the array functions can be compiled. (Newlib can be [[porting Newlib|compiled completely]] when it has memory management functionality)
 
[[Category:Bare bones tutorials]]