Execute Computer/Zero: Difference between revisions

→‎{{header|J}}: alternate approach
m (J: get rid of LIT pseudocode -- numbers are clear enough by themselves)
(→‎{{header|J}}: alternate approach)
Line 132:
 
assemble=: {{
acc=: pc=: 0
if. 0=L. y do.
delim=. {.((tolower y)-.(":i.10),;OPS),LF
Line 173 ⟶ 172:
6
exec assemble 'NOP; NOP; STP; NOP; LDA 3; SUB 29; BRZ 18; LDA 3; STA 29; BRZ 14; LDA 1; ADD 31; STA 1; JMP 2; LDA; ADD 31; STA; JMP 2; LDA 3; STA 29; LDA 1; ADD 30; ADD 3; STA 1; LDA; ADD 30; ADD 3; STA; JMP 2; 0; 1; 3'
0</lang>
 
=== Alternate approach ===
 
Another approach would be to eliminate the 'assemble' command and implement the opcodes as native J commands (which build a representation of the memory space). To conform with J syntax, the right arguments of these opcodes is required (so you need a 0 even for NOP and STP). The implementation of <code>exec</code> remains much the same as before, but it's convenient to have exec save the code to memory. In other words:
 
<lang J>opcode=: {{
(m+{.y),}.y
:
32{.x,m opcode y
}}
 
({{((x)=: y opcode)0}}&>32*i.@#);:'NOP LDA STA ADD SUB BRZ JMP STP'
 
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=: {{
mem=: y
accpc=: pcacc=: 0
while. 0<:pc do. exec1'' end.
acc
}}</lang>
 
and:
 
<lang J> exec LDA 3 ADD 4 STP 0 2 2
4
exec LDA 12 ADD 10 STA 12 LDA 11 SUB 13 STA 11 BRZ 8 JMP 0 LDA 12 STP 0 8 7 0 1
56
exec 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 1 1 0 8 1
55
exec 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 0 28 1 0 0 0 6 0 2 26 5 20 3 30 1 22 4 24
6
exec 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 0 1 3
0</lang>
 
6,951

edits