MARIE relies heavily on (global) pointers in data: its only indirection capability is to deference a memory location, via AddI, JumpI, LoadI, and StoreI.
Common sequence for example, is to LoadI pointer
for dereference, then Load pointer; Add One; Store pointer
to advance the pointer for its next usage.
But it does not have a direct mechanism to initialize a pointer variable to refer to a memory location by an assembler label. So, many either assemble twice, first time to figure the hex address of some data (then adjust source code), or, place data close to the beginning (where it is easy to pre-compute its address) and jump around the data at start.
Is there a way to initialize a pointer using a label? For example, I'd like the data location with label pointer
to be initialized to hold/refer-to the address of array
:
pointer, DEC array
/ ...
array, DEC 1000
DEC 1001
DEC 1002
However, this is a syntax error in MARIE assembly.
Can it be done: initializing a data word to refer to a label?
FYI, I'm using https://marie.js.org/ for simulation.
CodePudding user response:
Can it be done: initializing a data word to refer to a label?
Yes, it can be done.
While DEC and HEX are normally used to declare global/initialized data, these do not accept labels as arguments, only decimal and hex constants, respectively.
However: MARIE instructions and data are 16-bits wide, but the address space is only 12-bits wide. The instruction format is 4-bit opcode followed by 12-bit address (and these addresses are absolute rather than, say, pc-relative). So, we can use the instruction whose opcode is 0, JnS, to form a data value that is a pointer to a label.
pointer, JnS array / this is intended as data, not code
/ despite the use of an instruction opcode
/ ...
array, DEC 1000
DEC 1001
DEC 1002
(The MAR register is only 12 bits, so when an address is moved from the 16-bit MBR to the 12 bit MAR, the upper 4 bits are discarded. As a result, we might use any opcode to refer to the label, but using the opcode whose value is 0 is probably the best choice, since that way, in 16 bits the pointer has the same value as in 12 bits.)