Address of a variable: Difference between revisions
Content added Content deleted
(Added solution for Action!) |
Not a robot (talk | contribs) (Add Draco) |
||
Line 758: | Line 758: | ||
strLen: byte absolute str; |
strLen: byte absolute str; |
||
</lang> |
</lang> |
||
=={{header|Draco}}== |
|||
<lang draco>/* This code uses a CP/M-specific address to demonstrate fixed locations, |
|||
* so it will very likely only work under CP/M */ |
|||
proc nonrec main() void: |
|||
/* When declaring a variable, you can let the compiler choose an address */ |
|||
word var; |
|||
/* Or you can set the address manually using @, to a fixed address */ |
|||
word memtop @ 0x6; /* CP/M stores the top of memory at 0x0006 */ |
|||
/* or to the address of another variable, by naming that variable */ |
|||
word var2 @ var; /* var2 will overlap var */ |
|||
/* This works with both automatically and manually placed variables. */ |
|||
word memtop2 @ memtop; /* same as "memtop2 @ 0x6" */ |
|||
/* Once a variable is declared, you can't change its address at runtime. */ |
|||
var := 1234; /* assign a value to var _and_ var2 */ |
|||
/* The address of a variable can be retrieved using the & operator. |
|||
* However, this returns a pointer type, which is distinct from an |
|||
* integer type. To use it as a number, we have to coerce it to an integer |
|||
* first. */ |
|||
writeln("var address=", pretend(&var,word):5, " value=", var:5); |
|||
writeln("memtop address=", pretend(&memtop,word):5, " value=", memtop:5); |
|||
writeln("var2 address=", pretend(&var2,word):5, " value=", var2:5); |
|||
writeln("memtop2 address=", pretend(&memtop2,word):5, " value=", memtop2:5) |
|||
corp</lang> |
|||
{{out}} |
|||
<pre>var address= 276 value= 1234 |
|||
memtop address= 6 value=58374 |
|||
var2 address= 276 value= 1234 |
|||
memtop2 address= 6 value=58374</pre> |
|||
=={{header|ERRE}}== |
=={{header|ERRE}}== |