Most PIC Languages support some way of Indirect Addressing, what is Indirect Addressing you may ask!?. A simple form of accessing a PIC Register or storage location in a more efficient way and very useful if you need to access alot of data in this case an Array.
Firewing for an example supports this in more of an easier way to understand that I can rememeber on how to do this with Assembly language..
David Barker quick reference guide:
Byte Access (addr0, addr1 or baddr0, baddr1)
Code: Select all
addr = AddressOf(var) ' put address of var into addr
var = *(addr) ' get byte value at address
var = *(-addr) ' decrement address by one byte, then get byte value
var = *(+addr) ' increment address by one byte, then get byte value
var = *(addr-) ' get byte value, then decrement address by one byte
var = *(addr+) ' get byte value, then increment address by one byte
Code: Select all
waddr = AddressOf(var) ' put address of var into waddr
var = *(waddr) ' get word value at address
var = *(-waddr) ' decrement address by two bytes, then get word value
var = *(+waddr) ' increment address by two bytes, then get word value
var = *(waddr-) ' get word value, then decrement address by two bytes
var = *(waddr+) ' get word value, then increment address by two bytes
This first example uses a For Loop to Copy the Array data to another Array
Code: Select all
dim myArray(5) as byte = {1,2,3,4,5}
dim myNewArray(5) as ushort = {0,0,0,0,0}
Sub Main()
for i as byte = 0 to 4
myNewArray(i) = myArray(i)
next
for i as byte = 0 to 4
Console.Write("myNewArray = ",str(myNewArray(i)),13,10)
next
End Sub
The indirect addressing command *(addr1+) and *(addr0+) will automatically increase the register, see the quick reference guide above.
Code: Select all
dim count as byte = 0
dim myArray(5) as byte = {1,2,3,4,5}
dim myNewArray(5) as ushort = {0,0,0,0,0}
Sub Main()
addr0 = addressof(myArray)
addr1 = addressof(myNewArray)
Do
*(addr1+) = *(addr0+)
count += 1
loop until count = 5
for i as byte = 0 to 4
Console.Write("myNewArray = ",str(myNewArray(i)),13,10)
next
End Sub
- VERY IMPORTANT BIT TO REMEMBER WHEN USING INDIRECT ADDRESSING -
When using any indirect addressing command you need to remember that Firewing also uses these commands within Subs/Functions within the Compiler, and this will currupt the indirect addressing location you are trying to point too. Always check the Subs/Functions that you are calling within the Compiler for an example if you set a register address to command addr0 and then make a sub call to the compiler Console.Write() Sub then the addr0 will be changed as the Console.Write() sub uses the addr0 command.