XOR 8 SUB 8
STORE 20 STORE 21 ADD 20 ADD 20 STORE 20 ADD 20 ADD 21
X: LOAD Y 9023 Z: -70 JGZ 9 JLZ X MOD Z Y: XOR X
00000006 0000233F FFFFFFBA F0000009 E0000000 80000002 B0000000
JUMP start sum: 0 one: 1 count: 0 start: IN 5 JZ compute ; got zero so we are done reading ADD sum STORE sum ; sum += value read LOAD count ADD one STORE count ; count += 1 JUMP start ; go read the next number compute: LOAD sum DIV count OUT 10 ; output sum / count end: JUMP end
JUMP start one: 1 n: 0 start: IN 30 STORE n ; number of items left to read read: LOAD n JLZ done ; no more left to read IN 31 write: STORE 100 LOAD write ; OMG that is an instruction in memory ADD one STORE write ; OMG we are changing our program (cooooooool) LOAD n SUB one STORE n ; count down number of items left to read JUMP read end: JUMP end(Technically this program doesn't work if you ask it to read more than 268435455 items—do you see why?).
I will also accept an answer of “No, because we don’t know the memory address to write to before the program is run, and our computer has no way to use a computed memory address.” In this case, a convenient new instruction would be STOREINDIRECT x
, which would fetch the value in memory address $x$ (call it $y$), and then store the value in the accumulator into address $y$. By the way, all real computers actually do this.