By MAK3
This Application Note will show how to use and understand the OVERLAY functionality in BASCOM-AVR.
See also: and
All the explanation is already in the following example except the Bascom-AVR Simulator Memory output which really help to understand how OVERLAY works with Bascom-AVR.
You can find also an example to examine the Null termination at the end of strings together with OVERLAY.
Here you find the Bascom-AVR Simulator Memory status when you run the following example in Bascom-AVR simulator:
The Terminal Output of following example is: ------------------------- T e ------------------------- Hello ------------------------- My_word = 1111000000001111 ------------------------- 00000100000000110000001000000001 ------------------------- my_dword = 00000100000000110000001000000001 ------------------------- 10000000000000111111111100000000 ------------------------- Day= 16 Month= 11 Year= 2011 16/11
' by MAK3
' Various Overlay Examples
' Overlay: take care with that, while powerful, it can also lead to problems when you point to the wrong variable
$regfile = "m644pdef.dat"
$crystal = 4000000
$hwstack = 60
$swstack = 60
$framesize = 60 'frame space can grow rapid when using it on variables with a big size (strings)
$baud = 9600
$sim '$sim to use this example in Bascom-AVR simulator
Print "-------------------------"
Dim Array(5) As Byte
Dim My_string As String * 4 At Array Overlay
Dim K As Byte
K = 1
My_string = "Test"
'---> 4 ASCII but 5 Bytes because of 0 Termination of String which is another byte
'This is how it will be stored in SRAM
' Array(1) Array(2) Array(3) Array(4) Array(5)
' +--------+--------+--------+--------+--------+
' | T | e | s | t | 00 |
' +--------+--------+--------+--------+--------+
Print Chr(array(1))
Print Chr(array(2))
Print "-------------------------"
Dim Teststring As String * 5
Dim Ar(6) As Byte At Teststring Overlay
Dim J As Byte
J = &H03
Ar(5) = 47
Teststring = "Hello"
' ---> 5 ASCII but 6 Bytes because of 0 Termination of String
'This is how it will be stored in SRAM
' Ar(1) Ar(2) Ar(3) Ar(4) Ar(5) Ar(6)
' +--------+--------+--------+--------+--------+--------+
' | H | e | l | l | o | 00 |
' +--------+--------+--------+--------+--------+--------+
For K = 1 To 5
Print Chr(ar(k)) ;
Next
Print
K = 1
Print "-------------------------"
Dim My_word As Word
Dim Low_byte As Byte At My_word Overlay
Dim High_byte As Byte At My_word + 1 Overlay
Low_byte = &B0000_1111
High_byte = &B1111_0000
'This is how it will be stored in SRAM
' <-------my_word-------->
' +-----------+----------+
' | Low_byte |High_byte |
' +-----------+----------+
'But when you print it with print bin(Variable) you will see it as
' <-------my_word-------->
' 11110000 00001111
' +-----------+----------+
' | High_byte |Low_byte |
' +-----------+----------+
Print "My_word = " ; Bin(my_word)
Print "-------------------------"
Dim My_long_1 As Long
Dim Byte_1 As Byte At My_long_1 Overlay
Dim Byte_2 As Byte At My_long_1 + 1 Overlay
Dim Byte_3 As Byte At My_long_1 + 2 Overlay
Dim Byte_4 As Byte At My_long_1 + 3 Overlay
Byte_1 = 1
Byte_2 = 2
Byte_3 = 3
Byte_4 = 4
Print Bin(my_long_1)
'This is how it will be stored in SRAM
' <-------my_long_1------------>
' +-------+------+------+------+
' | Byte_1|Byte_2|Byte_3|Byte_4|
' +-------+------+------+------+
'But when you print it with print bin(Variable) you will see it as
' <-------my_long_1------------>
' +-------+------+------+------+
' | Byte_4|Byte_3|Byte_2|Byte_1|
' +-------+------+------+------+
Print "-------------------------"
Dim My_dword As Dword At $140 'This places the my_long_2 variable at a fixed SRAM address starting at HEX 140
Dim Byte__1 As Byte At $140 Overlay 'NOTICE: because this will be stored at the specified memory location
Dim Byte__2 As Byte At $141 Overlay ' which could be already be occupied by another OVERLAY variable, or by a normal variable
Dim Byte__3 As Byte At $142 Overlay
Dim Byte__4 As Byte At $143 Overlay
Byte__1 = 1
Byte__2 = 2
Byte__3 = 3
Byte__4 = 4
'This is how it will be stored in SRAM
' <----------my_dword---------->
' +-------+------+------+------+
' | Byte_1|Byte_2|Byte_3|Byte_4|
' +-------+------+------+------+
'But when you print it with print bin(Variable) you will see it as
' <----------my_dword---------->
' +-------+------+------+------+
' | Byte_4|Byte_3|Byte_2|Byte_1|
' +-------+------+------+------+
Print "my_dword = " ; Bin(my_dword)
Print "-------------------------"
Dim My_dword_2 As Dword
Dim My_word_2 As Word At My_dword_2 Overlay
Dim My_byte3 As Byte At My_dword_2 + 2 Overlay
Dim My_byte4 As Byte At My_dword_2 + 3 Overlay
My_word_2 = &B11111111_00000000
My_byte3 = &B00000011
My_byte4 = &B10000000
'This is how it will be stored in SRAM
' <--------------my_dword_2------------>
' +---------+--------+--------+--------+
' | my_word_2 |my_byte4|my_byte3|
' +---------+--------+--------+--------+
'But when you print it with print bin(Variable) you will see it as
' <--------------my_dword_2------------>
' +---------+--------+--------+--------+
' | my_byte4|my_byte3| my_word_2 |
' +---------+--------+--------+--------+
Print Bin(my_dword_2)
Print "-------------------------"
' Now we examine the Null terminator in Strings
Dim My_date(11) As Byte '8 strings + 3 Null terminator = 11 Byte
Dim Day As String * 2 At My_date(1) Overlay
Dim Null_terminator As Byte At My_date(1) + 2 Overlay 'Null terminator
Dim Month As String * 2 At My_date(1) + 3 Overlay
Dim Null_terminator_2 As Byte At My_date(1) + 5 Overlay 'Null terminator
Dim Year As String * 4 At My_date(1) + 6 Overlay
Dim Null_terminator_3 As Byte At My_date(1) + 11 Overlay 'Null terminator
Day = "16"
Month = "11"
Year = "2011"
Print "Day= " ; Day
Print "Month= " ; Month
Print "Year= " ; Year
'For example the print function use the Null Terminator to check the end of the string
'When we set now the Null_terminator to "/" (forward slash) instead of 0 then the print function print until a Null terminator is recognised
Null_terminator = 47 '47 = "/" (forward slash) '
Print Day 'This will now print "16/11" because the first Null terminator will be found after the "11"
End 'end program
|