Location of arrays for the loadw opcode

Hello,

I am sure that this is well defined since there are a number of Z-machine interpreters but I can’t figure this out from the specifications.

With the loadw opcode, what is the base address that I use for the first argument?

thanks,
Steve

I believe it’s just the location in memory that you want to read from. The reason why it takes two arguments instead of one is to make it easier to iterate over arrays, since the Z-machine doesn’t have fancy indexed addressing modes. (Likewise loadb, storew, storeb.)

For loadw, the address to be read is just operand_1 + 2 * operand_2.

This is only legal / defined as long as the resulting address lies within dynamic or static memory (less than or equal to 0xFFFF). I suspect some interpreters will read beyond this point, but the standard is clear on the matter.

1 Like

Hi Mike and Daniel,

Thanks for indulging me in this topic. I’ve gone through the specification and it says:

loadw array work-index → (result)

I’ve looked through the yzip specification and it explains a little bit more stating:

GET table item > VAL

Reading a bit more and array is a group of elements or items (as is table in the yzip spec) which does correspond to the idea of an array in programming languages. In the dumped Zork 1 file, I have the assembly commands as follows:

@563b 4f 04 02 00

This version of the loadw command suggests copying from local variable 4 and possibly the 2nd item is irrelevant or I am copying from local variable 8 (4 = 2*2)? in this case and putting the on the stack pointer?

Any clarification is appreciated.

Thanks,
Steve

So, loadw is a bit confusing and

No, loadw can’t read local variables. It reads a two-byte value from an address in main memory. As Mike said:

the address to be read is just operand_1 + 2 * operand_2.

1 Like

Thanks for the clarification about loadw reading a two byte from an address composed of operand1 + 2*operand2. I assume that this is true regardless of the form of loadw?

Thanks,
Steve

There is only one form of @loadw. The first operand is evaluated - if it’s a constant, that’s the value, if it’s a variable, the value of the variable is the value. Same thing with the second operand. Then the value of the first operand + 2 * the value of the second operand is calculated, and the word value at this address is put into the variable on the right hand side of the arrow.

(All Z-code opcodes start by evaluating all their operands - no exceptions. In a few cases, such as @inc, the value obtained is to be interpreted as a variable number which the opcode will operate on.)

1 Like

Hi Fredrik,

Thank you for the further clarification. And thanks to everyone for your inputs as well.

Cheers,
Steve

1 Like