Execute Computer/Zero: Difference between revisions

Content added Content deleted
m (→‎{{header|Wren}}: Added note to preamble.)
(J)
Line 13: Line 13:


<br><br>
<br><br>

=={{header|J}}==

The reference programs are not supplied in source code form, so in its current form this task is a bit unclear.

Here's one approach which might be sufficiently close to the task requirements:

<lang J>OPS=: ;:'nop lda sta add sub brz jmp stp lit'

assemble1=: {{
ins=. ;:tolower y
cod=. OPS i. {.ins
val=. {.0".;}.ins
if. 8=cod do.
assert. 256>val
val
else.
assert. 8>cod
assert. 32>val
val+32*cod
end.
}}

assemble=: {{
acc=: pc=: 0
code=. assemble1@> cutLF^:(0=L.) y
mem=: code (i.#code)} 32#0
}}

exec1=: {{
'cod val'=. 0 32#:pc{mem
pc=: 32|pc+1
select. cod
case. 0 do.
case. 1 do. acc=: val{mem
case. 2 do. mem=: acc val} mem
case. 3 do. acc=: 256|acc+val{mem
case. 4 do. acc=: 256|acc-val{mem
case. 5 do. pc=: 32|pc[^:(*acc) val
case. 6 do. pc=: 32|val
case. 7 do. pc=: __
end.
}}

exec=: {{
pc=: acc=: 0
while. 0<:pc do. exec1'' end.
acc
}}</lang>

With this implementation, we can assemble and run representations of the five suggested programs:

<lang J> exec assemble 'LDA 3';'ADD 4';'STP';'NOP 2';'NOP 2'
4
exec assemble 'LDA 12';'ADD 10';'STA 12';'LDA 11';'SUB 13';'STA 11';'BRZ 8';'JMP 0';'LDA 12';'STP 0';'NOP 8';'NOP 7';'NOP 0';'NOP 1'
56
exec assemble 'LDA 14';'STA 15';'ADD 13';'STA 14';'LDA 15';'STA 13';'LDA 16';'SUB 17';'BRZ 11';'STA 16';'JMP 0';'LDA 14';'STP 0';'NOP 1';'NOP 1';'NOP 0';'NOP 8';'NOP 1'
55
exec assemble 'LDA 13';'ADD 15';'STA 5';'ADD 16';'STA 7';'NOP 0';'STA 14';'NOP 0';'BRZ 11';'STA 15';'JMP 0';'LDA 14';'STP 0';'LDA 0';'NOP 0';'NOP 28';'NOP 1';'NOP 0';'NOP 0';'NOP 0';'NOP 6';'NOP 0';'NOP 2';'NOP 26';'NOP 5';'NOP 20';'NOP 3';'NOP 30';'NOP 1';'NOP 22';'NOP 4';'NOP 24'
6
exec assemble 'NOP 0';'NOP 0';'STP 0';'NOP 0';'LDA 3';'SUB 29';'BRZ 18';'LDA 3';'STA 29';'BRZ 14';'LDA 1';'ADD 31';'STA 1';'JMP 2';'LDA 0';'ADD 31';'STA 0';'JMP 2';'LDA 3';'STA 29';'LDA 1';'ADD 30';'ADD 3';'STA 1';'LDA 0';'ADD 30';'ADD 3';'STA 0';'JMP 2';'NOP 0';'NOP 1';'NOP 3'
0</lang>


=={{header|Wren}}==
=={{header|Wren}}==