Using a Pointer to access large array

General discussion on mikroBasic for dsPIC30/33 and PIC24.
Post Reply
Author
Message
Toley
Posts: 922
Joined: 03 Sep 2008 16:17

Using a Pointer to access large array

#1 Post by Toley » 10 Sep 2008 11:58

I'm trying to acces a large array (more than 3000 words) with a pointer. In fact, it's a C program that I try to adapt in Basic. So if there is another option other than using pointer I can also use it.

I've made this simple little program using a pointer to simulate and see what happen, but it don't work like I think it should be. There is surely something I don't understand with pointer, and I didn't found any example.

Code: Select all

program Pointer

Dim  MyArray   As Word[5]   ' Simple 5 elements array for testing
Dim  MyPointer As ^Word     ' Pointer declaration
Dim  MyData    As Word      ' Used to store the array elements
Dim  MyCount   As Word      ' Loop counter

MyArray[0] = $0001          ' Initial known content of the array
MyArray[1] = $0010
MyArray[2] = $0100
MyArray[3] = $1000
MyArray[4] = $1111

MyPointer = @MyArray        ' Pointer hold the address of the array

For MyCount = 0 To 4        ' Repeat 5 times
    MyData = MyPointer^     ' Content of pointer in MyData
    Inc (MyPointer)         ' Increment pointer value
Next MyCount

End.
Can someone tell me what I am doing wrong or another efficient way to do this ?

Thank you.

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#2 Post by yo2lio » 10 Sep 2008 13:22

Hello,

Because pointer is declared word, you must increment pointer location with 2 units.

Code: Select all

program Pointer

Dim  MyArray   As Word[5]   ' Simple 5 elements array for testing
Dim  MyPointer As ^Word     ' Pointer declaration
Dim  MyData    As Word      ' Used to store the array elements
Dim  MyCount   As Word      ' Loop counter

MyArray[0] = $0001          ' Initial known content of the array
MyArray[1] = $0010
MyArray[2] = $0100
MyArray[3] = $1000
MyArray[4] = $1111

MyPointer = @MyArray        ' Pointer hold the address of the array

For MyCount = 0 To 4        ' Repeat 5 times
    MyData = MyPointer^     ' Content of pointer in MyData
    MyPointer = MyPointer + 2         ' Increment pointer value with 2 units
Next MyCount

End.
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

extrapilot
Posts: 130
Joined: 28 Nov 2006 17:59

#3 Post by extrapilot » 12 Sep 2008 02:20

Hi Florin

Can assignments be made such that you can poke a word into a memory address by pointer reference?

Example,
Dim localArrayPointer as ^word
localArrayPointer=0x1800
localArrayPointer^ = 0x0001

When I try this, in ICD, I do not see the value of the word at 0x1800 change to 0x0001- or change at all for that matter.

Regards,
Rob

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#4 Post by yo2lio » 12 Sep 2008 06:42

HI,

Have your tested MCU this RAM address : 0x1800 ???

At me work at DSPIC33FJ64GP706 !

Code: Select all

Dim  MyPointer As ^Word  ' my pointer  
Dim  MyData  As Word absolute 0x1800  ' to see result in debugger

main:
MyPointer = 0x1800
MyPointer^ = 0x0001
end.
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

extrapilot
Posts: 130
Joined: 28 Nov 2006 17:59

#5 Post by extrapilot » 12 Sep 2008 07:21

Hi Florin

Yes, I am running a 24HJ128GP506. I have been able to do this allocation in ASM, so, I know that the addressing is OK. It would be nicer to be able to do this via a higher-level language; I worry that the compiler will modify some of my working registers if I have the ASM in a loop controlled by the compiler.

Code: Select all

       localArrayPointer=RegisterAddressAssigment[localRegisterID]        
       W6 = localArrayPointer 
       W7 = (localCellCount-1) ' from previous op that determines array depth
       asm             
         MOV #0, W8
         REPEAT W7     
         MOV W8, [W6++]
       end asm[code]
[/code]

extrapilot
Posts: 130
Joined: 28 Nov 2006 17:59

#6 Post by extrapilot » 12 Sep 2008 07:28

btw- I had to define the memory addresses in ram. Apparently, there is still some problem with accessing constant arrays- or at least I cannot figure out how to do something like:

Code: Select all

RegisterAddressAssigment as word[RegisterCount] = (0x1800, 0x1824, 0x1900, 0x1982)
When I try to access this via a loop- i.e.

Code: Select all

W6 = RegisterAddressAssigment[localRegisterID]
, I get strange returns- 0x1800 always returns as item[0], but item [1] might return 0x1982, or some other random address- but never 0x1824.

So I just allocated this in ram as:

Code: Select all

Dim RegisterAddressAssigment as word[RegisterCount]
 RegisterAddressAssigment[0]= 0x1800
 RegisterAddressAssigment[1]= 0x1824
 RegisterAddressAssigment[2]= 0x1900
 RegisterAddressAssigment[3]= 0x1982
and that works fine. Strange.

Regards,
R

Post Reply

Return to “mikroBasic for dsPIC30/33 and PIC24 General”