What does "GREG @" in this code do? I know it reserves a global register, but why do we need it here?
LOC Data_Segment
GREG @
Y OCTA 0
x IS $1
buf1 IS $2
buf2 IS $3
LOC #100
Main FLOT buf1,157
FLOT buf2,50
FDIV x,buf1,buf2
STO x,Y
TRAP 0,Halt,0
CodePudding user response:
The MMIX instruction set does not have an absolute addressing mode.
Instead, as is typical for RISC architectures, the only available addressing modes are a indexed addressing modes with a base register and either an 8 bit immediate or a register index. Hence, loading variables from memory requires you to first load a nearby address into a register. This is achieved with the GREG @
directive: it allocates a global register with the current address, permitting access to nearby global variables (in this case, that is the variable Y
).
In more complex programs, you might probably want to chose a different approach as you'll run out of global registers quickly. One solution is to store a pool of addresses next to your code and load the address of that pool with a GETA
instruction like this:
...
GETA $4, pool @ obtain the address of the pool
LDOU $5, $4, 0 @ load the address of Y from the pool
STO x, $5, 0 @ store x to Y
...
pool OCTA Y @ literal pool holding Y