
Basic Data Operation

This is a basic data operation. It represents a fundamental action on a basic data type.
You may see other such operations in the Basic Data Operations category, or:


Integer Operations
Arithmetic |
Comparison
Boolean Operations
Bitwise |
Logical
String Operations
Concatenation |
Interpolation |
Comparison |
Matching
Memory Operations
Pointers & references |
Addresses




Task

Demonstrate how to get the address of a variable and how to set the address of a variable.




/* 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