FreeBasic Bare Bones: Difference between revisions

Jump to navigation Jump to search
m
added source tags
[unchecked revision][unchecked revision]
(Rating down to intermediate)
m (added source tags)
Line 19:
 
In FreeBasic, a pointer is defined by adding the Ptr keyword.
<source lang="freebasic">
Dim mypointer As Byte Ptr
</source>
This one holds the location of a byte of memory.
<source lang="freebasic">
Dim mypointer As Long Ptr
</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.
<source lang="freebasic">
Dim variable as Byte
Dim pointer as Byte Ptr
pointer = @variable
</source>
The @ returns the address of the variable that follows it.
<source lang="freebasic">
Dim pointer as Byte Ptr
Dim address as Long
address = 12345
pointer = CPtr(Byte Ptr, address)
</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:
<source lang="freebasic">
Dim pointer1 as Byte Ptr
Dim pointer2 as Long Ptr
pointer2 = CPtr(Long Ptr, pointer1)
</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:
<source lang="freebasic">
Dim value as Long
Dim pointer as Long Pointer
pointer = @value
' value = pointer[0]
</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:
<source lang="freebasic">
Dim s as String
Dim pointer as String Ptr
pointer = @s
</source>
But since we do not have the Runtime, we have to use something that does not use it.
<source lang="freebasic">
Const s = "Text"
Dim pointer as Byte Ptr
Line 60 ⟶ 75:
' pointer[3] = Asc("t")
' pointer[4] = 0
</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.
 
<source lang="freebasic">
Sub PrintString(src As Byte Ptr, x As Long, y As Long)
Line 79 ⟶ 96:
wend
End Sub
</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 addres B8000 hex), and we pick a location in there. Next we take a character from the string, check if its 0, and if it isnt, 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 94 ⟶ 112:
 
=== kernel.bas ===
<source lang="freebasic">
<pre>
Declare Sub PrintString(src As Byte Ptr, x As Long, y As Long)
Declare Sub main ()
Line 154 ⟶ 172:
wend
End Sub
</presource>
 
=== link.ld ===
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu