Atom Pro Pointers

As in variable pointers not as in “tips”.

The Atom Pro compiler supports a new variable type called “pointer”. here is an example program that uses one.

[code]temp var pointer
temp2 var long

main
temp = @TEMP2 ;temp = the address of temp2
@temp = 0x1234 ;set the value of the memory location temp points at to 0x1234
serout s_out,i9600,"temp2 set by ptr = ",hex temp2,13]
pause 100
temp2 = 0
serout s_out,i9600,"temp2 set directly = ",hex temp2,13]
pause 100
goto main[/code]

The two new items are the “pointer” variable type and the “@” modifier. The @ modifer tells the compiler to use the value of the variable as the address of the target/source. The address consists of the byte address and type information. The low 16bits are the byte address. Type information is stored in the high 16bits. Pointers are equivilent to long variables but with a type flag that indicates they can be used as a pointer.

So you could use a pointer type variable in an argument of a subroutine and pass along the address of the variable you really want to access like so:

[code]temp1 var long
temp2 var long

temp1 = 0x1234
temp2 = 0x4321
main
gosub test@temp1]
gosub test@temp2]
goto main

arg var pointer
test [arg]
serout s_out,i9600,"Value is ",hex @arg,13]
pause 100
return[/code]

In this example I am passing the address of temp1 or temp2 to the subroutine and displaying the value stored at the address.

Note that the variable type that the pointer points at doesn’t matter. Here is a slightly modified version of the above sample showing me sending a long and then a word sized variable address to the subroutine.

[code]temp1 var long
temp2 var word

temp1 = 0x12345678
temp2 = 0x87654321
main
gosub test@temp1]
gosub test@temp2]
goto main

arg var pointer
test [arg]
serout s_out,i9600,"Value is ",hex @arg,13]
pause 100
return[/code]

Pointers can also be used to return values from a subroutine without using a value in the return statement or in addition to the value in the return.

[code]temp1 var long

main
temp1 = 0x12345678
serout s_out,i9600,"Value is ",hex temp1,13]
pause 100
gosub test@temp1]
serout s_out,i9600,"Value is ",hex temp1,13]
pause 100
goto main

arg var pointer
test [arg]
@arg = 0
return[/code]

Thanks Nathan,

I will have to experiment with these.

So if I understand correctly the use of an @ is depending on what it is in front of. If it is in front of a normal variable then it means to give me the address of that variable, whereas if it is in front of a pointer variable it implies give me the value that is contained at the address whose value is stored in the pointer variable. Are pointer variables 32 bits?

From your examples:
temp = @TEMP2

would translate into
mov.l #TEMP2, er0
mov.l er0, @TEMP

And likewise
temp = @TEMP2

would translater into:
mov.l @TEMP2, er0
mov.l @er0, er1
mov.l er1, @TEMP

Is this correct?

If so I assume I could set or query the values in assembly language.

Also does this work with arrays or byte tables and the like? Ie can I do something like:
MyPointer = @MyArray

and then get to elements of the array either like:
@MyPointer(5)

Or @(MyPointer+5)

My guess not, but maybe fun to try…

Thanks again.

Kurt

  1. Pointer variables are 32bits. They can be used just like a long when not using them with the @ argument. However longs can not be used as pointer variables. See 2 below.

  2. With normal variables the @ means get the address of the variable. With pointer variables the @ means access the value pointed at by the pointer(to get or set that value).

  3. Yes you can use the @ with arrays and tables. (temp = @array[10])

  4. Yes you can set and query the values in asm.

  5. You can increment and decrement pointers like MyPointer=MyPointer+5. temp = @MyPointer. I’m pretty sure though, you can’t do @(MyPointer+5) because the pointer ness of MyPointer will be destroyed by the +5 as far as the @ is concerned in this expression.

I am working on updating my Basic device library to use the new pointers.
What am I doing wrong in the following example?

Main
    sTestData var byte (10)

    ' this runs fine and the data gets set as expected
    gosub PointerTestWorks @sTestData]

    ' this causes a reset
    gosub PointerTestFails @sTestData]
    End

_pbPointerTestWorksData var pointer
PointerTestWorks [_pbPointerTestWorksData]

    ' this works fine
    @_pbPointerTestWorksData = 1
    _pbPointerTestWorksData = (_pbPointerTestWorksData+1)
    @_pbPointerTestWorksData = 2
    return

_pbPointerTestFailsData var pointer
PointerTestFails [_pbPointerTestFailsData]

    ' this causes a reset
    @_pbPointerTestFailsData (0) = 1
    @_pbPointerTestFailsData (1) = 2
    return

-Bob

Something for Nathan…

Early on I did not have much luck with trying to use arrays with the pointers. So I would typically do more like your first method or I would do some array calculations in assembly…

Kurt

Please send me an email about this. I’l try to look at it as soon as I can.