1 Variable assignment and addressing explained
leanto edited this page 2018-11-12 23:11:16 +01:00

Addressing modes explained

PLASMA has some interesting addressing modes that aren't entirely obvious at first.

The following examples assume this setup:

include "inc/cmdsys.plh"

var x
word y
byte a
byte b

def phvar(str, value)
  puts(str)
  puth(value)
  putln()
end

Simple assignment

We can do simple assignment like this:

x = 5
phvar("x = 5 = ", x) // x = 5 = $0005
y = $FF88
phvar("y = $FF88 = ", y) // y = $FF88 = $FF88

Note that x is a word size, too, even though we don't see it here.

Here are simple assignments to byte-sized variables:

a = 66
phvar("a = 66 = ", a) // a = 66 = $0042
b = 307
phvar("b = 307 = ", b) // b = 307 = $0033

Note that b overflows with no error.

Pointers

Using @ we can get the address of a variable:

y = @x
phvar("y = @x = ", y) // y = @x = $200

Using * we can get the value at an address contained by a variable. For example:

phvar("*y = ", *y) // y = $0005
x = 10
phvar("x = 10; *y = ", *y) // y = $000A

Because we changed the value of x, the value of *y changed as well.

Assignment can work the same way:

*y = $88FF
phvar("*y = $88FF; x = ", x) // *y = $88FF; x = $88FF

Because we changed *y, we changed the value of x.

The same is true of byte-sized variables, but we use ^ instead of *.

x = @a
phvar("x = @a = ", x) // x = @a = $0206
y = @b
phvar("y = @b = ", y) // y = @b = $0207

^x = $8F
phvar("^x = $8F; a = ", a) // ^x = $8F; a = $008F
^y = 555
phvar("^y = 555; b = ", b) // ^y = 555; b = $002B

But what if we address them wrong? What if we use * instead of ^?

*x = $7799
phvar("*x = $7799 = ", *x) // *x = $7799
phvar("a = ", a) // a = $0099
phvar("b = ", b) // b = $0077

This allows us to set both a and b at the same time.

What if we want to do that without using an intermediate variable? Can we use *@a?

phvar("*@a = ", *@a) // *@a = $0099
phvar("*(@a) = ", *(@a)) // *(@a) = $7799