FreeBasic Bare Bones: Difference between revisions

m
no edit summary
[unchecked revision][unchecked revision]
m (Fixed mistake: a long does not hold 4 bytes. It holds 8 bytes.)
mNo edit summary
 
(5 intermediate revisions by 4 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 14 ⟶ 15:
Pointers can be difficult to understand. Many modern languages do not work with pointers, while if you know a language like C, C++ or Assembly, you will probably know what they do. Since you will need to use them, a quick introduction in how they work in FreeBasic.
 
The computer uses a set of registers, and memory. Memory is divided into bytes, each byte has its own location number. Big numbers, strings, types and arrays use up multiple bytes. These bytes are stored next to each other. A Long will hold 84 bytes, and will for example occupy locations 239394, 239395, 239396, and 239397, 239398, 239399, 239400, 239401. In the computer, these location numbers are called addresses (like the address when sending letters)
 
Pointers hold these location numbers. For bytes, they hold the exact address, for larger objects, it will hold the address of the lowest address (you can determine the other addresses as they will immediately follow the first)
 
In FreeBasic, a pointer is defined by adding the Ptr keyword.
<sourcesyntaxhighlight lang="freebasicqbasic">
Dim mypointer As Byte Ptr
</syntaxhighlight>
</source>
This one holds the location of a byte of memory.
<sourcesyntaxhighlight lang="freebasicqbasic">
Dim mypointer As Long Ptr
</syntaxhighlight>
</source>
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.
<sourcesyntaxhighlight lang="freebasicqbasic">
Dim variable as Byte
Dim pointer as Byte Ptr
pointer = @variable
</syntaxhighlight>
</source>
The @ returns the address of the variable that follows it.
<sourcesyntaxhighlight lang="freebasicqbasic">
Dim pointer as Byte Ptr
Dim address as Long
address = 12345
pointer = CPtr(Byte Ptr, address)
</syntaxhighlight>
</source>
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:
<sourcesyntaxhighlight lang="freebasicqbasic">
Dim pointer1 as Byte Ptr
Dim pointer2 as Long Ptr
pointer2 = CPtr(Long Ptr, pointer1)
</syntaxhighlight>
</source>
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:
<sourcesyntaxhighlight lang="freebasicqbasic">
Dim value as Long
Dim pointer as Long Pointer
pointer = @value
' value = pointer[0]
</syntaxhighlight>
</source>
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:
<sourcesyntaxhighlight lang="freebasicqbasic">
Dim s as String
Dim pointer as String Ptr
pointer = @s
</syntaxhighlight>
</source>
But since we do not have the Runtime, we have to use something that does not use it.
<sourcesyntaxhighlight lang="freebasicqbasic">
Const s = "Text"
Dim pointer as Byte Ptr
Line 75 ⟶ 76:
' pointer[3] = Asc("t")
' pointer[4] = 0
</syntaxhighlight>
</source>
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.
 
<sourcesyntaxhighlight lang="freebasicqbasic">
Sub PrintString(src As Byte Ptr, x As Long, y As Long)
Line 96 ⟶ 97:
wend
End Sub
</syntaxhighlight>
</source>
 
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 address B8000 hex), and we pick a location in there. Next we take a character from the string, check if its 0, and if it isn'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.
Line 113 ⟶ 114:
=== kernel.bas ===
<!-- GeSHi is *broken*. don't use freebasic as language here !-->
<sourcesyntaxhighlight lang="qbasic">
Declare Sub PrintString(src As Byte Ptr, x As Long, y As Long)
Declare Sub main ()
Line 173 ⟶ 174:
wend
End Sub
</syntaxhighlight>
</source>
 
=== link.ld ===
<syntaxhighlight lang="text">
<pre>
OUTPUT_FORMAT("elf32-i386")
ENTRY (loader)
Line 209 ⟶ 210:
}
}
</syntaxhighlight>
</pre>
 
=== Build instructions ===
<syntaxhighlight lang="bash">
<pre>fbc -c kernel.bas -o kernel.o
i586-elf-ldfbc -Tc linkkernel.ldbas -o kernel.bin kernel.o</pre>
i586-elf-ld -T link.ld -o kernel.bin kernel.o
</syntaxhighlight>
 
<tt>kernel.bin</tt> can then be loaded by [[GRUB]]